SlideShare a Scribd company logo
1 of 35
CODE GENERATION WITH ANNOTATION
PROCESSORS:
STATE OF THE ART IN JAVA 9
[CON3282]
ABOUT THE SPEAKERS
Copyright © 2017 Accenture. All rights reserved. 2
Welcome to our session!
Julio Palma Vázquez
Member – Málaga JUG
Technology Architect – Accenture Technology
Accenture Global Java Champion
Accenture Delivery Center in Spain
Mountain biker, AD&D player, Sid Meier’s Civilization gamer,
LotR fan, …
@restalion
https://github.com/restalion
Jorge Hidalgo
Coordinator – Málaga JUG
Global Java Lead – Accenture Technology
Java, Architecture & DevOps Lead
Accenture Delivery Center in Spain
Father of two children, husband, whistle player, video gamer, sci-
fi ‘junkie’, Star Wars ‘addict’, Lego brick ‘wielder’, Raspberry Pi
fan, … LLAP!
@_deors
https://github.com/deors
• Overview of Annotations and Annotation Processors in Java
• What’s new in Java 9 APT API
• Learn how to create Annotation Processors
• Learn how to use Annotation Processors to generate source code
• Annotation Processors and Modules in Java 9
• With lots of examples and source code
CODE GENERATION IN THE JAVA COMPILER
Copyright © 2017 Accenture. All rights reserved. 3
Objetives for the session
Please raise your hands if you are familiar with Annotations, know how to use them or are
even capable of writing Annotation Types
POLL #1
Copyright © 2017 Accenture. All rights reserved. 4
ANNOTATIONS IN
JAVA LANGUAGE
Copyright © 2017 Accenture. All rights reserved. 5
• Annotations were first introduced in Java 5
• Add metadata capabilities to the Java language:
• Build/deployment information
• Configuration properties
• Compiler behavior
• Quality checks
• Dependency injection
• Persistence mapping
• ...
ANNOTATIONS IN THE JAVA LANGUAGE
Copyright © 2017 Accenture. All rights reserved. 6
History
• Annotations always appear before the annotated piece of code
• Annotations can ‘decorate’...
• Packages
• Types (classes, interfaces, enums, annotation types)
• Variables (class, instance and local variables)
• Constructors, methods and their parameters
• Generic type parameter (since Java 8)
• Any type use (since Java 8)
ANNOTATIONS IN THE JAVA LANGUAGE
Copyright © 2017 Accenture. All rights reserved. 7
Overview
• Can be grouped, nested, repeated... very powerful!
Map<@NonNull String, @NonEmpty List<@Readonly Document>> docStore;
• Annotations may be used just as a ‘marker’
@NonNull String email;
• They may contain name-value pairs, and arrays
@Author(name = "Albert",
created = "17/09/2010",
revision = 3,
reviewers = {"George", "Fred"})
public class SomeClass {...}
• Can be defined with default values, and then omitted when used
• When it has only one attribute named value, it can be omitted, too
@DatabaseTable(“MY_TABLE”) public class MyTable {...}
ANNOTATIONS IN THE JAVA LANGUAGE
Copyright © 2017 Accenture. All rights reserved. 8
Overview
HOW TO CREATE
ANNOTATIONS AND
ANNOTATION
PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 9
Please raise your hands if you are familiar with Annotation Processors, their capabilities or
know how to write new ones
POLL #2
Copyright © 2017 Accenture. All rights reserved. 10
• A special type of interface
public @interface Author {
String name();
String created();
int revision() default 1;
String[] reviewers() default {};
}
• Only primitives, enum types, annotation types, String, Class and arrays of them are
allowed as elements
• May be annotated itself, i.e. to define the retention policy or targets
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
public @interface Author {...}
Can trigger custom actions at compile time managed by
Annotation Processors!
ANNOTATIONS
Copyright © 2017 Accenture. All rights reserved. 11
Create an Annotation Type
• Annotation Processor:
– Implements javax.annotation.processing.Processor
– Or extends javax.annotation.processing.AbstractProcessor
• May use three annotations to configure itself:
– jx.a.p.SupportedAnnotationTypes – Used to flag which Annotation Types are managed by the
– jx.a.p.SupportedSourceVersion – Used to flag the latest Java source level supported by the processor
– jx.a.p.SupportedOptions – Used to register custom parameters that may be passed through the
• TL;DR – Implement the process() method
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 12
Annotation Processing API
• Annotation processing happens in ‘rounds’
• In each round, a subset of sources are compiled, and then
processors matching the annotations found on those sources
are called
• Processing ends when no more sources/annotations are
pending
• Processors may claim all annotation types with a wildcard
• process() method has two parameters:
– Set<? extends jx.lang.model.element.TypeElement> – subset of annotations being processed
– jx.a.p.RoundEnvironment – access to information about the current and previous round
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 13
Annotation Processing API
jx.a.p.ProcessingEnvironment also provides access to the Filer utility that allows for easy file creation
• Files are placed in the right folder as required by the compiler settings
• Folder hierarchy matching the package name
JavaFileObject jfo = processingEnv.getFiler()
.createSourceFile(newClassQualifiedName);
Writer writer = jfo.openWriter();
...or...
JavaFileObject jfo = processingEnv.getFiler()
.createClassFile(newClassQualifiedName);
OutputStream os = jfo.openOutputStream();
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 14
Creating Files
Annotation Processors leverage the standard services mechanism
• Package the processors in a Jar file
• Include file META-INF/services/javax.annotation.processing.Processor
• Contents are the fully qualified names of the bundled processors, one per line
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 15
How the compiler knows?
Find more information about APT in JavaOne 2014 session:
https://www.slideshare.net/deors/javaone-2014-con2013-code-generation-in-the-java-compiler-annotation-processors-do-the-hard-work
https://www.youtube.com/watch?v=xswPPwYPAFM
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 16
Running Annotation Processors
Multiple ways:
• javac
• Apache Maven builds
• Continuous Integration builds
• IDE
ANNOTATION PROCESSORS
Copyright © 2017 Accenture. All rights reserved. 17
Running Annotation Processors
ADAPT LEGACY APT
PROJECTS TO JAVA
9
Copyright © 2017 Accenture. All rights reserved. 18
• Update <java.version> tag in pom.xml
<java.version>1.9</java.version>
• Update version of maven-compiler-plugin
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
• Upgrade the supported source version in Processor class
@SupportedAnnotationTypes(“com.foo.AnnotationSample")
@SupportedSourceVersion(SourceVersion.RELEASE_9)
public class AnnotationSampleProcessor
ADAPT LEGACY CODE
Copyright © 2017 Accenture. All rights reserved. 19
Upgrading your code to Java 9
• mvn clean install finish without errors:
ANNOTATIONS API IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 20
Upgrading your code to Java 9
DEMO
ANNOTATIONS API IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 21
Executing upgraded projects
Upgraded legacy code sample can be found in github:
https://github.com/deors/deors.demos.annotations
ADAPT LEGACY CODE
Copyright © 2017 Accenture. All rights reserved. 22
Upgrading your code to Java 9
WHAT’S NEW IN
JAVA 9 APT API
Copyright © 2017 Accenture. All rights reserved. 23
ANNOTATIONS API IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 24
Overview
• New @Generated Annotation
• To add information about the annotation processor, date, and comments:
@Generated(value = "com.foo.processors.SampleProcessor",
date = "2017-09-28T09:43:28.170886",
comments = "Sample")
• Processed as a subsequent step by the annotation processor.
• Two new methods in RoundEnvironment interface
• Simplify processing of multiple annotations
default Set<? extends Element> getElementsAnnotatedWithAny​(
Set<Class<? extends Annotation>> annotations);
default Set<? extends Element> getElementsAnnotatedWithAny​(
TypeElement... annotations);
ANNOTATIONS API IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 25
Overview
• Changes in all methods of Filer interface.
To manage files in a module, module name is prefixed to the type name and separated using a
“/” character, i.e.:
JavaFileObject jfo = processingEnv.getFiler().createSourceFile(
"com.foo/com.foo.Bar");
JavaFileObject jfo = processingEnv.getFiler().getResource(location,
"com.foo/com.foo", "Bar");
NEW FEATURES IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 26
• It’s a best practice to include this annotation in the generated code:
bw.append("@Generated(value = "" + this.getClass().getName() +
"" , date = "" +
LocalDateTime.now() +
"", comments = "Test generator")");
Generated Annotation
Comments
MUST have the name of
the code generator
Date when the source was generated
• During annotation processing,the Generated annotation will be found but nothing is done,
it’s just informational.
• Generated code:
NEW FEATURES IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 27
Generated Annotation
Name of the code generator
Generation date Comments
ANNOTATIONS API IN JAVA 9
Copyright © 2017 Accenture. All rights reserved. 28
Module
ANNOTATION
PROCESSORS AND
MODULES IN JAVA
9
Copyright © 2017 Accenture. All rights reserved. 29
ANNOTATION PROCESSORS AND MODULES
Copyright © 2017 Accenture. All rights reserved. 30
Creating classes in modules
• In previous examples we have three
different jar files:
• Annotation type
• Annotation processor
• classes that use annotations
• Dependencies are managed using the
regular classpath
30
annotation
annotation.processor
client
ANNOTATION PROCESSORS AND MODULES
Copyright © 2017 Accenture. All rights reserved. 31
Grouping classes in modules
• One module per jar file.
• Put a module-info.java file into each module.
• Each module-info file needs to define exported
packages and required modules.
annotation
annotation.processor
client
ANNOTATION PROCESSORS AND MODULES
Copyright © 2017 Accenture. All rights reserved. 32
Creating classes in modules
annotation module:
module com.foo.annotation {
exports com.foo.annotation;
}
annotation.processor module:
module com.foo.annotation.processors {
requires com.foo.annotation;
requires java.compiler;
}
client module:
module com.foo.client {
exports com.foo.client;
requires com.foo.annotation;
requires java.compiler;
}
Why? Because we add
@Generated annotation
annotation
annotation.processor
client
ANNOTATION PROCESSORS AND MODULES
Copyright © 2017 Accenture. All rights reserved. 33
Creating classes in modules
DEMO
https://github.com/restalion/deors.demos.annotations/tree/jdk9
Copyright © 2017 Accenture. All rights reserved. 34
• APT API is as powerful as in previous versions.
• All your legacy code still works as expected.
• You can add a couple a of new functionalities:
• @Generator annotation
• Simplified processing of multiple annotations
• You can also manage file generation in other modules
SUMMARY
Q & A
Copyright © 2017 Accenture. All rights reserved. 35
Many thanks for attending!
@_deors
https://github.com/deors
@restalion
https://github.com/restalion

More Related Content

What's hot

Getting started with Spring Security
Getting started with Spring SecurityGetting started with Spring Security
Getting started with Spring SecurityKnoldus Inc.
 
Introduction to SAML 2.0
Introduction to SAML 2.0Introduction to SAML 2.0
Introduction to SAML 2.0Mika Koivisto
 
Swagger / Quick Start Guide
Swagger / Quick Start GuideSwagger / Quick Start Guide
Swagger / Quick Start GuideAndrii Gakhov
 
Getting Started in Pentesting the Cloud: Azure
Getting Started in Pentesting the Cloud: AzureGetting Started in Pentesting the Cloud: Azure
Getting Started in Pentesting the Cloud: AzureBeau Bullock
 
A Pattern Language for Microservices
A Pattern Language for MicroservicesA Pattern Language for Microservices
A Pattern Language for MicroservicesChris Richardson
 
Percona Live 2012PPT: MySQL Query optimization
Percona Live 2012PPT: MySQL Query optimizationPercona Live 2012PPT: MySQL Query optimization
Percona Live 2012PPT: MySQL Query optimizationmysqlops
 
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud Services
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud ServicesOracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud Services
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud ServicesMichael Hichwa
 
OpenId Connect Protocol
OpenId Connect ProtocolOpenId Connect Protocol
OpenId Connect ProtocolMichael Furman
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)CODE WHITE GmbH
 
Restful api design
Restful api designRestful api design
Restful api designMizan Riqzia
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & DevelopmentAshok Pundit
 

What's hot (20)

Getting started with Spring Security
Getting started with Spring SecurityGetting started with Spring Security
Getting started with Spring Security
 
Introduction to SAML 2.0
Introduction to SAML 2.0Introduction to SAML 2.0
Introduction to SAML 2.0
 
Swagger / Quick Start Guide
Swagger / Quick Start GuideSwagger / Quick Start Guide
Swagger / Quick Start Guide
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
 
REST in Peace
REST in PeaceREST in Peace
REST in Peace
 
.Net Core
.Net Core.Net Core
.Net Core
 
Getting Started in Pentesting the Cloud: Azure
Getting Started in Pentesting the Cloud: AzureGetting Started in Pentesting the Cloud: Azure
Getting Started in Pentesting the Cloud: Azure
 
OAuth 2.0
OAuth 2.0OAuth 2.0
OAuth 2.0
 
Rest API
Rest APIRest API
Rest API
 
OAuth in the Wild
OAuth in the WildOAuth in the Wild
OAuth in the Wild
 
A Pattern Language for Microservices
A Pattern Language for MicroservicesA Pattern Language for Microservices
A Pattern Language for Microservices
 
Percona Live 2012PPT: MySQL Query optimization
Percona Live 2012PPT: MySQL Query optimizationPercona Live 2012PPT: MySQL Query optimization
Percona Live 2012PPT: MySQL Query optimization
 
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud Services
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud ServicesOracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud Services
Oracle APEX, Oracle Autonomous Database, Always Free Oracle Cloud Services
 
Json Web Token - JWT
Json Web Token - JWTJson Web Token - JWT
Json Web Token - JWT
 
OpenId Connect Protocol
OpenId Connect ProtocolOpenId Connect Protocol
OpenId Connect Protocol
 
Api security
Api security Api security
Api security
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
 
Restful api design
Restful api designRestful api design
Restful api design
 
Json web token
Json web tokenJson web token
Json web token
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 

Similar to JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of the Art in Java 9

JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...Jorge Hidalgo
 
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...DroidConTLV
 
Write code that writes code!
Write code that writes code!Write code that writes code!
Write code that writes code!Jason Feinstein
 
The Art of Metaprogramming in Java
The Art of Metaprogramming in Java  The Art of Metaprogramming in Java
The Art of Metaprogramming in Java Abdelmonaim Remani
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIArnaud Tournier
 
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum
 
Intro to Perfect - LA presentation
Intro to Perfect - LA presentationIntro to Perfect - LA presentation
Intro to Perfect - LA presentationTim Taplin
 
UKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUlrich Krause
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC frameworkMohit Gupta
 
2. introduction to compiler
2. introduction to compiler2. introduction to compiler
2. introduction to compilerSaeed Parsa
 
Ankit Chohan - Java
Ankit Chohan - JavaAnkit Chohan - Java
Ankit Chohan - JavaAnkit Chohan
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxMalla Reddy University
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the BasicsUlrich Krause
 
Introduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQLIntroduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQLanand raj
 
Java ScriptingJava Scripting: One VM, Many Languages
Java ScriptingJava Scripting: One VM, Many LanguagesJava ScriptingJava Scripting: One VM, Many Languages
Java ScriptingJava Scripting: One VM, Many Languageselliando dias
 

Similar to JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of the Art in Java 9 (20)

JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
 
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
 
Write code that writes code!
Write code that writes code!Write code that writes code!
Write code that writes code!
 
The Art of Metaprogramming in Java
The Art of Metaprogramming in Java  The Art of Metaprogramming in Java
The Art of Metaprogramming in Java
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing API
 
Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
 
Infinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum Android Talks #02 - How to write an annotation processor in Android
Infinum Android Talks #02 - How to write an annotation processor in Android
 
Intro to Perfect - LA presentation
Intro to Perfect - LA presentationIntro to Perfect - LA presentation
Intro to Perfect - LA presentation
 
UKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basicsUKLUG 2012 - XPages, Beyond the basics
UKLUG 2012 - XPages, Beyond the basics
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
2. introduction to compiler
2. introduction to compiler2. introduction to compiler
2. introduction to compiler
 
Ankit Chohan - Java
Ankit Chohan - JavaAnkit Chohan - Java
Ankit Chohan - Java
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics
 
Introduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQLIntroduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQL
 
Java se7 features
Java se7 featuresJava se7 features
Java se7 features
 
Java ScriptingJava Scripting: One VM, Many Languages
Java ScriptingJava Scripting: One VM, Many LanguagesJava ScriptingJava Scripting: One VM, Many Languages
Java ScriptingJava Scripting: One VM, Many Languages
 

More from Jorge Hidalgo

GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22Jorge Hidalgo
 
GraalVM - OpenSlava 2019-10-18
GraalVM - OpenSlava 2019-10-18GraalVM - OpenSlava 2019-10-18
GraalVM - OpenSlava 2019-10-18Jorge Hidalgo
 
Architecture 2020 - eComputing 2019-07-01
Architecture 2020 - eComputing 2019-07-01Architecture 2020 - eComputing 2019-07-01
Architecture 2020 - eComputing 2019-07-01Jorge Hidalgo
 
GraalVM - JBCNConf 2019-05-28
GraalVM - JBCNConf 2019-05-28GraalVM - JBCNConf 2019-05-28
GraalVM - JBCNConf 2019-05-28Jorge Hidalgo
 
GraalVM - MálagaJUG 2018-11-29
GraalVM - MálagaJUG 2018-11-29GraalVM - MálagaJUG 2018-11-29
GraalVM - MálagaJUG 2018-11-29Jorge Hidalgo
 
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)Jorge Hidalgo
 
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...Jorge Hidalgo
 
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...Jorge Hidalgo
 
DevOps Te Cambia la Vida - eComputing 2018-07-03
DevOps Te Cambia la Vida - eComputing 2018-07-03DevOps Te Cambia la Vida - eComputing 2018-07-03
DevOps Te Cambia la Vida - eComputing 2018-07-03Jorge Hidalgo
 
Open Source Power Tools - Opensouthcode 2018-06-02
Open Source Power Tools - Opensouthcode 2018-06-02Open Source Power Tools - Opensouthcode 2018-06-02
Open Source Power Tools - Opensouthcode 2018-06-02Jorge Hidalgo
 
JavaOne 2017 CON3276 - Selenium Testing Patterns Reloaded
JavaOne 2017 CON3276 - Selenium Testing Patterns ReloadedJavaOne 2017 CON3276 - Selenium Testing Patterns Reloaded
JavaOne 2017 CON3276 - Selenium Testing Patterns ReloadedJorge Hidalgo
 
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power Tools
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power ToolsJavaOne 2017 CON2902 - Java Code Inspection and Testing Power Tools
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power ToolsJorge Hidalgo
 
All Your Faces Belong to Us - Opensouthcode 2017-05-06
All Your Faces Belong to Us - Opensouthcode 2017-05-06All Your Faces Belong to Us - Opensouthcode 2017-05-06
All Your Faces Belong to Us - Opensouthcode 2017-05-06Jorge Hidalgo
 
Por qué DevOps, por qué ahora @ CHAPI 2017
Por qué DevOps, por qué ahora @ CHAPI 2017Por qué DevOps, por qué ahora @ CHAPI 2017
Por qué DevOps, por qué ahora @ CHAPI 2017Jorge Hidalgo
 
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)Jorge Hidalgo
 
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17Jorge Hidalgo
 
OpenSlava 2016 - Lightweight Java Architectures
OpenSlava 2016 - Lightweight Java ArchitecturesOpenSlava 2016 - Lightweight Java Architectures
OpenSlava 2016 - Lightweight Java ArchitecturesJorge Hidalgo
 
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A Cookbook
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A CookbookJavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A Cookbook
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A CookbookJorge Hidalgo
 
OpenSouthCode 2016 - Accenture DevOps Platform 2016-05-07
OpenSouthCode 2016  - Accenture DevOps Platform 2016-05-07OpenSouthCode 2016  - Accenture DevOps Platform 2016-05-07
OpenSouthCode 2016 - Accenture DevOps Platform 2016-05-07Jorge Hidalgo
 
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost Computers
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost ComputersJavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost Computers
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost ComputersJorge Hidalgo
 

More from Jorge Hidalgo (20)

GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22GraalVM - MadridJUG 2019-10-22
GraalVM - MadridJUG 2019-10-22
 
GraalVM - OpenSlava 2019-10-18
GraalVM - OpenSlava 2019-10-18GraalVM - OpenSlava 2019-10-18
GraalVM - OpenSlava 2019-10-18
 
Architecture 2020 - eComputing 2019-07-01
Architecture 2020 - eComputing 2019-07-01Architecture 2020 - eComputing 2019-07-01
Architecture 2020 - eComputing 2019-07-01
 
GraalVM - JBCNConf 2019-05-28
GraalVM - JBCNConf 2019-05-28GraalVM - JBCNConf 2019-05-28
GraalVM - JBCNConf 2019-05-28
 
GraalVM - MálagaJUG 2018-11-29
GraalVM - MálagaJUG 2018-11-29GraalVM - MálagaJUG 2018-11-29
GraalVM - MálagaJUG 2018-11-29
 
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Commit Conf 2018)
 
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...
Multilanguage Pipelines with Jenkins, Docker and Kubernetes (Oracle Code One ...
 
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...
Multilanguage pipelines with Jenkins, Docker and Kubernetes (DevOpsDays Riga ...
 
DevOps Te Cambia la Vida - eComputing 2018-07-03
DevOps Te Cambia la Vida - eComputing 2018-07-03DevOps Te Cambia la Vida - eComputing 2018-07-03
DevOps Te Cambia la Vida - eComputing 2018-07-03
 
Open Source Power Tools - Opensouthcode 2018-06-02
Open Source Power Tools - Opensouthcode 2018-06-02Open Source Power Tools - Opensouthcode 2018-06-02
Open Source Power Tools - Opensouthcode 2018-06-02
 
JavaOne 2017 CON3276 - Selenium Testing Patterns Reloaded
JavaOne 2017 CON3276 - Selenium Testing Patterns ReloadedJavaOne 2017 CON3276 - Selenium Testing Patterns Reloaded
JavaOne 2017 CON3276 - Selenium Testing Patterns Reloaded
 
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power Tools
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power ToolsJavaOne 2017 CON2902 - Java Code Inspection and Testing Power Tools
JavaOne 2017 CON2902 - Java Code Inspection and Testing Power Tools
 
All Your Faces Belong to Us - Opensouthcode 2017-05-06
All Your Faces Belong to Us - Opensouthcode 2017-05-06All Your Faces Belong to Us - Opensouthcode 2017-05-06
All Your Faces Belong to Us - Opensouthcode 2017-05-06
 
Por qué DevOps, por qué ahora @ CHAPI 2017
Por qué DevOps, por qué ahora @ CHAPI 2017Por qué DevOps, por qué ahora @ CHAPI 2017
Por qué DevOps, por qué ahora @ CHAPI 2017
 
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)
Accenture Liquid Architectures (for Master EMSE UPM-FI - April 2017)
 
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17
La JVM y el Internet de las Cosas @ MálagaJUG 2016-11-17
 
OpenSlava 2016 - Lightweight Java Architectures
OpenSlava 2016 - Lightweight Java ArchitecturesOpenSlava 2016 - Lightweight Java Architectures
OpenSlava 2016 - Lightweight Java Architectures
 
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A Cookbook
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A CookbookJavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A Cookbook
JavaOne 2016 - CON3080 - Testing Java Web Applications with Selenium: A Cookbook
 
OpenSouthCode 2016 - Accenture DevOps Platform 2016-05-07
OpenSouthCode 2016  - Accenture DevOps Platform 2016-05-07OpenSouthCode 2016  - Accenture DevOps Platform 2016-05-07
OpenSouthCode 2016 - Accenture DevOps Platform 2016-05-07
 
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost Computers
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost ComputersJavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost Computers
JavaOne 2015 - CON6489 - Smart Open Spaces Powered by Low Cost Computers
 

Recently uploaded

Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 

Recently uploaded (20)

Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 

JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of the Art in Java 9

  • 1. CODE GENERATION WITH ANNOTATION PROCESSORS: STATE OF THE ART IN JAVA 9 [CON3282]
  • 2. ABOUT THE SPEAKERS Copyright © 2017 Accenture. All rights reserved. 2 Welcome to our session! Julio Palma Vázquez Member – Málaga JUG Technology Architect – Accenture Technology Accenture Global Java Champion Accenture Delivery Center in Spain Mountain biker, AD&D player, Sid Meier’s Civilization gamer, LotR fan, … @restalion https://github.com/restalion Jorge Hidalgo Coordinator – Málaga JUG Global Java Lead – Accenture Technology Java, Architecture & DevOps Lead Accenture Delivery Center in Spain Father of two children, husband, whistle player, video gamer, sci- fi ‘junkie’, Star Wars ‘addict’, Lego brick ‘wielder’, Raspberry Pi fan, … LLAP! @_deors https://github.com/deors
  • 3. • Overview of Annotations and Annotation Processors in Java • What’s new in Java 9 APT API • Learn how to create Annotation Processors • Learn how to use Annotation Processors to generate source code • Annotation Processors and Modules in Java 9 • With lots of examples and source code CODE GENERATION IN THE JAVA COMPILER Copyright © 2017 Accenture. All rights reserved. 3 Objetives for the session
  • 4. Please raise your hands if you are familiar with Annotations, know how to use them or are even capable of writing Annotation Types POLL #1 Copyright © 2017 Accenture. All rights reserved. 4
  • 5. ANNOTATIONS IN JAVA LANGUAGE Copyright © 2017 Accenture. All rights reserved. 5
  • 6. • Annotations were first introduced in Java 5 • Add metadata capabilities to the Java language: • Build/deployment information • Configuration properties • Compiler behavior • Quality checks • Dependency injection • Persistence mapping • ... ANNOTATIONS IN THE JAVA LANGUAGE Copyright © 2017 Accenture. All rights reserved. 6 History
  • 7. • Annotations always appear before the annotated piece of code • Annotations can ‘decorate’... • Packages • Types (classes, interfaces, enums, annotation types) • Variables (class, instance and local variables) • Constructors, methods and their parameters • Generic type parameter (since Java 8) • Any type use (since Java 8) ANNOTATIONS IN THE JAVA LANGUAGE Copyright © 2017 Accenture. All rights reserved. 7 Overview
  • 8. • Can be grouped, nested, repeated... very powerful! Map<@NonNull String, @NonEmpty List<@Readonly Document>> docStore; • Annotations may be used just as a ‘marker’ @NonNull String email; • They may contain name-value pairs, and arrays @Author(name = "Albert", created = "17/09/2010", revision = 3, reviewers = {"George", "Fred"}) public class SomeClass {...} • Can be defined with default values, and then omitted when used • When it has only one attribute named value, it can be omitted, too @DatabaseTable(“MY_TABLE”) public class MyTable {...} ANNOTATIONS IN THE JAVA LANGUAGE Copyright © 2017 Accenture. All rights reserved. 8 Overview
  • 9. HOW TO CREATE ANNOTATIONS AND ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 9
  • 10. Please raise your hands if you are familiar with Annotation Processors, their capabilities or know how to write new ones POLL #2 Copyright © 2017 Accenture. All rights reserved. 10
  • 11. • A special type of interface public @interface Author { String name(); String created(); int revision() default 1; String[] reviewers() default {}; } • Only primitives, enum types, annotation types, String, Class and arrays of them are allowed as elements • May be annotated itself, i.e. to define the retention policy or targets @Retention(RetentionPolicy.SOURCE) @Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) public @interface Author {...} Can trigger custom actions at compile time managed by Annotation Processors! ANNOTATIONS Copyright © 2017 Accenture. All rights reserved. 11 Create an Annotation Type
  • 12. • Annotation Processor: – Implements javax.annotation.processing.Processor – Or extends javax.annotation.processing.AbstractProcessor • May use three annotations to configure itself: – jx.a.p.SupportedAnnotationTypes – Used to flag which Annotation Types are managed by the – jx.a.p.SupportedSourceVersion – Used to flag the latest Java source level supported by the processor – jx.a.p.SupportedOptions – Used to register custom parameters that may be passed through the • TL;DR – Implement the process() method ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 12 Annotation Processing API
  • 13. • Annotation processing happens in ‘rounds’ • In each round, a subset of sources are compiled, and then processors matching the annotations found on those sources are called • Processing ends when no more sources/annotations are pending • Processors may claim all annotation types with a wildcard • process() method has two parameters: – Set<? extends jx.lang.model.element.TypeElement> – subset of annotations being processed – jx.a.p.RoundEnvironment – access to information about the current and previous round ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 13 Annotation Processing API
  • 14. jx.a.p.ProcessingEnvironment also provides access to the Filer utility that allows for easy file creation • Files are placed in the right folder as required by the compiler settings • Folder hierarchy matching the package name JavaFileObject jfo = processingEnv.getFiler() .createSourceFile(newClassQualifiedName); Writer writer = jfo.openWriter(); ...or... JavaFileObject jfo = processingEnv.getFiler() .createClassFile(newClassQualifiedName); OutputStream os = jfo.openOutputStream(); ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 14 Creating Files
  • 15. Annotation Processors leverage the standard services mechanism • Package the processors in a Jar file • Include file META-INF/services/javax.annotation.processing.Processor • Contents are the fully qualified names of the bundled processors, one per line ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 15 How the compiler knows?
  • 16. Find more information about APT in JavaOne 2014 session: https://www.slideshare.net/deors/javaone-2014-con2013-code-generation-in-the-java-compiler-annotation-processors-do-the-hard-work https://www.youtube.com/watch?v=xswPPwYPAFM ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 16 Running Annotation Processors
  • 17. Multiple ways: • javac • Apache Maven builds • Continuous Integration builds • IDE ANNOTATION PROCESSORS Copyright © 2017 Accenture. All rights reserved. 17 Running Annotation Processors
  • 18. ADAPT LEGACY APT PROJECTS TO JAVA 9 Copyright © 2017 Accenture. All rights reserved. 18
  • 19. • Update <java.version> tag in pom.xml <java.version>1.9</java.version> • Update version of maven-compiler-plugin <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.2</version> • Upgrade the supported source version in Processor class @SupportedAnnotationTypes(“com.foo.AnnotationSample") @SupportedSourceVersion(SourceVersion.RELEASE_9) public class AnnotationSampleProcessor ADAPT LEGACY CODE Copyright © 2017 Accenture. All rights reserved. 19 Upgrading your code to Java 9
  • 20. • mvn clean install finish without errors: ANNOTATIONS API IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 20 Upgrading your code to Java 9
  • 21. DEMO ANNOTATIONS API IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 21 Executing upgraded projects
  • 22. Upgraded legacy code sample can be found in github: https://github.com/deors/deors.demos.annotations ADAPT LEGACY CODE Copyright © 2017 Accenture. All rights reserved. 22 Upgrading your code to Java 9
  • 23. WHAT’S NEW IN JAVA 9 APT API Copyright © 2017 Accenture. All rights reserved. 23
  • 24. ANNOTATIONS API IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 24 Overview • New @Generated Annotation • To add information about the annotation processor, date, and comments: @Generated(value = "com.foo.processors.SampleProcessor", date = "2017-09-28T09:43:28.170886", comments = "Sample") • Processed as a subsequent step by the annotation processor. • Two new methods in RoundEnvironment interface • Simplify processing of multiple annotations default Set<? extends Element> getElementsAnnotatedWithAny​( Set<Class<? extends Annotation>> annotations); default Set<? extends Element> getElementsAnnotatedWithAny​( TypeElement... annotations);
  • 25. ANNOTATIONS API IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 25 Overview • Changes in all methods of Filer interface. To manage files in a module, module name is prefixed to the type name and separated using a “/” character, i.e.: JavaFileObject jfo = processingEnv.getFiler().createSourceFile( "com.foo/com.foo.Bar"); JavaFileObject jfo = processingEnv.getFiler().getResource(location, "com.foo/com.foo", "Bar");
  • 26. NEW FEATURES IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 26 • It’s a best practice to include this annotation in the generated code: bw.append("@Generated(value = "" + this.getClass().getName() + "" , date = "" + LocalDateTime.now() + "", comments = "Test generator")"); Generated Annotation Comments MUST have the name of the code generator Date when the source was generated
  • 27. • During annotation processing,the Generated annotation will be found but nothing is done, it’s just informational. • Generated code: NEW FEATURES IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 27 Generated Annotation Name of the code generator Generation date Comments
  • 28. ANNOTATIONS API IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 28 Module
  • 29. ANNOTATION PROCESSORS AND MODULES IN JAVA 9 Copyright © 2017 Accenture. All rights reserved. 29
  • 30. ANNOTATION PROCESSORS AND MODULES Copyright © 2017 Accenture. All rights reserved. 30 Creating classes in modules • In previous examples we have three different jar files: • Annotation type • Annotation processor • classes that use annotations • Dependencies are managed using the regular classpath 30 annotation annotation.processor client
  • 31. ANNOTATION PROCESSORS AND MODULES Copyright © 2017 Accenture. All rights reserved. 31 Grouping classes in modules • One module per jar file. • Put a module-info.java file into each module. • Each module-info file needs to define exported packages and required modules. annotation annotation.processor client
  • 32. ANNOTATION PROCESSORS AND MODULES Copyright © 2017 Accenture. All rights reserved. 32 Creating classes in modules annotation module: module com.foo.annotation { exports com.foo.annotation; } annotation.processor module: module com.foo.annotation.processors { requires com.foo.annotation; requires java.compiler; } client module: module com.foo.client { exports com.foo.client; requires com.foo.annotation; requires java.compiler; } Why? Because we add @Generated annotation annotation annotation.processor client
  • 33. ANNOTATION PROCESSORS AND MODULES Copyright © 2017 Accenture. All rights reserved. 33 Creating classes in modules DEMO https://github.com/restalion/deors.demos.annotations/tree/jdk9
  • 34. Copyright © 2017 Accenture. All rights reserved. 34 • APT API is as powerful as in previous versions. • All your legacy code still works as expected. • You can add a couple a of new functionalities: • @Generator annotation • Simplified processing of multiple annotations • You can also manage file generation in other modules SUMMARY
  • 35. Q & A Copyright © 2017 Accenture. All rights reserved. 35 Many thanks for attending! @_deors https://github.com/deors @restalion https://github.com/restalion