public class Model extends 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.
|
Elements |
elementUtils()
Returns a
Elements implementation. |
ProcessingEnvironment |
environment()
Returns a
ProcessingEnvironment implementation. |
Element |
newElementAnnotatedWith(Class<? extends Annotation> annotationType,
String... code)
Parses the supplied code, returning the
Element annotated with the given annotation. |
Element |
newElementWithMarker(String... code)
Parses the supplied code, returning the
Element marked with "--->". |
TypeElement |
newType(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.
|
TypeElement |
typeElement(Class<?> cls)
Returns the
TypeElement of cls. |
TypeElement |
typeElement(String qualifiedType)
Returns the
TypeElement of qualifiedType. |
TypeMirror |
typeMirror(Class<?> cls)
Returns a
TypeMirror for the given class (raw T, not T<?>, if T is generic). |
TypeMirror |
typeMirror(String typeSnippet,
TypeMirror... args)
Returns a
TypeMirror for the given type, substituting any provided arguments for
%1, %2, etc. |
TypeMirror |
typeMirror(com.google.common.reflect.TypeToken<?> type)
Returns a
TypeMirror for the given type. |
Types |
typeUtils()
Returns a
Types implementation. |
public static Model create()
protected void start()
public ProcessingEnvironment environment()
ProcessingEnvironment implementation.public TypeMirror typeMirror(Class<?> cls)
TypeMirror for the given class (raw T, not T<?>, if T is generic).public TypeMirror typeMirror(com.google.common.reflect.TypeToken<?> type)
TypeMirror for the given type.public TypeMirror typeMirror(String typeSnippet, 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 TypeElement typeElement(Class<?> cls)
TypeElement of cls.public TypeElement typeElement(String qualifiedType)
TypeElement of qualifiedType.public TypeElement newType(String... code)
TypeElement.public Element newElementWithMarker(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 Element newElementAnnotatedWith(Class<? extends Annotation> annotationType, String... code)
Element annotated with the given annotation.
Element element = model.newElementAnnotatedWith(MyAnnotation.class
"interface MyType {",
" void myMethod(@MyAnnotation int arg);",
"}")
public void destroy()
Copyright © 2015 inferred.org. All rights reserved.