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 (20)

PPTX
Is your C# optimized
Woody Pewitt
 
PDF
The Ring programming language version 1.5.4 book - Part 82 of 185
Mahmoud Samir Fayed
 
PPTX
What's new in C# 8.0 (beta)
Muralidharan Deenathayalan
 
PDF
The Ring programming language version 1.5.3 book - Part 92 of 184
Mahmoud Samir Fayed
 
PDF
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
 
PDF
The Ring programming language version 1.7 book - Part 88 of 196
Mahmoud Samir Fayed
 
ODP
AST Transformations
HamletDRC
 
PDF
The Ring programming language version 1.5.3 book - Part 13 of 184
Mahmoud Samir Fayed
 
PDF
ECMAScript 2017
max peng
 
PDF
The Ring programming language version 1.7 book - Part 9 of 196
Mahmoud Samir Fayed
 
ODP
Ast transformations
HamletDRC
 
PPTX
Building native Android applications with Mirah and Pindah
Nick Plante
 
PPT
ITK Tutorial Presentation Slides-944
Kitware Kitware
 
PDF
The Ring programming language version 1.9 book - Part 19 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 184 of 189
Mahmoud Samir Fayed
 
PDF
Nseg41 あなたの知らないjavascriptの基本
hATrayflood
 
PPTX
Roslyn and C# 6.0 New Features
Michael Step
 
PDF
The Ring programming language version 1.8 book - Part 91 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 17 of 202
Mahmoud Samir Fayed
 
PDF
Implement ERC20 on TestRPC
KC Tam
 
Is your C# optimized
Woody Pewitt
 
The Ring programming language version 1.5.4 book - Part 82 of 185
Mahmoud Samir Fayed
 
What's new in C# 8.0 (beta)
Muralidharan Deenathayalan
 
The Ring programming language version 1.5.3 book - Part 92 of 184
Mahmoud Samir Fayed
 
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
 
The Ring programming language version 1.7 book - Part 88 of 196
Mahmoud Samir Fayed
 
AST Transformations
HamletDRC
 
The Ring programming language version 1.5.3 book - Part 13 of 184
Mahmoud Samir Fayed
 
ECMAScript 2017
max peng
 
The Ring programming language version 1.7 book - Part 9 of 196
Mahmoud Samir Fayed
 
Ast transformations
HamletDRC
 
Building native Android applications with Mirah and Pindah
Nick Plante
 
ITK Tutorial Presentation Slides-944
Kitware Kitware
 
The Ring programming language version 1.9 book - Part 19 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 184 of 189
Mahmoud Samir Fayed
 
Nseg41 あなたの知らないjavascriptの基本
hATrayflood
 
Roslyn and C# 6.0 New Features
Michael Step
 
The Ring programming language version 1.8 book - Part 91 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 17 of 202
Mahmoud Samir Fayed
 
Implement ERC20 on TestRPC
KC Tam
 

Viewers also liked (12)

PDF
CS-Cart V4.3 2015 09-12-tokyo
Toshihiro Yoshiura
 
PDF
Comunicado
Marcha Patriótica
 
PDF
Tendencias revision de pagos al extranjero
Aldo O Camacho Murillo
 
PDF
RLW type pole mounted vacuum load break switch
Rockwill T&D Group Manufacturer,Contractor, OEM, ODM
 
PDF
CS-Cartセミナー東京 20151128
Toshihiro Yoshiura
 
PPTX
How do I use the Daily Report module to send a consolidated summary to paren...
ediffyofficial
 
PPTX
How do I use the Send Via Excel Feature
ediffyofficial
 
PPTX
Fecal Microbiota Transplantation
John Little
 
PDF
Mahikel peñuela ing
Mahikel Peñuela
 
PPTX
Liturgia de las horas
miguelsilvaaguiar
 
PPTX
コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】
Shohei Komatsu
 
CS-Cart V4.3 2015 09-12-tokyo
Toshihiro Yoshiura
 
Comunicado
Marcha Patriótica
 
Tendencias revision de pagos al extranjero
Aldo O Camacho Murillo
 
RLW type pole mounted vacuum load break switch
Rockwill T&D Group Manufacturer,Contractor, OEM, ODM
 
CS-Cartセミナー東京 20151128
Toshihiro Yoshiura
 
How do I use the Daily Report module to send a consolidated summary to paren...
ediffyofficial
 
How do I use the Send Via Excel Feature
ediffyofficial
 
Fecal Microbiota Transplantation
John Little
 
Mahikel peñuela ing
Mahikel Peñuela
 
Liturgia de las horas
miguelsilvaaguiar
 
コンテンツマーケティング勉強会資料(GREE Mini Tech Talk)【Ginzametrics】
Shohei Komatsu
 
Ad

Similar to Annotation processor and compiler plugin (20)

PPTX
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Jorge Hidalgo
 
PPTX
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Jorge Hidalgo
 
PDF
Java Annotation Processing: A Beginner Walkthrough
Mahfuz Islam Bhuiyan
 
PDF
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
DroidConTLV
 
PDF
Write code that writes code!
Jason Feinstein
 
PDF
Annotation Processing in Android
emanuelez
 
PDF
Annotation processing
Felipe Theodoro
 
PDF
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Java User Group Latvia
 
PPTX
Dive in Annotation Processor
Fandy Gotama
 
PDF
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum
 
PDF
Android: Dive in KAPT
Fandy Gotama
 
PDF
Daggerate your code - Write your own annotation processor
Bartosz Kosarzycki
 
PDF
On Processors, Compilers and @Configurations
Netcetera
 
PDF
Gwt and JSR 269's Pluggable Annotation Processing API
Arnaud Tournier
 
PDF
Custom Annotations in Java with Project Lombok
Yann-Gaël Guéhéneuc
 
PPTX
Java Custom Annotations- Part1
Mohammad Sabir Khan
 
PPTX
Annotation processing
Florent Champigny
 
PPTX
Code generation with javac plugin
Oleksandr Radchykov
 
PDF
Annotation Processing
Jintin Lin
 
PPTX
Annotations in Java
Kirill Kulakov
 
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Jorge Hidalgo
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Jorge Hidalgo
 
Java Annotation Processing: A Beginner Walkthrough
Mahfuz Islam Bhuiyan
 
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
DroidConTLV
 
Write code that writes code!
Jason Feinstein
 
Annotation Processing in Android
emanuelez
 
Annotation processing
Felipe Theodoro
 
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Java User Group Latvia
 
Dive in Annotation Processor
Fandy Gotama
 
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum
 
Android: Dive in KAPT
Fandy Gotama
 
Daggerate your code - Write your own annotation processor
Bartosz Kosarzycki
 
On Processors, Compilers and @Configurations
Netcetera
 
Gwt and JSR 269's Pluggable Annotation Processing API
Arnaud Tournier
 
Custom Annotations in Java with Project Lombok
Yann-Gaël Guéhéneuc
 
Java Custom Annotations- Part1
Mohammad Sabir Khan
 
Annotation processing
Florent Champigny
 
Code generation with javac plugin
Oleksandr Radchykov
 
Annotation Processing
Jintin Lin
 
Annotations in Java
Kirill Kulakov
 
Ad

Recently uploaded (20)

PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

Annotation processor and compiler plugin