Search in sources :

Example 91 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project lombok by rzwitserloot.

the class JavacHandlerUtil method findAnnotations.

/**
 * 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.
 */
public static List<JCAnnotation> findAnnotations(JavacNode fieldNode, Pattern namePattern) {
    ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
    for (JavacNode child : fieldNode.down()) {
        if (child.getKind() == Kind.ANNOTATION) {
            JCAnnotation annotation = (JCAnnotation) child.get();
            String name = annotation.annotationType.toString();
            int idx = name.lastIndexOf(".");
            String suspect = idx == -1 ? name : name.substring(idx + 1);
            if (namePattern.matcher(suspect).matches()) {
                result.append(annotation);
            }
        }
    }
    return result.toList();
}
Also used : JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 92 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project lombok by rzwitserloot.

the class JavacHandlerUtil method addAnnotation.

public static void addAnnotation(JCModifiers mods, JavacNode node, JavacNode source, String annotationTypeFqn, JCExpression arg) {
    boolean isJavaLangBased;
    String simpleName;
    {
        int idx = annotationTypeFqn.lastIndexOf('.');
        simpleName = idx == -1 ? annotationTypeFqn : annotationTypeFqn.substring(idx + 1);
        isJavaLangBased = idx == 9 && annotationTypeFqn.regionMatches(0, "java.lang.", 0, 10);
    }
    for (JCAnnotation ann : mods.annotations) {
        JCTree annType = ann.getAnnotationType();
        if (annType instanceof JCIdent) {
            Name lastPart = ((JCIdent) annType).name;
            if (lastPart.contentEquals(simpleName))
                return;
        }
        if (annType instanceof JCFieldAccess) {
            if (annType.toString().equals(annotationTypeFqn))
                return;
        }
    }
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression annType = isJavaLangBased ? genJavaLangTypeRef(node, simpleName) : chainDotsString(node, annotationTypeFqn);
    List<JCExpression> argList = arg != null ? List.of(arg) : List.<JCExpression>nil();
    JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(annType, argList), source);
    mods.annotations = mods.annotations.append(annotation);
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCTree(com.sun.tools.javac.tree.JCTree) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) TypeName(lombok.core.configuration.TypeName) Name(com.sun.tools.javac.util.Name)

Example 93 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project lombok by rzwitserloot.

the class PrettyPrinter method printAnnotations.

private void printAnnotations(List<JCAnnotation> annotations, boolean newlines) {
    for (JCAnnotation ann : annotations) {
        print(ann);
        if (newlines) {
            println();
            align();
        } else
            print(" ");
    }
}
Also used : JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)93 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)64 Name (com.sun.tools.javac.util.Name)35 ListBuffer (com.sun.tools.javac.util.ListBuffer)34 JavacTreeMaker (lombok.javac.JavacTreeMaker)33 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)29 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)29 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)28 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)27 JavacNode (lombok.javac.JavacNode)27 JCTree (com.sun.tools.javac.tree.JCTree)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)22 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)21 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)11 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)11 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)11 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)10 ArrayList (java.util.ArrayList)10