Lombok - v0.10.8

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.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, 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 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 com.sun.tools.javac.tree.JCTree getGeneratedBy(com.sun.tools.javac.tree.JCTree 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 isGenerated(com.sun.tools.javac.tree.JCTree node)
           
static JavacHandlerUtil.MemberExistsResult methodExists(String methodName, JavacNode node)
           
static JavacHandlerUtil.MemberExistsResult methodExists(String methodName, JavacNode node, boolean caseSensitive)
          Checks if there is a method with the provided name.
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 int toJavacModifier(AccessLevel accessLevel)
          Turns an AccessLevel instance into the flag bit used by javac.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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)

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.

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)

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)

methodExists

public static JavacHandlerUtil.MemberExistsResult methodExists(String methodName,
                                                               JavacNode node,
                                                               boolean caseSensitive)
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.

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. 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.


Lombok - v0.10.8

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