public final class GeneratorUtilities extends Object
Modifier and Type | Method and Description |
---|---|
CompilationUnitTree |
addImports(CompilationUnitTree cut,
Set<? extends Element> toImport)
Adds import statements for given elements to a compilation unit.
|
CompilationUnitTree |
appendToAnnotationValue(CompilationUnitTree compilationUnit,
TypeElement annotation,
String attributeName,
ExpressionTree... attributeValuesToAdd)
Ensures that the given
compilationUnit contains annotation of the given type,
which has attribute name attributeName , which contains values attributeValuesToAdd . |
ModifiersTree |
appendToAnnotationValue(ModifiersTree modifiers,
TypeElement annotation,
String attributeName,
ExpressionTree... attributeValuesToAdd)
Ensures that the given
modifiers contains annotation of the given type,
which has attribute name attributeName , which contains values attributeValuesToAdd . |
void |
copyComments(Tree source,
Tree target,
boolean preceding)
Copy comments from source tree to target tree.
|
MethodTree |
createAbstractMethodImplementation(TypeElement clazz,
ExecutableElement method)
Creates an implementation of an abstract method within a class.
|
List<? extends MethodTree> |
createAbstractMethodImplementations(TypeElement clazz,
Iterable<? extends ExecutableElement> methods)
Creates implementations of abstract methods within a class.
|
List<? extends MethodTree> |
createAllAbstractMethodImplementations(TypeElement clazz)
Creates implementations of the all abstract methods within a class.
|
MethodTree |
createConstructor(ClassTree clazz,
Iterable<? extends VariableTree> fields)
Creates a class constructor.
|
MethodTree |
createConstructor(TypeElement clazz,
Iterable<? extends VariableElement> fields,
ExecutableElement constructor)
Creates a class constructor.
|
MethodTree |
createDefaultConstructor(TypeElement clazz,
Iterable<? extends VariableElement> fields,
ExecutableElement constructor)
Creates a class default constructor.
|
BlockTree |
createDefaultLambdaBody(LambdaExpressionTree lambda,
ExecutableElement method)
Creates a default lambda body.
|
ExpressionTree |
createDefaultLambdaExpression(LambdaExpressionTree lambda,
ExecutableElement method)
Creates a default lambda expression.
|
CompilationUnitTree |
createFromTemplate(FileObject sourceRoot,
String path,
ElementKind kind)
Create a new CompilationUnitTree from a template.
|
MethodTree |
createGetter(TypeElement clazz,
VariableElement field)
Creates a getter method for a field.
|
MethodTree |
createGetter(VariableTree field)
Creates a getter method for a field.
|
MethodTree |
createMethod(DeclaredType asMemberOf,
ExecutableElement method)
Create a new method tree for the given method element.
|
MethodTree |
createOverridingMethod(TypeElement clazz,
ExecutableElement method)
Creates an overriding method within a class.
|
List<? extends MethodTree> |
createOverridingMethods(TypeElement clazz,
Iterable<? extends ExecutableElement> methods)
Creates overriding methods within a class.
|
MethodTree |
createSetter(ClassTree clazz,
VariableTree field)
Creates a setter method for a field.
|
MethodTree |
createSetter(TypeElement clazz,
VariableElement field)
Creates a setter method for a field.
|
static GeneratorUtilities |
get(WorkingCopy copy)
Returns the instance of this class
|
<T extends Tree> |
importComments(T original,
CompilationUnitTree cut) |
<T extends Tree> |
importFQNs(T original)
Take a tree as a parameter, replace resolved fully qualified names with
simple names and add imports to compilation unit during task commit.
|
ClassTree |
insertClassMember(ClassTree clazz,
Tree member)
Inserts a member to a class.
|
ClassTree |
insertClassMember(ClassTree clazz,
Tree member,
int offset)
Inserts a member to a class.
|
ClassTree |
insertClassMembers(ClassTree clazz,
Iterable<? extends Tree> members)
Inserts members to a class.
|
ClassTree |
insertClassMembers(ClassTree clazz,
List<? extends Tree> members,
int offset)
Inserts members to a class.
|
public static GeneratorUtilities get(WorkingCopy copy)
copy
- GeneratorUtilities
instancepublic CompilationUnitTree createFromTemplate(FileObject sourceRoot, String path, ElementKind kind) throws IOException
sourceRoot
- a source root under which the new file is createdpath
- a relative path to file separated by '/'kind
- the kind of Element to use for the template, can be null or
CLASS, INTERFACE, ANNOTATION_TYPE, ENUM, PACKAGEIOException
- when an exception occurs while creating the templatepublic ClassTree insertClassMember(ClassTree clazz, Tree member)
CodeStyle
it finds the proper place for the member and calls TreeMaker.insertClassMember(com.sun.source.tree.ClassTree, int, com.sun.source.tree.Tree)
clazz
- the class to insert the member tomember
- the member to addpublic ClassTree insertClassMembers(ClassTree clazz, List<? extends Tree> members, int offset)
CodeStyle
it finds the proper place for each of the members and calls TreeMaker.insertClassMember(com.sun.source.tree.ClassTree, int, com.sun.source.tree.Tree)
clazz
- the class to insert the members tomembers
- the members to insertoffset
- the caret location to use for CodeStyle.InsertionPoint.CARET_LOCATION
public ClassTree insertClassMember(ClassTree clazz, Tree member, int offset)
CodeStyle
it finds the proper place for the member and calls TreeMaker.insertClassMember(com.sun.source.tree.ClassTree, int, com.sun.source.tree.Tree)
clazz
- the class to insert the member tomember
- the member to addoffset
- the caret location to use for CodeStyle.InsertionPoint.CARET_LOCATION
public ClassTree insertClassMembers(ClassTree clazz, Iterable<? extends Tree> members)
CodeStyle
it finds the proper place for each of the members and calls TreeMaker.insertClassMember(com.sun.source.tree.ClassTree, int, com.sun.source.tree.Tree)
clazz
- the class to insert the members tomembers
- the members to insertpublic List<? extends MethodTree> createAllAbstractMethodImplementations(TypeElement clazz)
clazz
- the class to create the implementations withinpublic List<? extends MethodTree> createAbstractMethodImplementations(TypeElement clazz, Iterable<? extends ExecutableElement> methods)
clazz
- the class to create the implementations withinmethods
- the abstract methods to implementpublic MethodTree createAbstractMethodImplementation(TypeElement clazz, ExecutableElement method)
clazz
- the class to create the implementation withinmethod
- the abstract method to implementpublic List<? extends MethodTree> createOverridingMethods(TypeElement clazz, Iterable<? extends ExecutableElement> methods)
clazz
- the class to create the methods withinmethods
- the methods to overridepublic MethodTree createOverridingMethod(TypeElement clazz, ExecutableElement method)
clazz
- the class to create the method withinmethod
- the method to overridepublic MethodTree createMethod(DeclaredType asMemberOf, ExecutableElement method)
asMemberOf
type
(see also Types.asMemberOf(javax.lang.model.type.DeclaredType,javax.lang.model.element.Element)
).
The new method will have an empty body.asMemberOf
- create the method as if it were member of this typemethod
- method to createTypes.asMemberOf(javax.lang.model.type.DeclaredType,javax.lang.model.element.Element)
public MethodTree createConstructor(TypeElement clazz, Iterable<? extends VariableElement> fields, ExecutableElement constructor)
clazz
- the class to create the constructor forfields
- fields to be initialized by the constructorconstructor
- inherited constructor to be calledpublic MethodTree createDefaultConstructor(TypeElement clazz, Iterable<? extends VariableElement> fields, ExecutableElement constructor)
clazz
- the class to create the constructor forfields
- fields to be initialized by the constructorconstructor
- inherited constructor to be calledpublic MethodTree createConstructor(ClassTree clazz, Iterable<? extends VariableTree> fields)
clazz
- the class to create the constructor forfields
- fields to be initialized by the constructorpublic MethodTree createGetter(TypeElement clazz, VariableElement field)
clazz
- the class to create the getter withinfield
- field to create getter forpublic MethodTree createGetter(VariableTree field)
field
- field to create getter forpublic MethodTree createSetter(TypeElement clazz, VariableElement field)
clazz
- the class to create the setter withinfield
- field to create setter forpublic MethodTree createSetter(ClassTree clazz, VariableTree field)
clazz
- the class to create the setter withinfield
- field to create setter forpublic BlockTree createDefaultLambdaBody(LambdaExpressionTree lambda, ExecutableElement method)
lambda
- a lambda to generate body formethod
- a method of a functional interface to be implemented by the lambda expressionpublic ExpressionTree createDefaultLambdaExpression(LambdaExpressionTree lambda, ExecutableElement method)
lambda
- a lambda to generate the expression formethod
- a method of a functional interface to be implemented by the lambda expressionpublic CompilationUnitTree addImports(CompilationUnitTree cut, Set<? extends Element> toImport)
CodeStyle
.
Use TreeMaker.QualIdent, TreeMaker.Type or GeneratorUtilities.importFQNs instead of this method if possible. These methods will correctly resolve imports according to the user's preferences.
cut
- the compilation unit to insert imports totoImport
- the elements to import.public <T extends Tree> T importFQNs(T original)
original
- resolved FQNs in the tree will be importedpublic <T extends Tree> T importComments(T original, CompilationUnitTree cut)
public void copyComments(Tree source, Tree target, boolean preceding)
source
- tree to copy comments fromtarget
- tree to copy comments topreceding
- true iff preceding comments should be copiedpublic ModifiersTree appendToAnnotationValue(ModifiersTree modifiers, TypeElement annotation, String attributeName, ExpressionTree... attributeValuesToAdd)
modifiers
contains annotation of the given type,
which has attribute name attributeName
, which contains values attributeValuesToAdd
.
The annotation or the attribute will be added as needed, as will be the attribute value
converted from a single value into an array.
The typical trees passed as attributeValuesToAdd
are:
attribute type | expected tree type |
---|---|
primitive type | LiteralTree created by TreeMaker.Literal(java.lang.Object) |
java.lang.String |
LiteralTree created by TreeMaker.Literal(java.lang.Object) |
java.lang.Class |
MemberSelectTree created by TreeMaker.MemberSelect(com.sun.source.tree.ExpressionTree, java.lang.CharSequence) ,
with identifier class and expression created by TreeMaker.QualIdent(javax.lang.model.element.Element) |
enum constant | MemberSelectTree , with identifier representing the enum constant
and expression created by TreeMaker.QualIdent(javax.lang.model.element.Element) |
annotation type | AnnotationTree created by TreeMaker.Annotation(com.sun.source.tree.Tree, java.util.List) |
array (of a supported type) | NewArrayTree created by TreeMaker.NewArray(com.sun.source.tree.Tree, java.util.List, java.util.List) ,
where elemtype is null , dimensions is Collections.<ExpressionTree>emptyList() ,
initializers should contain the elements that should appear in the array |
modifiers
- into which the values should be addedannotation
- the annotation type that should be added or augmentedattributeName
- the attribute that should be added or augmentedattributeValuesToAdd
- values that should be added into the given attribute of the given annotationmodifiers
augmented in such a way that it contains the given annotation, with the given valuespublic CompilationUnitTree appendToAnnotationValue(CompilationUnitTree compilationUnit, TypeElement annotation, String attributeName, ExpressionTree... attributeValuesToAdd)
compilationUnit
contains annotation of the given type,
which has attribute name attributeName
, which contains values attributeValuesToAdd
.
The annotation or the attribute will be added as needed, as will be the attribute value
converted from a single value into an array. This method is intended to be called on
CompilationUnitTree
from package-info.java
.
The typical trees passed as attributeValuesToAdd
are:
attribute type | expected tree type |
---|---|
primitive type | LiteralTree created by TreeMaker.Literal(java.lang.Object) |
java.lang.String |
LiteralTree created by TreeMaker.Literal(java.lang.Object) |
java.lang.Class |
MemberSelectTree created by TreeMaker.MemberSelect(com.sun.source.tree.ExpressionTree, java.lang.CharSequence) ,
with identifier class and expression created by TreeMaker.QualIdent(javax.lang.model.element.Element) |
enum constant | MemberSelectTree , with identifier representing the enum constant
and expression created by TreeMaker.QualIdent(javax.lang.model.element.Element) |
annotation type | AnnotationTree created by TreeMaker.Annotation(com.sun.source.tree.Tree, java.util.List) |
array (of a supported type) | NewArrayTree created by TreeMaker.NewArray(com.sun.source.tree.Tree, java.util.List, java.util.List) ,
where elemtype is null , dimensions is Collections.<ExpressionTree>emptyList() ,
initializers should contain the elements that should appear in the array |
compilationUnit
- into which the values should be addedannotation
- the annotation type that should be added or augmentedattributeName
- the attribute that should be added or augmentedattributeValuesToAdd
- values that should be added into the given attribute of the given annotationcompilationUnit
augmented in such a way that it contains the given annotation, with the given values