Search in sources :

Example 26 with JavacNode

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

the class JavacHandlerUtil method createListOfNonExistentFields.

/**
	 * 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.
	 */
public static List<Integer> createListOfNonExistentFields(List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient) {
    boolean[] matched = new boolean[list.size()];
    for (JavacNode child : type.down()) {
        if (list.isEmpty())
            break;
        if (child.getKind() != Kind.FIELD)
            continue;
        JCVariableDecl field = (JCVariableDecl) child.get();
        if (excludeStandard) {
            if ((field.mods.flags & Flags.STATIC) != 0)
                continue;
            if (field.name.toString().startsWith("$"))
                continue;
        }
        if (excludeTransient && (field.mods.flags & Flags.TRANSIENT) != 0)
            continue;
        int idx = list.indexOf(child.getName());
        if (idx > -1)
            matched[idx] = true;
    }
    ListBuffer<Integer> problematic = new ListBuffer<Integer>();
    for (int i = 0; i < list.size(); i++) {
        if (!matched[i])
            problematic.append(i);
    }
    return problematic.toList();
}
Also used : JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 27 with JavacNode

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

the class JavacJavaUtilMapSingularizer method generateFields.

@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JCTree source) {
    if (useGuavaInstead(builderType)) {
        return guavaMapSingularizer.generateFields(data, builderType, source);
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    JCVariableDecl buildKeyField;
    {
        JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
        type = addTypeArgs(1, false, builderType, type, data.getTypeArgs(), source);
        buildKeyField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$key"), type, null);
    }
    JCVariableDecl buildValueField;
    {
        JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
        List<JCExpression> tArgs = data.getTypeArgs();
        if (tArgs != null && tArgs.size() > 1)
            tArgs = tArgs.tail;
        else
            tArgs = List.nil();
        type = addTypeArgs(1, false, builderType, type, tArgs, source);
        buildValueField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$value"), type, null);
    }
    JavacNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
    JavacNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
    return Arrays.asList(keyFieldNode, valueFieldNode);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 28 with JavacNode

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

the class JavacHandlerUtil method sanityCheckForMethodGeneratingAnnotationsOnBuilderClass.

public static void sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(JavacNode typeNode, JavacNode errorNode) {
    List<String> disallowed = List.nil();
    for (JavacNode child : typeNode.down()) {
        for (Class<? extends java.lang.annotation.Annotation> annType : INVALID_ON_BUILDERS) {
            if (annotationTypeMatches(annType, child)) {
                disallowed = disallowed.append(annType.getSimpleName());
            }
        }
    }
    int size = disallowed.size();
    if (size == 0)
        return;
    if (size == 1) {
        errorNode.addError("@" + disallowed.head + " is not allowed on builder classes.");
        return;
    }
    StringBuilder out = new StringBuilder();
    for (String a : disallowed) out.append("@").append(a).append(", ");
    out.setLength(out.length() - 2);
    errorNode.addError(out.append(" are not allowed on builder classes.").toString());
}
Also used : JavacNode(lombok.javac.JavacNode)

Example 29 with JavacNode

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

the class JavacHandlerUtil method injectMethod.

/**
	 * Adds the given new method declaration to the provided type AST Node.
	 * Can also inject constructors.
	 * 
	 * Also takes care of updating the JavacAST.
	 */
public static void injectMethod(JavacNode typeNode, JCMethodDecl method, List<Type> paramTypes, Type returnType) {
    JCClassDecl type = (JCClassDecl) typeNode.get();
    if (method.getName().contentEquals("<init>")) {
        //Scan for default constructor, and remove it.
        int idx = 0;
        for (JCTree def : type.defs) {
            if (def instanceof JCMethodDecl) {
                if ((((JCMethodDecl) def).mods.flags & Flags.GENERATEDCONSTR) != 0) {
                    JavacNode tossMe = typeNode.getNodeFor(def);
                    if (tossMe != null)
                        tossMe.up().removeChild(tossMe);
                    type.defs = addAllButOne(type.defs, idx);
                    ClassSymbolMembersField.remove(type.sym, ((JCMethodDecl) def).sym);
                    break;
                }
            }
            idx++;
        }
    }
    addSuppressWarningsAll(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
    addGenerated(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
    type.defs = type.defs.append(method);
    fixMethodMirror(typeNode.getContext(), typeNode.getElement(), method.getModifiers().flags, method.getName(), paramTypes, returnType);
    typeNode.add(method, Kind.METHOD);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) JCTree(com.sun.tools.javac.tree.JCTree)

Example 30 with JavacNode

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

the class JavacHandlerUtil method findGetter.

private static GetterMethod findGetter(JavacNode field) {
    JCVariableDecl decl = (JCVariableDecl) field.get();
    JavacNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field)) {
        for (JavacNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
            if (!method.name.toString().equalsIgnoreCase(potentialGetterName))
                continue;
            /** static getX() methods don't count. */
            if ((method.mods.flags & Flags.STATIC) != 0)
                continue;
            /** Nor do getters with a non-empty parameter list. */
            if (method.params != null && method.params.size() > 0)
                continue;
            return new GetterMethod(method.name, method.restype);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (JavacNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            //Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && new HandleGetter().fieldQualifiesForGetterGeneration(field)) {
        //Check if the class has @Getter or @Data annotation.
        JavacNode containingType = field.up();
        if (containingType != null)
            for (JavacNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    //Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field);
        if (getterName == null)
            return null;
        return new GetterMethod(field.toName(getterName), decl.vartype);
    }
    return null;
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) 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