Search in sources :

Example 31 with JavacNode

use of lombok.javac.JavacNode in project lombok by rzwitserloot.

the class JavacHandlerUtil method cloneSelfType.

public static JCExpression cloneSelfType(JavacNode childOfType) {
    JavacNode typeNode = childOfType;
    JavacTreeMaker maker = childOfType.getTreeMaker();
    while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
    if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
        JCClassDecl type = (JCClassDecl) typeNode.get();
        ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
        if (!type.typarams.isEmpty()) {
            for (JCTypeParameter tp : type.typarams) {
                typeArgs.append(maker.Ident(tp.name));
            }
            return maker.TypeApply(maker.Ident(type.name), typeArgs.toList());
        } else {
            return maker.Ident(type.name);
        }
    } else {
        return null;
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 32 with JavacNode

use of lombok.javac.JavacNode 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 33 with JavacNode

use of lombok.javac.JavacNode in project lombok by rzwitserloot.

the class HandleUtilityClass method changeModifiersAndGenerateConstructor.

private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode errorNode) {
    JCClassDecl classDecl = (JCClassDecl) typeNode.get();
    boolean makeConstructor = true;
    classDecl.mods.flags |= Flags.FINAL;
    boolean markStatic = true;
    if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
        markStatic = false;
    if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
        JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get();
        if ((typeDecl.mods.flags & Flags.INTERFACE) != 0)
            markStatic = false;
    }
    if (markStatic)
        classDecl.mods.flags |= Flags.STATIC;
    for (JavacNode element : typeNode.down()) {
        if (element.getKind() == Kind.FIELD) {
            JCVariableDecl fieldDecl = (JCVariableDecl) element.get();
            fieldDecl.mods.flags |= Flags.STATIC;
        } else if (element.getKind() == Kind.METHOD) {
            JCMethodDecl methodDecl = (JCMethodDecl) element.get();
            if (methodDecl.name.contentEquals("<init>")) {
                if (getGeneratedBy(methodDecl) == null && (methodDecl.mods.flags & Flags.GENERATEDCONSTR) == 0) {
                    element.addError("@UtilityClasses cannot have declared constructors.");
                    makeConstructor = false;
                    continue;
                }
            }
            methodDecl.mods.flags |= Flags.STATIC;
        } else if (element.getKind() == Kind.TYPE) {
            JCClassDecl innerClassDecl = (JCClassDecl) element.get();
            innerClassDecl.mods.flags |= Flags.STATIC;
        }
    }
    if (makeConstructor)
        createPrivateDefaultConstructor(typeNode);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 34 with JavacNode

use of lombok.javac.JavacNode in project lombok by rzwitserloot.

the class HandleWither method generateWitherForType.

public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
    if (checkForTypeLevelWither) {
        if (hasAnnotation(Wither.class, typeNode)) {
            //The annotation will make it happen, so we can skip it.
            return;
        }
    }
    JCClassDecl typeDecl = null;
    if (typeNode.get() instanceof JCClassDecl)
        typeDecl = (JCClassDecl) typeNode.get();
    long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
    boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
    if (typeDecl == null || notAClass) {
        errorNode.addError("@Wither is only supported on a class or a field.");
        return;
    }
    for (JavacNode field : typeNode.down()) {
        if (field.getKind() != Kind.FIELD)
            continue;
        JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
        //Skip fields that start with $
        if (fieldDecl.name.toString().startsWith("$"))
            continue;
        //Skip static fields.
        if ((fieldDecl.mods.flags & Flags.STATIC) != 0)
            continue;
        //Skip final initialized fields.
        if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null)
            continue;
        generateWitherForField(field, errorNode.get(), level);
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 35 with JavacNode

use of lombok.javac.JavacNode in project lombok by rzwitserloot.

the class JavacHandlerUtil method createFieldAccessor.

static JCExpression createFieldAccessor(JavacTreeMaker maker, JavacNode field, FieldAccess fieldAccess, JCExpression receiver) {
    boolean lookForGetter = lookForGetter(field, fieldAccess);
    GetterMethod getter = lookForGetter ? findGetter(field) : null;
    JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
    if (getter == null) {
        if (receiver == null) {
            if ((fieldDecl.mods.flags & Flags.STATIC) == 0) {
                receiver = maker.Ident(field.toName("this"));
            } else {
                JavacNode containerNode = field.up();
                if (containerNode != null && containerNode.get() instanceof JCClassDecl) {
                    JCClassDecl container = (JCClassDecl) field.up().get();
                    receiver = maker.Ident(container.name);
                }
            }
        }
        return receiver == null ? maker.Ident(fieldDecl.name) : maker.Select(receiver, fieldDecl.name);
    }
    if (receiver == null)
        receiver = maker.Ident(field.toName("this"));
    JCMethodInvocation call = maker.Apply(List.<JCExpression>nil(), maker.Select(receiver, getter.name), List.<JCExpression>nil());
    return call;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

JavacNode (lombok.javac.JavacNode)52 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)27 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)23 ListBuffer (com.sun.tools.javac.util.ListBuffer)18 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)15 JavacTreeMaker (lombok.javac.JavacTreeMaker)15 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)13 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)13 Name (com.sun.tools.javac.util.Name)11 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)9 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)8 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)7 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)6 ArrayList (java.util.ArrayList)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCTree (com.sun.tools.javac.tree.JCTree)4 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 AccessLevel (lombok.AccessLevel)4