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

PDF
Java day 2016.pptx
PDF
Typescript is the best by Maxim Kryuk
PDF
Typescript is the best
PDF
The Ring programming language version 1.5 book - Part 14 of 31
PDF
ConFess Vienna 2015 - Metaprogramming with Groovy
PDF
The Ring programming language version 1.5.2 book - Part 79 of 181
PPTX
JS Fest 2018. Виталий Ратушный. ES X
ODP
AST Transformations at JFokus
Java day 2016.pptx
Typescript is the best by Maxim Kryuk
Typescript is the best
The Ring programming language version 1.5 book - Part 14 of 31
ConFess Vienna 2015 - Metaprogramming with Groovy
The Ring programming language version 1.5.2 book - Part 79 of 181
JS Fest 2018. Виталий Ратушный. ES X
AST Transformations at JFokus

What's hot (20)

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

Viewers also liked (12)

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

Similar to Annotation processor and compiler plugin (20)

PPTX
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
PPTX
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
PDF
Java Annotation Processing: A Beginner Walkthrough
PDF
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
PDF
Write code that writes code!
PDF
Annotation Processing in Android
PDF
Annotation processing
PDF
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
PPTX
Dive in Annotation Processor
PDF
Infinum Android Talks #02 - How to write an annotation processor in Android
PDF
Android: Dive in KAPT
PDF
Daggerate your code - Write your own annotation processor
PDF
On Processors, Compilers and @Configurations
PDF
Gwt and JSR 269's Pluggable Annotation Processing API
PDF
Custom Annotations in Java with Project Lombok
PPTX
Java Custom Annotations- Part1
PPTX
Annotation processing
PPTX
Code generation with javac plugin
PDF
Annotation Processing
PPTX
Annotations in Java
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Java Annotation Processing: A Beginner Walkthrough
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code!
Annotation Processing in Android
Annotation processing
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Dive in Annotation Processor
Infinum Android Talks #02 - How to write an annotation processor in Android
Android: Dive in KAPT
Daggerate your code - Write your own annotation processor
On Processors, Compilers and @Configurations
Gwt and JSR 269's Pluggable Annotation Processing API
Custom Annotations in Java with Project Lombok
Java Custom Annotations- Part1
Annotation processing
Code generation with javac plugin
Annotation Processing
Annotations in Java

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Essential Infomation Tech presentation.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Transform Your Business with a Software ERP System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Essential Infomation Tech presentation.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Which alternative to Crystal Reports is best for small or large businesses.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
Digital Strategies for Manufacturing Companies
How to Migrate SBCGlobal Email to Yahoo Easily
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Transform Your Business with a Software ERP System
Understanding Forklifts - TECH EHS Solution
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
VVF-Customer-Presentation2025-Ver1.9.pptx

Annotation processor and compiler plugin