Lombok - v0.11.4

lombok.javac.handlers
Class JavacHandlerUtil

java.lang.Object
  extended by lombok.javac.handlers.JavacHandlerUtil

public class JavacHandlerUtil
extends Object

Container for static utility methods useful to handlers written for javac.


Nested Class Summary
static class JavacHandlerUtil.FieldAccess
           
static class JavacHandlerUtil.MemberExistsResult
          Serves as return value for the methods that check for the existence of fields and methods.
 
Method Summary
static boolean annotationTypeMatches(Class<? extends Annotation> type, JavacNode node)
          Checks if the Annotation AST Node provided is likely to be an instance of the provided annotation type.
static com.sun.tools.javac.tree.JCTree.JCExpression chainDots(JavacNode node, int pos, String... elems)
          In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node.
static com.sun.tools.javac.tree.JCTree.JCExpression chainDots(JavacNode node, String... elems)
          In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node.
static com.sun.tools.javac.tree.JCTree.JCExpression chainDotsString(JavacNode node, String elems)
          In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node.
static com.sun.tools.javac.tree.JCTree.JCExpression cloneSelfType(JavacNode field)
           
static com.sun.tools.javac.tree.JCTree.JCExpression cloneType(com.sun.tools.javac.tree.TreeMaker maker, com.sun.tools.javac.tree.JCTree.JCExpression in, com.sun.tools.javac.tree.JCTree source)
          Creates a full clone of a given javac AST type node.
static JavacHandlerUtil.MemberExistsResult constructorExists(JavacNode node)
          Checks if there is a (non-default) constructor.
static
<A extends Annotation>
AnnotationValues<A>
createAnnotation(Class<A> type, JavacNode node)
          Creates an instance of AnnotationValues for the provided AST Node.
static com.sun.tools.javac.util.List<Integer> createListOfNonExistentFields(com.sun.tools.javac.util.List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient)
          Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
static void deleteAnnotationIfNeccessary(JavacNode annotation, Class<? extends Annotation> annotationType)
          Removes the annotation from javac's AST (it remains in lombok's AST), then removes any import statement that imports this exact annotation (not star imports).
static void deleteImportFromCompilationUnit(JavacNode node, String name)
           
static JavacHandlerUtil.MemberExistsResult fieldExists(String fieldName, JavacNode node)
          Checks if there is a field with the provided name.
static com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree.JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern)
          Searches the given field node for annotations and returns each one that matches the provided regular expression pattern.
static com.sun.tools.javac.tree.JCTree.JCStatement generateNullCheck(com.sun.tools.javac.tree.TreeMaker treeMaker, JavacNode variable)
          Generates a new statement that checks if the given variable is null, and if so, throws a NullPointerException with the variable name as message.
static AnnotationValues<Accessors> getAccessorsForField(JavacNode field)
           
static com.sun.tools.javac.tree.JCTree getGeneratedBy(com.sun.tools.javac.tree.JCTree node)
           
static boolean hasAnnotation(Class<? extends Annotation> type, JavacNode node)
           
static boolean hasAnnotationAndDeleteIfNeccessary(Class<? extends Annotation> type, JavacNode node)
           
static void injectField(JavacNode typeNode, com.sun.tools.javac.tree.JCTree.JCVariableDecl field)
          Adds the given new field declaration to the provided type AST Node.
static void injectFieldSuppressWarnings(JavacNode typeNode, com.sun.tools.javac.tree.JCTree.JCVariableDecl field)
          Adds the given new field declaration to the provided type AST Node.
static void injectMethod(JavacNode typeNode, com.sun.tools.javac.tree.JCTree.JCMethodDecl method)
          Adds the given new method declaration to the provided type AST Node.
static boolean inNetbeansEditor(JavacNode node)
          Contributed by Jan Lahoda; many lombok transformations should not be run (or a lite version should be run) when the netbeans editor is running javac on the open source file to find inline errors and such.
static boolean isFieldDeprecated(JavacNode field)
          Returns if a field is marked deprecated, either by @Deprecated or in javadoc
static boolean isGenerated(com.sun.tools.javac.tree.JCTree node)
           
static JavacHandlerUtil.MemberExistsResult methodExists(String methodName, JavacNode node, boolean caseSensitive, int params)
          Checks if there is a method with the provided name.
static JavacHandlerUtil.MemberExistsResult methodExists(String methodName, JavacNode node, int params)
           
static
<T extends com.sun.tools.javac.tree.JCTree>
T
recursiveSetGeneratedBy(T node, com.sun.tools.javac.tree.JCTree source)
           
static
<T extends com.sun.tools.javac.tree.JCTree>
T
setGeneratedBy(T node, com.sun.tools.javac.tree.JCTree source)
           
static boolean shouldReturnThis(JavacNode field)
          When generating a setter, the setter either returns void (beanspec) or Self (fluent).
static List<String> toAllGetterNames(JavacNode field)
          Translates the given field into all possible getter names.
static List<String> toAllSetterNames(JavacNode field)
          Translates the given field into all possible setter names.
static List<String> toAllWitherNames(JavacNode field)
          Translates the given field into all possible wither names.
static String toGetterName(JavacNode field)
           
static int toJavacModifier(AccessLevel accessLevel)
          Turns an AccessLevel instance into the flag bit used by javac.
static String toSetterName(JavacNode field)
           
static String toWitherName(JavacNode field)
           
static boolean typeMatches(Class<?> type, JavacNode node, com.sun.tools.javac.tree.JCTree typeNode)
          Checks if the given TypeReference node is likely to be a reference to the provided class.
static JavacNode upToTypeNode(JavacNode node)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

inNetbeansEditor

public static boolean inNetbeansEditor(JavacNode node)
Contributed by Jan Lahoda; many lombok transformations should not be run (or a lite version should be run) when the netbeans editor is running javac on the open source file to find inline errors and such. As class files are compiled separately this does not affect actual runtime behaviour or file output of the netbeans IDE.


getGeneratedBy

public static com.sun.tools.javac.tree.JCTree getGeneratedBy(com.sun.tools.javac.tree.JCTree node)

isGenerated

public static boolean isGenerated(com.sun.tools.javac.tree.JCTree node)

recursiveSetGeneratedBy

public static <T extends com.sun.tools.javac.tree.JCTree> T recursiveSetGeneratedBy(T node,
                                                                                    com.sun.tools.javac.tree.JCTree source)

setGeneratedBy

public static <T extends com.sun.tools.javac.tree.JCTree> T setGeneratedBy(T node,
                                                                           com.sun.tools.javac.tree.JCTree source)

hasAnnotation

public static boolean hasAnnotation(Class<? extends Annotation> type,
                                    JavacNode node)

hasAnnotationAndDeleteIfNeccessary

public static boolean hasAnnotationAndDeleteIfNeccessary(Class<? extends Annotation> type,
                                                         JavacNode node)

annotationTypeMatches

public static boolean annotationTypeMatches(Class<? extends Annotation> type,
                                            JavacNode node)
Checks if the Annotation AST Node provided is likely to be an instance of the provided annotation type.

Parameters:
type - An actual annotation type, such as lombok.Getter.class.
node - A Lombok AST node representing an annotation in source code.

typeMatches

public static boolean typeMatches(Class<?> type,
                                  JavacNode node,
                                  com.sun.tools.javac.tree.JCTree typeNode)
Checks if the given TypeReference node is likely to be a reference to the provided class.

Parameters:
type - An actual type. This method checks if typeNode is likely to be a reference to this type.
node - A Lombok AST node. Any node in the appropriate compilation unit will do (used to get access to import statements).
typeNode - A type reference to check.

isFieldDeprecated

public static boolean isFieldDeprecated(JavacNode field)
Returns if a field is marked deprecated, either by @Deprecated or in javadoc

Parameters:
field - the field to check
Returns:
true if a field is marked deprecated, either by @Deprecated or in javadoc, otherwise false

createAnnotation

public static <A extends Annotation> AnnotationValues<A> createAnnotation(Class<A> type,
                                                                          JavacNode node)
Creates an instance of AnnotationValues for the provided AST Node.

Parameters:
type - An annotation class type, such as lombok.Getter.class.
node - A Lombok AST node representing an annotation in source code.

deleteAnnotationIfNeccessary

public static void deleteAnnotationIfNeccessary(JavacNode annotation,
                                                Class<? extends Annotation> annotationType)
Removes the annotation from javac's AST (it remains in lombok's AST), then removes any import statement that imports this exact annotation (not star imports). Only does this if the DeleteLombokAnnotations class is in the context.


deleteImportFromCompilationUnit

public static void deleteImportFromCompilationUnit(JavacNode node,
                                                   String name)

toAllGetterNames

public static List<String> toAllGetterNames(JavacNode field)
Translates the given field into all possible getter names. Convenient wrapper around TransformationsUtil.toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean).


toGetterName

public static String toGetterName(JavacNode field)
Returns:
the likely getter name for the stated field. (e.g. private boolean foo; to isFoo). Convenient wrapper around TransformationsUtil.toGetterName(lombok.core.AnnotationValues, CharSequence, boolean).

toAllSetterNames

public static List<String> toAllSetterNames(JavacNode field)
Translates the given field into all possible setter names. Convenient wrapper around TransformationsUtil.toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean).


toSetterName

public static String toSetterName(JavacNode field)
Returns:
the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). Convenient wrapper around TransformationsUtil.toSetterName(lombok.core.AnnotationValues, CharSequence, boolean).

toAllWitherNames

public static List<String> toAllWitherNames(JavacNode field)
Translates the given field into all possible wither names. Convenient wrapper around TransformationsUtil.toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean).


toWitherName

public static String toWitherName(JavacNode field)
Returns:
the likely wither name for the stated field. (e.g. private boolean foo; to withFoo). Convenient wrapper around TransformationsUtil.toWitherName(lombok.core.AnnotationValues, CharSequence, boolean).

shouldReturnThis

public static boolean shouldReturnThis(JavacNode field)
When generating a setter, the setter either returns void (beanspec) or Self (fluent). This method scans for the Accessors annotation to figure that out.


cloneSelfType

public static com.sun.tools.javac.tree.JCTree.JCExpression cloneSelfType(JavacNode field)

getAccessorsForField

public static AnnotationValues<Accessors> getAccessorsForField(JavacNode field)

fieldExists

public static JavacHandlerUtil.MemberExistsResult fieldExists(String fieldName,
                                                              JavacNode node)
Checks if there is a field with the provided name.

Parameters:
fieldName - the field name to check for.
node - Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.

methodExists

public static JavacHandlerUtil.MemberExistsResult methodExists(String methodName,
                                                               JavacNode node,
                                                               int params)

methodExists

public static JavacHandlerUtil.MemberExistsResult methodExists(String methodName,
                                                               JavacNode node,
                                                               boolean caseSensitive,
                                                               int params)
Checks if there is a method with the provided name. In case of multiple methods (overloading), only the first method decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.

Parameters:
methodName - the method name to check for.
node - Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.
caseSensitive - If the search should be case sensitive.
params - The number of parameters the method should have; varargs count as 0-*. Set to -1 to find any method with the appropriate name regardless of parameter count.

constructorExists

public static JavacHandlerUtil.MemberExistsResult constructorExists(JavacNode node)
Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.

Parameters:
node - Any node that represents the Type (JCClassDecl) to look in, or any child node thereof.

toJavacModifier

public static int toJavacModifier(AccessLevel accessLevel)
Turns an AccessLevel instance into the flag bit used by javac.


injectFieldSuppressWarnings

public static void injectFieldSuppressWarnings(JavacNode typeNode,
                                               com.sun.tools.javac.tree.JCTree.JCVariableDecl field)
Adds the given new field declaration to the provided type AST Node. The field carries the @SuppressWarnings("all") annotation. Also takes care of updating the JavacAST.


injectField

public static void injectField(JavacNode typeNode,
                               com.sun.tools.javac.tree.JCTree.JCVariableDecl field)
Adds the given new field declaration to the provided type AST Node. Also takes care of updating the JavacAST.


injectMethod

public static void injectMethod(JavacNode typeNode,
                                com.sun.tools.javac.tree.JCTree.JCMethodDecl method)
Adds the given new method declaration to the provided type AST Node. Can also inject constructors. Also takes care of updating the JavacAST.


chainDots

public static com.sun.tools.javac.tree.JCTree.JCExpression chainDots(JavacNode node,
                                                                     String... elems)
In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node. This method generates such an expression.

The position of the generated node(s) will be unpositioned (-1). For example, maker.Select(maker.Select(maker.Ident(NAME[java]), NAME[lang]), NAME[String]).

See Also:
JCTree.JCIdent, JCTree.JCFieldAccess

chainDots

public static com.sun.tools.javac.tree.JCTree.JCExpression chainDots(JavacNode node,
                                                                     int pos,
                                                                     String... elems)
In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node. This method generates such an expression.

The position of the generated node(s) will be equal to the pos parameter. For example, maker.Select(maker.Select(maker.Ident(NAME[java]), NAME[lang]), NAME[String]).

See Also:
JCTree.JCIdent, JCTree.JCFieldAccess

chainDotsString

public static com.sun.tools.javac.tree.JCTree.JCExpression chainDotsString(JavacNode node,
                                                                           String elems)
In javac, dotted access of any kind, from java.lang.String to var.methodName is represented by a fold-left of Select nodes with the leftmost string represented by a Ident node. This method generates such an expression. For example, maker.Select(maker.Select(maker.Ident(NAME[java]), NAME[lang]), NAME[String]).

See Also:
JCTree.JCIdent, JCTree.JCFieldAccess

findAnnotations

public static com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree.JCAnnotation> findAnnotations(JavacNode fieldNode,
                                                                                                          Pattern namePattern)
Searches the given field node for annotations and returns each one that matches the provided regular expression pattern. Only the simple name is checked - the package and any containing class are ignored.


generateNullCheck

public static com.sun.tools.javac.tree.JCTree.JCStatement generateNullCheck(com.sun.tools.javac.tree.TreeMaker treeMaker,
                                                                            JavacNode variable)
Generates a new statement that checks if the given variable is null, and if so, throws a NullPointerException with the variable name as message.


createListOfNonExistentFields

public static com.sun.tools.javac.util.List<Integer> createListOfNonExistentFields(com.sun.tools.javac.util.List<String> list,
                                                                                   JavacNode type,
                                                                                   boolean excludeStandard,
                                                                                   boolean excludeTransient)
Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.


upToTypeNode

public static JavacNode upToTypeNode(JavacNode node)

cloneType

public static com.sun.tools.javac.tree.JCTree.JCExpression cloneType(com.sun.tools.javac.tree.TreeMaker maker,
                                                                     com.sun.tools.javac.tree.JCTree.JCExpression in,
                                                                     com.sun.tools.javac.tree.JCTree source)
Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply). If there's any node in the tree that we don't know how to clone, that part isn't cloned. However, we wouldn't know what could possibly show up that we can't currently clone; that's just a safeguard. This should be used if the type looks the same in the code, but resolves differently. For example, a static method that has some generics in it named after the class's own parameter, but as its a static method, the static method's notion of T is different from the class notion of T. If you're duplicating a type used in the class context, you need to use this method.


Lombok - v0.11.4

Copyright © 2009-2011 The Project Lombok Authors, licensed under the MIT licence.