Annotation Processing

annotation processing in java and annotation processing seems to be disabled for the project
GraceRogers Profile Pic
GraceRogers,Greece,Professional
Published Date:12-07-2017
Your Website URL(Optional)
Comment
Annotation Processing Angelika Langer Trainer/Consultant http://www.AngelikaLanger.comgoal • give an overview of annotation processing – what are annotations? • meta information – how are they defined? • language features since JDK 5.0 – how are they processed? • on the source code level • (on the byte code level) • (at runtime via reflection) © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 2 last update: 4/16/2008 ,12:42speaker's qualifications • independent trainer / consultant / author – teaching C++ and Java for 10+ years – curriculum of a dozen challenging courses – co-author of "Effective Java" column in JavaSpektrum – author of Java Generics FAQ online – Java champion since 2005 © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 3 last update: 4/16/2008 ,12:42agenda • annotation language features • processing annotations • case study © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 4 last update: 4/16/2008 ,12:42program annotation facility • allows developers – to define custom annotation types –to annotate fields, methods, classes, etc. with annotations corresponding to these types • allow tools to read and process the annotations – no direct effect on semantics of a program – e.g. tool can produce additional Java source files or XML documents related to the annotated program © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 5 last update: 4/16/2008 ,12:42sample usage • annotated class Copyright("2008 Vibro Systems, Ltd.") public class OscillationOverthruster ... • corresponding definition of annotation type public interface Copyright String value(); • reading an annotation via reflection String copyrightHolder = OscillationOverthruster.class. getAnnotation(Copyright.class).value(); © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 6 last update: 4/16/2008 ,12:42retention • it makes little sense to retain all annotations at run time – would increase run-time memory-footprint • annotations can have different lifetime: SOURCE: • discarded after compilation CLASS: • recorded in the class file as signature attributes • not retained until run time RUNTIME: • recorded in the class file and retained by the VM at run time • may be read reflectively © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 7 last update: 4/16/2008 ,12:42agenda • annotation language features – declaring annotation types – annotating program elements – meta annotations • processing annotations • case study © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 8 last update: 4/16/2008 ,12:42annotation type • every annotation has an annotation type – takes the form of a highly restricted interface declaration – new "keyword" interface –a default value may be specified for an annotation type member – permitted return types include primitive types, String, Class public interface RequestForEnhancement int id(); String synopsis(); String engineer() default "unassigned"; String date() default "unimplemented"; s © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 9 last update: 4/16/2008 ,12:42using annotation types RequestForEnhancement( id = 28, synopsis = "Provide time-travel functionality", engineer = "Mr. Peabody", date = "12/24/2008" ) public static void travelThroughTime(Date destination) ... • members with a default may be omitted RequestForEnhancement( id = 45, synopsis = "Add extension as per request 392" ) public static void balanceFederalBudget() throw new UnsupportedOperationException("Not implemented"); © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 10 last update: 4/16/2008 ,12:42marker annotations • annotation types can have no members –called marker annotations public interface Immutable • sample usage Immutable public class String ... © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 11 last update: 4/16/2008 ,12:42agenda • annotation language features – declaring annotation types – annotating program elements – meta annotations • processing annotations • case study © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 12 last update: 4/16/2008 ,12:42annotatable program elements • annotations may be used as modifiers in the declaration of: – package, class, interface, field, method, parameter, constructor, local variable, enum type, enum constant, annotation type public interface Copyright String value(); public interface Default Copyright("2004 Angelika Langer") public enum Color RED, BLUE, GREEN, Default NOCOLOR © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 13 last update: 4/16/2008 ,12:42more annotated types • JSR 308 (in Java 7.0) allows annotations as type qualifiers (on any use of a type) •type MapNonNull String, NonEmpty ListReadonly Document files; parameter: • bounds: class FolderF extends Existing File ... Collection? super Existing File var; • array: DocumentReadonly docs1 = new DocumentReadonly 212; DocumentReadonly docs2 = new Document2Readonly 12; © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 14 last update: 4/16/2008 ,12:42disambiguation Dimension getSize() Readonly ... • Readonly annotates the type of this Readonly Dimension getSize() ... • Target meta-annotation indicates the intent: • Readonly annotates the return type Target(ElementType.TYPE) Override public interface ReadOnly NonNull Dimension getSize() ... • NonNull annotates the return type Target(ElementType.TYPE) public interface NonNull • Override annotates the method declaration Target(ElementType.METHOD) public interface Override © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 15 last update: 4/16/2008 ,12:42agenda • annotation language features – declaring annotation types – annotating program elements – meta annotations • processing annotations • case study © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 16 last update: 4/16/2008 ,12:42meta annotations Target(ElementType) • indicates the program elements to which an annotation type can be applied • values: TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE • default: applicable to all program elements Documented • indicates that annotations are documented in javadoc Retention(RetentionPolicy) • indicates how long annotations are to be retained • values: SOURCE, CLASS, RUNTIME • default: CLASS © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 17 last update: 4/16/2008 ,12:42sample usage • self-referential meta-annotation Documented Retention(value=RUNTIME) Target(value=ANNOTATION_TYPE) public interface Retention RetentionPolicy value(); © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 18 last update: 4/16/2008 ,12:42agenda • annotation language features • processing annotations • case study © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 19 last update: 4/16/2008 ,12:42annotation processing • can happen on 3 levels – introspectors • process runtime-visible annotations of their own program elements • use reflection and need annotations with RUNTIME retention – byte code analyzers • process annotations in .class files • e.g. stub generators – source code analyzers • process annotations in Java source code • e.g. compilers, documentation generators, class browsers © Copyright 2003-2008 by Angelika Langer & Klaus Kreft. All Rights Reserved. http://www.AngelikaLanger.com 20 last update: 4/16/2008 ,12:42