- Forward


Compile-Time Processing of Annotations
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Purpose:
    • Annotations provide information/data about a program
  • Defining:
    • Annotations are defined using an interface-like syntax
Compile-Time Processing
Back SMYC Forward
  • The Process:
    • The development tool creates an instance of a Processor
    • The Processor object's init() method is called
    • The Processor object's getSupported___() methods are called
    • The Processor object's process() method is called
  • Configuring the Tool:
    • Involves configuring the process it will use to discover Processor classes
Compile-Time Processing (cont.)
Back SMYC Forward
  • One Convenient Tool:
    • The javac command has a -processor option that allows you to specify a Processor
  • Incorporation into IDEs:
    • The ProcessingEnvironment object that is passed into the init() method has an associated Messager that can be used to send output to the IDE (rather then the console or a file)
An Example
Back SMYC Forward
  • The Setting:
    • We want an annotation that helps us ensure that attributes are declated to be final
  • What's Needed:
    • An annotation
    • A Processor
An Example (cont.)
Back SMYC Forward

The Annotation

javaexamples/annotations/FinalAttributes.java
 
An Example (cont.)
Back SMYC Forward

The Processor

javaexamples/annotations/FinalProcessor.java
 
An Example (cont.)
Back SMYC Forward

Using the Annotation

@FinalAttributes public class Student { private int id; private String name; public Student(String name) { this.name = name; this.id = IDGenerator.generate(); } public String getID() { return name; } public String getName() { return name; } }
There's Always More to Learn
Back -