Search in sources :

Example 41 with JavacTreeMaker

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

the class HandleBuilder method generateToBuilderMethod.

private JCMethodDecl generateToBuilderMethod(BuilderJob job, List<JCTypeParameter> typeParameters, String prefix) {
    // return new ThingieBuilder<A, B>().setA(this.a).setB(this.b);
    JavacTreeMaker maker = job.getTreeMaker();
    JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderClassName), !job.isStatic, job.builderTypeParams), List.<JCExpression>nil(), null);
    JCExpression invoke = call;
    ListBuffer<JCStatement> preStatements = null;
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    for (BuilderFieldData bfd : job.builderFields) {
        String setterPrefix = !prefix.isEmpty() ? prefix : job.oldFluent ? "" : "set";
        String prefixedSetterName = bfd.name.toString();
        if (!setterPrefix.isEmpty())
            prefixedSetterName = HandlerUtil.buildAccessorName(job.sourceNode, setterPrefix, prefixedSetterName);
        Name setterName = job.toName(prefixedSetterName);
        JCExpression[] tgt = new JCExpression[bfd.singularData == null ? 1 : 2];
        if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) {
            for (int i = 0; i < tgt.length; i++) {
                tgt[i] = maker.Select(maker.Ident(job.toName("this")), bfd.obtainVia == null ? bfd.rawName : job.toName(bfd.obtainVia.field()));
            }
        } else {
            String name = bfd.obtainVia.method();
            JCMethodInvocation inv;
            if (bfd.obtainVia.isStatic()) {
                JCExpression c = maker.Select(maker.Ident(job.toName(job.parentType.getName())), job.toName(name));
                inv = maker.Apply(typeParameterNames(maker, typeParameters), c, List.<JCExpression>of(maker.Ident(job.toName("this"))));
            } else {
                JCExpression c = maker.Select(maker.Ident(job.toName("this")), job.toName(name));
                inv = maker.Apply(List.<JCExpression>nil(), c, List.<JCExpression>nil());
            }
            for (int i = 0; i < tgt.length; i++) tgt[i] = maker.Ident(bfd.name);
            // javac appears to cache the type of JCMethodInvocation expressions based on position, meaning, if you have 2 ObtainVia-based method invokes on different types, you get bizarre type mismatch errors.
            // going via a local variable declaration solves the problem.
            JCExpression varType = JavacHandlerUtil.cloneType(maker, bfd.type, job.sourceNode);
            if (preStatements == null)
                preStatements = new ListBuffer<JCStatement>();
            preStatements.append(maker.VarDef(maker.Modifiers(Flags.FINAL), bfd.name, varType, inv));
        }
        JCExpression arg;
        if (bfd.singularData == null) {
            arg = tgt[0];
            invoke = maker.Apply(List.<JCExpression>nil(), maker.Select(invoke, setterName), List.of(arg));
        } else {
            JCExpression isNotNull = maker.Binary(CTC_NOT_EQUAL, tgt[0], maker.Literal(CTC_BOT, null));
            JCExpression invokeBuilder = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(job.toName(BUILDER_TEMP_VAR)), setterName), List.<JCExpression>of(tgt[1]));
            statements.append(maker.If(isNotNull, maker.Exec(invokeBuilder), null));
        }
    }
    if (!statements.isEmpty()) {
        JCExpression tempVarType = namePlusTypeParamsToTypeReference(maker, job.parentType, job.getBuilderClassName(), !job.isStatic, typeParameters);
        statements.prepend(maker.VarDef(maker.Modifiers(Flags.FINAL), job.toName(BUILDER_TEMP_VAR), tempVarType, invoke));
        statements.append(maker.Return(maker.Ident(job.toName(BUILDER_TEMP_VAR))));
    } else {
        statements.append(maker.Return(invoke));
    }
    if (preStatements != null) {
        preStatements.appendList(statements);
        statements = preStatements;
    }
    JCBlock body = maker.Block(0, statements.toList());
    List<JCAnnotation> annsOnParamType = List.nil();
    if (job.checkerFramework.generateUnique())
        annsOnParamType = List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
    JCMethodDecl methodDef = maker.MethodDef(maker.Modifiers(toJavacModifier(job.accessOuters)), job.toName(TO_BUILDER_METHOD_NAME), namePlusTypeParamsToTypeReference(maker, job.parentType, job.getBuilderClassName(), !job.isStatic, typeParameters, annsOnParamType), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
    createRelevantNonNullAnnotation(job.parentType, methodDef);
    return methodDef;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) ToString(lombok.ToString) Name(com.sun.tools.javac.util.Name) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 42 with JavacTreeMaker

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

the class HandleBuilder method generateBuilderMethod.

public JCMethodDecl generateBuilderMethod(BuilderJob job) {
    // String builderClassName, JavacNode source, JavacNode type, List<JCTypeParameter> typeParams, AccessLevel access) {
    // builderClassName, annotationNode, tdParent, typeParams, accessForOuters);
    JavacTreeMaker maker = job.getTreeMaker();
    JCExpression call;
    if (job.isStatic) {
        call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderClassName), false, job.typeParams), List.<JCExpression>nil(), null);
    } else {
        call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, null, job.toName(job.builderClassName), false, job.typeParams), List.<JCExpression>nil(), null);
        ((JCNewClass) call).encl = maker.Ident(job.toName("this"));
    }
    JCStatement statement = maker.Return(call);
    JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
    int modifiers = toJavacModifier(job.accessOuters);
    if (job.isStatic)
        modifiers |= Flags.STATIC;
    List<JCAnnotation> annsOnMethod = List.nil();
    if (job.checkerFramework.generateSideEffectFree())
        annsOnMethod = List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil()));
    List<JCAnnotation> annsOnParamType = List.nil();
    if (job.checkerFramework.generateUnique())
        annsOnParamType = List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
    JCExpression returnType = namePlusTypeParamsToTypeReference(maker, job.parentType, job.getBuilderClassName(), !job.isStatic, job.builderTypeParams, annsOnParamType);
    JCMethodDecl methodDef = maker.MethodDef(maker.Modifiers(modifiers, annsOnMethod), job.toName(job.builderMethodName), returnType, job.copyTypeParams(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
    createRelevantNonNullAnnotation(job.parentType, methodDef);
    return methodDef;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 43 with JavacTreeMaker

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

the class JavacHandlerUtil method chainDots.

public static JCExpression chainDots(JavacNode node, LombokImmutableList<String> elems) {
    assert elems != null;
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression e = null;
    for (String elem : elems) {
        if (e == null)
            e = maker.Ident(node.toName(elem));
        else
            e = maker.Select(e, node.toName(elem));
    }
    return e;
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 44 with JavacTreeMaker

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

the class HandleStandardException method addConstructorProperties.

private static void addConstructorProperties(JCModifiers mods, JavacNode node, boolean msgParam, boolean causeParam) {
    if (!msgParam && !causeParam)
        return;
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties");
    ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>();
    if (msgParam)
        fieldNames.append(maker.Literal("message"));
    if (causeParam)
        fieldNames.append(maker.Literal("cause"));
    JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList());
    JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray));
    mods.annotations = mods.annotations.append(annotation);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 45 with JavacTreeMaker

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

the class HandleConstructor method addConstructorProperties.

public static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) {
    if (fields.isEmpty())
        return;
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties");
    ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>();
    for (JavacNode field : fields) {
        Name fieldName = removePrefixFromField(field);
        fieldNames.append(maker.Literal(fieldName.toString()));
    }
    JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList());
    JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray));
    mods.annotations = mods.annotations.append(annotation);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) Name(com.sun.tools.javac.util.Name)

Aggregations

JavacTreeMaker (lombok.javac.JavacTreeMaker)94 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)70 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)43 Name (com.sun.tools.javac.util.Name)42 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)34 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)34 ListBuffer (com.sun.tools.javac.util.ListBuffer)34 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)33 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)30 JavacNode (lombok.javac.JavacNode)29 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)26 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)23 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)18 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)12 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)12 ArrayList (java.util.ArrayList)12 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)10 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)9 JCTypeApply (com.sun.tools.javac.tree.JCTree.JCTypeApply)9 ToString (lombok.ToString)9