SlideShare a Scribd company logo
Annotation processor
and compiler plugin
as code generators.
Hello!
I am Oleksandr Radchykov
committed.chinchilla@gmail.com
belict
Agenda
1.Motivation
2.How to generate code?
3.How to annotation processor
4.How to create plugin
5.Demo
Motivation for
code generation
Getting rid of boilerplate
class MyService {
private final String name;
private final Integer version;
public MyService(String name, Integer version) {
this.name = name;
This.version = version;
}
public String getName() { return name; }
public Integer getVersion() { return version; }
}
@Getter
@RequiredArgsConstructor
class MyService {
private final String name;
private final Integer version;
}
6
Getting rid from annoying
code duplication
public String validate(Object input) {
if (income instanceof String
&& !((String) income).isEmpty()) {
return (String) income;
}
…
}
public String validate(Object input) {
if (income instanceof String && !income.isEmpty()) {
return income;
}
…
}
8
Two ways to
generate code
Annotation Processing
▷Java 5+
▷Takes java code and generates source files
▷You can not manipulate an existing java code
▷Use the model of processing rounds
Javac Plugin
▷Since Java 8
▷Can give us access to AST trees parsed from
source code
▷Can be used to add compile-time checks
▷Can run on different phases of compilation
process
Annotation
processor
1. Implement AbstractProcessor
interface
import javax.annotation.processing.AbstractProcessor;
class MyProcessor implements AbstractProcessor {
@Override
public boolean process(
Set<? Extends TypeElement> annotations,
RoundEnvironment roundEnv) {
/*...*/
}
}
2. Create provider-configuration file
MyProcessor.jar
- com/example
- MyProcessor.class
- META-INF/services
- javax.annotation.processing.Processor
Provider-configuration file should declare all
annotation processors you want to expose.
3. Compilation
$ javac –cp processor.jar 
*sources*.java
Javac plugin
1. Implement Plugin interface
import com.sun.source.util.Plugin;
class MyPlugin implements Plugin {
@Override
public String getName() { return “pluginName”; }
@Override
public void init(JavacTask javacTask,
String… strings) {
/*...*/
}
}
2. Create provider-configuration file
MyPlugin.jar
- com/example
- MyPlugin.class
- META-INF/services
- com.sun.source.util.Plugin
Provider-configuration file should declare all plugins
you want to expose.
3. Compilation
$ javac -Xplugin:PluginName 
–cp /path/to/plugin 
Source.java
Demo
Questions?
Thanks
committed.chinchilla@gmail.com
belict

More Related Content

What's hot

Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
Nseg41 あなたの知らないjavascriptの基本
Nseg41 あなたの知らないjavascriptの基本Nseg41 あなたの知らないjavascriptの基本
Nseg41 あなたの知らないjavascriptの基本
hATrayflood
 

What's hot (20)

Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.5.4 book - Part 82 of 185
 
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
 
The Ring programming language version 1.5.3 book - Part 92 of 184
The Ring programming language version 1.5.3 book - Part 92 of 184The Ring programming language version 1.5.3 book - Part 92 of 184
The Ring programming language version 1.5.3 book - Part 92 of 184
 
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
 
The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
The Ring programming language version 1.5.3 book - Part 13 of 184
The Ring programming language version 1.5.3 book - Part 13 of 184The Ring programming language version 1.5.3 book - Part 13 of 184
The Ring programming language version 1.5.3 book - Part 13 of 184
 
ECMAScript 2017
ECMAScript 2017ECMAScript 2017
ECMAScript 2017
 
The Ring programming language version 1.7 book - Part 9 of 196
The Ring programming language version 1.7 book - Part 9 of 196The Ring programming language version 1.7 book - Part 9 of 196
The Ring programming language version 1.7 book - Part 9 of 196
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
ITK Tutorial Presentation Slides-944
ITK Tutorial Presentation Slides-944ITK Tutorial Presentation Slides-944
ITK Tutorial Presentation Slides-944
 
The Ring programming language version 1.9 book - Part 19 of 210
The Ring programming language version 1.9 book - Part 19 of 210The Ring programming language version 1.9 book - Part 19 of 210
The Ring programming language version 1.9 book - Part 19 of 210
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
Nseg41 あなたの知らないjavascriptの基本
Nseg41 あなたの知らないjavascriptの基本Nseg41 あなたの知らないjavascriptの基本
Nseg41 あなたの知らないjavascriptの基本
 
Roslyn and C# 6.0 New Features
Roslyn and C# 6.0 New FeaturesRoslyn and C# 6.0 New Features
Roslyn and C# 6.0 New Features
 
The Ring programming language version 1.8 book - Part 91 of 202
The Ring programming language version 1.8 book - Part 91 of 202The Ring programming language version 1.8 book - Part 91 of 202
The Ring programming language version 1.8 book - Part 91 of 202
 
The Ring programming language version 1.8 book - Part 17 of 202
The Ring programming language version 1.8 book - Part 17 of 202The Ring programming language version 1.8 book - Part 17 of 202
The Ring programming language version 1.8 book - Part 17 of 202
 
Implement ERC20 on TestRPC
Implement ERC20 on TestRPCImplement ERC20 on TestRPC
Implement ERC20 on TestRPC
 

Viewers also liked

Fecal Microbiota Transplantation
Fecal Microbiota TransplantationFecal Microbiota Transplantation
Fecal Microbiota Transplantation
John Little
 

Viewers also liked (12)

CS-Cart V4.3 2015 09-12-tokyo
CS-Cart V4.3 2015 09-12-tokyoCS-Cart V4.3 2015 09-12-tokyo
CS-Cart V4.3 2015 09-12-tokyo
 
Comunicado
ComunicadoComunicado
Comunicado
 
Tendencias revision de pagos al extranjero
Tendencias revision de pagos al extranjeroTendencias revision de pagos al extranjero
Tendencias revision de pagos al extranjero
 
RLW type pole mounted vacuum load break switch
RLW type pole mounted vacuum load break switchRLW type pole mounted vacuum load break switch
RLW type pole mounted vacuum load break switch
 
Standing type GIS RMU
Standing type GIS RMUStanding type GIS RMU
Standing type GIS RMU
 
CS-Cartセミナー東京 20151128
CS-Cartセミナー東京 20151128CS-Cartセミナー東京 20151128
CS-Cartセミナー東京 20151128
 
How do I use the Daily Report module to send a consolidated summary to paren...
How do I use the Daily Report module to send  a consolidated summary to paren...How do I use the Daily Report module to send  a consolidated summary to paren...
How do I use the Daily Report module to send a consolidated summary to paren...
 
How do I use the Send Via Excel Feature
How do I use the Send Via Excel FeatureHow do I use the Send Via Excel Feature
How do I use the Send Via Excel Feature
 
Fecal Microbiota Transplantation
Fecal Microbiota TransplantationFecal Microbiota Transplantation
Fecal Microbiota Transplantation
 
Mahikel peñuela ing
Mahikel peñuela ingMahikel peñuela ing
Mahikel peñuela ing
 
Liturgia de las horas
Liturgia de las horasLiturgia de las horas
Liturgia de las horas
 
コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】
コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】
コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】
 

Similar to Annotation processor and compiler plugin

Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Carol McDonald
 
Whidbey old
Whidbey old Whidbey old
Whidbey old
grenaud
 

Similar to Annotation processor and compiler plugin (20)

Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac plugin
 
Linq intro
Linq introLinq intro
Linq intro
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
.gradle 파일 정독해보기
.gradle 파일 정독해보기.gradle 파일 정독해보기
.gradle 파일 정독해보기
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Clean up your code with C#6
Clean up your code with C#6Clean up your code with C#6
Clean up your code with C#6
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
 
Whidbey old
Whidbey old Whidbey old
Whidbey old
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#.NET Foundation, Future of .NET and C#
.NET Foundation, Future of .NET and C#
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
 
ASP.NET
ASP.NETASP.NET
ASP.NET
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 

Recently uploaded (20)

Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 

Annotation processor and compiler plugin