The following topics are covered
The apt tool and its associated API contained in the package com.sun.mirror have been deprecated since JDK 7 and are planned to be removed in the next major JDK release. Use the options available in the javac tool and the APIs contained in the packages javax.annotation.processing and javax.lang.model to process annotations.
The following tables summarize apt API replacements:
apt type |
Standard Replacement |
|---|---|
AnnotationProcessor |
javax.annotation.processing.Processor |
AnnotationProcessorEnvironment |
javax.annotation.processing.ProcessingEnvironment |
AnnotationProcessorFactory |
javax.annotation.processing.Processor |
AnnotationProcessorListener |
No analog. |
AnnotationProcessors |
No analog. |
Filer |
javax.annotation.processing.Filer |
Filer.Location |
javax.tools.StandardLocation |
Messager |
javax.annotation.processing.Messager |
RoundCompleteEvent |
No analog. |
RoundCompleteListener |
No analog. |
RoundState |
javax.annotation.processing.RoundEnvironment |
apt type |
Standard Replacement |
|---|---|
AnnotationMirror |
javax.lang.model.element.AnnotationMirror |
AnnotationTypeDeclaration |
javax.lang.model.element.TypeElement |
AnnotationTypeElementDeclaration |
javax.lang.model.element.ExecutableElement
|
AnnotationValue
|
javax.lang.model.element.AnnotationValue
|
ClassDeclaration
|
javax.lang.model.element.TypeElement
|
ConstructorDeclaration
|
javax.lang.model.element.ExecutableElement
|
Declaration
|
javax.lang.model.element.Element
|
EnumConstantDeclaration
|
javax.lang.model.element.VariableElement
|
EnumDeclaration
|
javax.lang.model.element.TypeElement
|
ExecutableDeclaration
|
javax.lang.model.element.ExecutableElement
|
FieldDeclaration
|
javax.lang.model.element.VariableElement
|
InterfaceDeclaration
|
javax.lang.model.element.TypeElement
|
MemberDeclaration
|
javax.lang.model.element.Element
|
MethodDeclaration
|
javax.lang.model.element.ExecutableElement
|
Modifier
|
javax.lang.model.element.Modifier
|
PackageDeclaration
|
javax.lang.model.element.PackageElement
|
ParameterDeclaration
|
javax.lang.model.element.VariableElement
|
TypeDeclaration
|
javax.lang.model.element.TypeElement
|
TypeParameterDeclaration
|
javax.lang.model.element.TypeParameterElement
|
apt type |
Standard Replacement |
|---|---|
AnnotationType
|
javax.lang.model.type.DeclaredType
|
ArrayType
|
javax.lang.model.type.ArrayType
|
ClassType
|
javax.lang.model.type.DeclaredType
|
DeclaredType
|
javax.lang.model.type.DeclaredType
|
EnumType
|
javax.lang.model.type.DeclaredType
|
InterfaceType
|
javax.lang.model.type.DeclaredType
|
MirroredTypeException
|
javax.lang.model.type.MirroredTypeException
|
MirroredTypesException
|
javax.lang.model.type.MirroredTypesException
|
PrimitiveType
|
javax.lang.model.type.PrimitiveType
|
PrimitiveType.Kind
|
javax.lang.model.type.TypeKind
|
ReferenceType
|
javax.lang.model.type.ReferenceType
|
TypeMirror
|
javax.lang.model.type.TypeMirror
|
TypeVariable
|
javax.lang.model.type.TypeVariable
|
VoidType
|
javax.lang.model.type.NoType
|
WildcardType
|
javax.lang.model.type.WildcardType
|
apt type |
Standard Replacement |
|---|---|
DeclarationFilter
|
javax.lang.model.util.ElementFilter
|
DeclarationScanner
|
javax.lang.model.util.ElementScanner6
|
DeclarationVisitor
|
javax.lang.model.element.ElementVisitor
|
DeclarationVisitors
|
No replacement. |
Declarations
|
javax.lang.model.util.Elements
|
SimpleDeclarationVisitor
|
javax.lang.model.util.SimpleElementVisitor6
|
SimpleTypeVisitor
|
javax.lang.model.util.SimpleTypeVisitor6
|
SourceOrderDeclScanner
|
javax.lang.model.util.SimpleElementVisitor6
|
SourcePosition
|
No replacement. |
TypeVisitor
|
javax.lang.model.element.TypeVisitor
|
Types
|
javax.lang.model.util.Types
|
apt, annotation processing
tool, finds and executes annotation processors based on the
annotations present in the set of specified source files being
examined. The annotation processors use a set of reflective APIs
and supporting infrastructure to perform their processing of
program annotations
(JSR 175).
The apt reflective APIs provide a build-time,
source-based, read-only view of program structure. These reflective
APIs are designed to cleanly model the
JavaTM programming language's type system
after the addition of generics (JSR 14). First,
apt runs annotation processors that can produce new
source code and other files. Next, apt can cause
compilation of both original and generated source files, thus
easing the development cycle.
Compared to using a doclet to generate the derived files based on annotations, apt
A processor instance is returned by its corresponding factory -- an AnnotationProcessorFactory. The apt tool calls the factory's getProcessorFor method to get hold of the processor. During this call, the tool provides an AnnotationProcessorEnvironment. In the environment the processor will find everything it needs to get started, including references to the program structure on which it is operating, and the means to communicate and cooperate with the apt tool by creating new files and passing on warning and error messages.
There are two ways a factory can be found; the factory to use can be specified via the "-factory" command line option or the factory can be located during the apt discovery procedure. Using the "-factory" option is the simplest way to run a single known factory; this option may also be used when a factory needs more control over how it is run. To locate the factories on a particular path, the discovery procedure retrieves from jar files META-INF/services information in the format described below.
To create and use an annotation processor using the "-factory" option:
import com.sun.mirror.apt.*;
import com.sun.mirror.declaration.*;
import com.sun.mirror.type.*;
import com.sun.mirror.util.*;
import java.util.Collection;
import java.util.Set;
import java.util.Arrays;
import static java.util.Collections.*;
import static com.sun.mirror.util.DeclarationVisitors.*;
/*
* This class is used to run an annotation processor that lists class
* names. The functionality of the processor is analogous to the
* ListClass doclet in the Doclet Overview.
*/
public class ListClassApf implements AnnotationProcessorFactory {
// Process any set of annotations
private static final Collection<String> supportedAnnotations
= unmodifiableCollection(Arrays.asList("*"));
// No supported options
private static final Collection<String> supportedOptions = emptySet();
public Collection<String> supportedAnnotationTypes() {
return supportedAnnotations;
}
public Collection<String> supportedOptions() {
return supportedOptions;
}
public AnnotationProcessor getProcessorFor(
Set<AnnotationTypeDeclaration> atds,
AnnotationProcessorEnvironment env) {
return new ListClassAp(env);
}
private static class ListClassAp implements AnnotationProcessor {
private final AnnotationProcessorEnvironment env;
ListClassAp(AnnotationProcessorEnvironment env) {
this.env = env;
}
public void process() {
for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations())
typeDecl.accept(getDeclarationScanner(new ListClassVisitor(),
NO_OP));
}
private static class ListClassVisitor extends SimpleDeclarationVisitor {
public void visitClassDeclaration(ClassDeclaration d) {
System.out.println(d.getQualifiedName());
}
}
}
}
A number of new language and library features are used in the
sample processor. First, static imports are used so that
the simple name of various utility methods can be used; for
example
"unmodifiableCollection"
instead of
"Collections.unmodifiableCollection".
Second, generic collections are used throughout. The
Arrays.asList method is now a var-args method so it can
accept a comma separated list of strings and create a list with the
desired elements. The Collections.emptySet method is a
generic method and can be used to create a type-safe empty set. The
for loop in the process method is an enhanced for
loop that can iterate over collections.
The apt tool presents the factory with a set of annotations for the factory to process. Based on the set of annotations and the annotation processor environment, the factory returns a single annotation processor. What if the factory wants to return multiple annotation processors? The factory can use com.sun.mirror.apt.AnnotationProcessors.getCompositeAnnotationProcessor to combine and sequence the operation of multiple annotation processors.
The apt specific options are:
If a factory class is used by more than one round of annotation processing, the factory class is loaded once and the factory's getProcessorFor method will be called once per round. This allows a factory to store static state across rounds.
If the -factory option is used, the named factory is the only one queried.
The mirror API represents source code constructs principally through the Declaration interface and its hierarchy of subinterfaces, in the package com.sun.mirror.declaration. A Declaration represents a program element such as a package, class, or method, and typically corresponds one-to-one with a particular fragment of source code. Declarations are the structures that may be annotated.
Types are represented by the TypeMirror interface and its hierarchy of subinterfaces in the package com.sun.mirror.type. Types include primitive types, class and interface types, array types, type variables, and wildcard types.
The API is careful to distinguish between declarations and types. This is most significant for generic types, where a single declaration can define a whole family of types. For example, the declaration of the class java.util.Set corresponds to
TypeMirrors are used to model the return types, parameter types, etc., in source code. A TypeMirror for a reference type provides a mapping from a type to corresponding declaration; for example from the type mirror for java.util.Set<String> to the declaration for java.util.Set.