Search in sources :

Example 51 with JCModifiers

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

the class HandleBuilder method generateBuilderFields.

public void generateBuilderFields(BuilderJob job) {
    int len = job.builderFields.size();
    java.util.List<JavacNode> existing = new ArrayList<JavacNode>();
    for (JavacNode child : job.builderType.down()) {
        if (child.getKind() == Kind.FIELD)
            existing.add(child);
    }
    java.util.List<JCVariableDecl> generated = new ArrayList<JCVariableDecl>();
    for (int i = len - 1; i >= 0; i--) {
        BuilderFieldData bfd = job.builderFields.get(i);
        if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
            java.util.List<JavacNode> generateSingularFields = bfd.singularData.getSingularizer().generateFields(bfd.singularData, job.builderType, job.sourceNode);
            for (JavacNode field : generateSingularFields) {
                generated.add((JCVariableDecl) field.get());
            }
            bfd.createdFields.addAll(generateSingularFields);
        } else {
            JavacNode field = null, setFlag = null;
            for (JavacNode exists : existing) {
                Name n = ((JCVariableDecl) exists.get()).name;
                if (n.equals(bfd.builderFieldName))
                    field = exists;
                if (n.equals(bfd.nameOfSetFlag))
                    setFlag = exists;
            }
            JavacTreeMaker maker = job.getTreeMaker();
            if (field == null) {
                JCModifiers mods = maker.Modifiers(Flags.PRIVATE);
                JCVariableDecl newField = maker.VarDef(mods, bfd.builderFieldName, cloneType(maker, bfd.type, job.sourceNode), null);
                field = injectFieldAndMarkGenerated(job.builderType, newField);
                generated.add(newField);
            }
            if (setFlag == null && bfd.nameOfSetFlag != null) {
                JCModifiers mods = maker.Modifiers(Flags.PRIVATE);
                JCVariableDecl newField = maker.VarDef(mods, bfd.nameOfSetFlag, maker.TypeIdent(CTC_BOOLEAN), null);
                injectFieldAndMarkGenerated(job.builderType, newField);
                generated.add(newField);
            }
            bfd.createdFields.add(field);
        }
    }
    for (JCVariableDecl gen : generated) recursiveSetGeneratedBy(gen, job.sourceNode);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) ArrayList(java.util.ArrayList) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 52 with JCModifiers

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

the class HandleBuilder method makeBuilderClass.

public JavacNode makeBuilderClass(BuilderJob job) {
    // boolean isStatic, JavacNode source, JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast, AccessLevel access) {
    // isStatic, annotationNode, tdParent, builderClassName, typeParams, ast, accessForOuters
    JavacTreeMaker maker = job.getTreeMaker();
    int modifiers = toJavacModifier(job.accessOuters);
    if (job.isStatic)
        modifiers |= Flags.STATIC;
    JCModifiers mods = maker.Modifiers(modifiers);
    JCClassDecl builder = maker.ClassDef(mods, job.getBuilderClassName(), job.copyTypeParams(), null, List.<JCExpression>nil(), List.<JCTree>nil());
    recursiveSetGeneratedBy(builder, job.sourceNode);
    return injectType(job.parentType, builder);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 53 with JCModifiers

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

the class JavacNode method isStatic.

@Override
public boolean isStatic() {
    if (node instanceof JCClassDecl) {
        JCClassDecl t = (JCClassDecl) node;
        long f = t.mods.flags;
        if (((Flags.INTERFACE | Flags.ENUM) & f) != 0)
            return true;
        JavacNode directUp = directUp();
        if (directUp == null || directUp.getKind() == Kind.COMPILATION_UNIT)
            return true;
        if (!(directUp.get() instanceof JCClassDecl))
            return false;
        JCClassDecl p = (JCClassDecl) directUp.get();
        f = p.mods.flags;
        if (((Flags.INTERFACE | Flags.ENUM) & f) != 0)
            return true;
    }
    if (node instanceof JCVariableDecl) {
        JavacNode directUp = directUp();
        if (directUp != null && directUp.get() instanceof JCClassDecl) {
            JCClassDecl p = (JCClassDecl) directUp.get();
            long f = p.mods.flags;
            if ((Flags.INTERFACE & f) != 0)
                return true;
        }
    }
    JCModifiers mods = getModifiers();
    if (mods == null)
        return false;
    return (mods.flags & Flags.STATIC) != 0;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 54 with JCModifiers

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

the class HandleSuperBuilder method generateAbstractBuildMethod.

private JCMethodDecl generateAbstractBuildMethod(SuperBuilderJob job, boolean override, String classGenericName) {
    JavacTreeMaker maker = job.getTreeMaker();
    List<JCAnnotation> annotations = List.nil();
    if (override) {
        JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(job.builderType, "Override"), List.<JCExpression>nil());
        annotations = List.of(overrideAnnotation);
    }
    if (job.checkerFramework.generateSideEffectFree())
        annotations = annotations.prepend(maker.Annotation(genTypeRef(job.builderType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil()));
    JCModifiers modifiers = maker.Modifiers(Flags.PUBLIC | Flags.ABSTRACT, annotations);
    Name name = job.toName(job.buildMethodName);
    JCExpression returnType = maker.Ident(job.toName(classGenericName));
    JCVariableDecl recv = HandleBuilder.generateReceiver(job);
    JCMethodDecl methodDef;
    if (recv != null && maker.hasMethodDefWithRecvParam()) {
        methodDef = maker.MethodDefWithRecvParam(modifiers, name, returnType, List.<JCTypeParameter>nil(), recv, List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
    } else {
        methodDef = maker.MethodDef(modifiers, name, returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
    }
    return methodDef;
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 55 with JCModifiers

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

the class HandleSuperBuilder method generateBuilderImplClass.

/**
 * Creates and returns the concrete builder implementation class and injects it into the annotated class.
 */
private JavacNode generateBuilderImplClass(SuperBuilderJob job) {
    JavacTreeMaker maker = job.getTreeMaker();
    JCModifiers mods = maker.Modifiers(Flags.STATIC | Flags.PRIVATE | Flags.FINAL);
    // Extend the abstract builder.
    JCExpression extending = namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderAbstractClassName), false, List.<JCTypeParameter>nil());
    // Add builder-specific type params required for inheritable builders.
    // 1. The return type for the build() method (named "C" in the abstract builder), which is the annotated class.
    JCExpression annotatedClass = namePlusTypeParamsToTypeReference(maker, job.parentType, job.typeParams);
    // 2. The return type for all setter methods (named "B" in the abstract builder), which is this builder class.
    JCExpression builderImplClassExpression = namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderImplClassName), false, job.typeParams);
    ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(job.typeParams, maker, job.sourceNode);
    typeParamsForBuilder.append(annotatedClass);
    typeParamsForBuilder.append(builderImplClassExpression);
    extending = maker.TypeApply(extending, typeParamsForBuilder.toList());
    JCClassDecl builder = maker.ClassDef(mods, job.toName(job.builderImplClassName), copyTypeParams(job.parentType, job.typeParams), extending, List.<JCExpression>nil(), List.<JCTree>nil());
    recursiveSetGeneratedBy(builder, job.sourceNode);
    return injectType(job.parentType, builder);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Aggregations

JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)58 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)43 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)43 Name (com.sun.tools.javac.util.Name)43 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)37 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)35 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)31 JavacTreeMaker (lombok.javac.JavacTreeMaker)30 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)29 ListBuffer (com.sun.tools.javac.util.ListBuffer)26 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)22 JavacNode (lombok.javac.JavacNode)15 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)12 ArrayList (java.util.ArrayList)7 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)6 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)3 JCReturn (com.sun.tools.javac.tree.JCTree.JCReturn)3