public class Model
extends java.lang.Object
The standard javax.lang.model.util objects, and TypeMirror instances for existing
classes, are readily available. More complex elements can be constructed from
Java source code snippets, allowing top-level types and even code with errors in to be contained
within a single test method.
Modelmodel =create(); TypeMirror intType = model.typeMirror(java.lang.Class<?>)(int.class); TypeElement myType = model.newType(java.lang.String...)( "package my.test.package;", "public class MyType {", " public void aMethod(int anArg);", "}"); ... model.destroy();
To save walking the hierarchy of elements to find an inner class, or other element, you can
grab any annotatable element with newElementWithMarker(java.lang.String...) (which uses ---> as an
easily-spotted element identifier) or newElementAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation>, java.lang.String...) (which uses a
user-supplied annotation as the element identifier):
VariableElement anArg = (VariableElement) model.newElementWithMarker(java.lang.String...)( "package my.test.package;", "public class MyType2 {", " public void aMethod(---> int anArg);", "}"); ExecutableElement aMethod = (ExecutableElement) model.newElementAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation>, java.lang.String...)( Deprecated.class, "package my.test.package;", "public class MyType3 {", " @Deprecated public void aMethod(int anArg);", "}");
| Constructor and Description |
|---|
Model() |
| Modifier and Type | Method and Description |
|---|---|
static Model |
create() |
void |
destroy()
Gracefully shuts down the compiler thread.
|
javax.lang.model.util.Elements |
elementUtils()
Returns a
Elements implementation. |
javax.annotation.processing.ProcessingEnvironment |
environment()
Returns a
ProcessingEnvironment implementation. |
javax.lang.model.element.Element |
newElementAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.String... code)
Parses the supplied code, returning the
Element annotated with the given annotation. |
javax.lang.model.element.Element |
newElementWithMarker(java.lang.String... code)
Parses the supplied code, returning the
Element marked with --->. |
javax.lang.model.element.TypeElement |
newType(java.lang.String... code)
Parses the supplied type definition, returning its
TypeElement. |
protected void |
start()
Starts up the compiler thread and waits for it to return the processing environment.
|
javax.lang.model.element.TypeElement |
typeElement(java.lang.Class<?> cls)
Returns the
TypeElement of cls. |
javax.lang.model.element.TypeElement |
typeElement(java.lang.String qualifiedType)
Returns the
TypeElement of qualifiedType. |
javax.lang.model.type.TypeMirror |
typeMirror(java.lang.Class<?> cls)
Returns a
TypeMirror for the given class (raw T, not T<?>, if T is generic). |
javax.lang.model.type.TypeMirror |
typeMirror(java.lang.String typeSnippet,
javax.lang.model.type.TypeMirror... args)
Returns a
TypeMirror for the given type, substituting any provided arguments for
%1, %2, etc. |
javax.lang.model.type.TypeMirror |
typeMirror(com.google.common.reflect.TypeToken<?> type)
Returns a
TypeMirror for the given type. |
javax.lang.model.util.Types |
typeUtils()
Returns a
Types implementation. |
public static Model create()
protected void start()
public javax.lang.model.util.Types typeUtils()
Types implementation.public javax.lang.model.util.Elements elementUtils()
Elements implementation.public javax.annotation.processing.ProcessingEnvironment environment()
ProcessingEnvironment implementation.public javax.lang.model.type.TypeMirror typeMirror(java.lang.Class<?> cls)
TypeMirror for the given class (raw T, not T<?>, if T is generic).public javax.lang.model.type.TypeMirror typeMirror(com.google.common.reflect.TypeToken<?> type)
TypeMirror for the given type.public javax.lang.model.type.TypeMirror typeMirror(java.lang.String typeSnippet,
javax.lang.model.type.TypeMirror... args)
TypeMirror for the given type, substituting any provided arguments for
%1, %2, etc.
e.g. typeMirror("java.util.List<%1>", typeMirror(String.class)) will
return the same thing as typeMirror("java.util.List<java.lang.String>")
typeSnippet - the type, represented as a snippet of Java code, e.g.
"java.lang.String", "java.util.Map<%1, %2>"args - existing TypeMirror instances to be substituted into the typepublic javax.lang.model.element.TypeElement typeElement(java.lang.Class<?> cls)
TypeElement of cls.public javax.lang.model.element.TypeElement typeElement(java.lang.String qualifiedType)
TypeElement of qualifiedType.public javax.lang.model.element.TypeElement newType(java.lang.String... code)
TypeElement.public javax.lang.model.element.Element newElementWithMarker(java.lang.String... code)
Element marked with --->. (Only
elements that can be annotated in Java can be found this way; the marker is substituted with
an annotation internally.)
Element element = model.newElementWithMarker(
"interface MyType {",
" void myMethod(---> int arg);",
"}")
public javax.lang.model.element.Element newElementAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
java.lang.String... code)
Element annotated with the given annotation.
Element element = model.newElementAnnotatedWith(MyAnnotation.class
"interface MyType {",
" void myMethod(@MyAnnotation int arg);",
"}")
public void destroy()