Search in sources :

Example 56 with JCModifiers

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

the class HandleSuperBuilder method generateBuilderBasedConstructor.

/**
 * Generates a constructor that has a builder as the only parameter.
 * The values from the builder are used to initialize the fields of new instances.
 *
 * @param callBuilderBasedSuperConstructor
 *            If {@code true}, the constructor will explicitly call a super
 *            constructor with the builder as argument. Requires
 *            {@code builderClassAsParameter != null}.
 */
private void generateBuilderBasedConstructor(SuperBuilderJob job, boolean callBuilderBasedSuperConstructor) {
    JavacTreeMaker maker = job.getTreeMaker();
    AccessLevel level = AccessLevel.PROTECTED;
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    Name builderVariableName = job.toName(BUILDER_VARIABLE_NAME);
    for (BuilderFieldData bfd : job.builderFields) {
        JCExpression rhs;
        if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
            bfd.singularData.getSingularizer().appendBuildCode(bfd.singularData, bfd.originalFieldNode, job.sourceNode, statements, bfd.builderFieldName, "b");
            rhs = maker.Ident(bfd.singularData.getPluralName());
        } else {
            rhs = maker.Select(maker.Ident(builderVariableName), bfd.builderFieldName);
        }
        JCFieldAccess fieldInThis = maker.Select(maker.Ident(job.toName("this")), bfd.rawName);
        JCStatement assign = maker.Exec(maker.Assign(fieldInThis, rhs));
        // In case of @Builder.Default, set the value to the default if it was not set in the builder.
        if (bfd.nameOfSetFlag != null) {
            JCFieldAccess setField = maker.Select(maker.Ident(builderVariableName), bfd.nameOfSetFlag);
            fieldInThis = maker.Select(maker.Ident(job.toName("this")), bfd.rawName);
            JCExpression parentTypeRef = namePlusTypeParamsToTypeReference(maker, job.parentType, List.<JCTypeParameter>nil());
            JCAssign assignDefault = maker.Assign(fieldInThis, maker.Apply(typeParameterNames(maker, ((JCClassDecl) job.parentType.get()).typarams), maker.Select(parentTypeRef, bfd.nameOfDefaultProvider), List.<JCExpression>nil()));
            statements.append(maker.If(setField, assign, maker.Exec(assignDefault)));
        } else {
            statements.append(assign);
        }
        if (hasNonNullAnnotations(bfd.originalFieldNode)) {
            JCStatement nullCheck = generateNullCheck(maker, bfd.originalFieldNode, job.sourceNode);
            if (nullCheck != null)
                statements.append(nullCheck);
        }
    }
    List<JCAnnotation> annsOnMethod = job.checkerFramework.generateSideEffectFree() ? List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil())) : List.<JCAnnotation>nil();
    JCModifiers mods = maker.Modifiers(toJavacModifier(level), annsOnMethod);
    // Create a constructor that has just the builder as parameter.
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, job.getContext());
    // First add all generics that are present on the parent type.
    ListBuffer<JCExpression> typeParamsForBuilderParameter = getTypeParamExpressions(job.typeParams, maker, job.sourceNode);
    // Now add the <?, ?>.
    JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
    typeParamsForBuilderParameter.append(wildcard);
    wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
    typeParamsForBuilderParameter.append(wildcard);
    JCTypeApply paramType = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, job.parentType, job.getBuilderClassName(), false, List.<JCTypeParameter>nil()), typeParamsForBuilderParameter.toList());
    JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), builderVariableName, paramType, null);
    params.append(param);
    if (callBuilderBasedSuperConstructor) {
        // The first statement must be the call to the super constructor.
        JCMethodInvocation callToSuperConstructor = maker.Apply(List.<JCExpression>nil(), maker.Ident(job.toName("super")), List.<JCExpression>of(maker.Ident(builderVariableName)));
        statements.prepend(maker.Exec(callToSuperConstructor));
    }
    JCMethodDecl constr = recursiveSetGeneratedBy(maker.MethodDef(mods, job.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, statements.toList()), null), job.sourceNode);
    injectMethod(job.parentType, constr);
}
Also used : JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) BuilderFieldData(lombok.javac.handlers.HandleBuilder.BuilderFieldData) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) AccessLevel(lombok.AccessLevel) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 57 with JCModifiers

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

the class HandleSuperBuilder method generateAbstractSelfMethod.

private JCMethodDecl generateAbstractSelfMethod(SuperBuilderJob job, boolean override, String builderGenericName) {
    JavacTreeMaker maker = job.getTreeMaker();
    List<JCAnnotation> annotations = List.nil();
    JCAnnotation overrideAnnotation = override ? maker.Annotation(genJavaLangTypeRef(job.builderType, "Override"), List.<JCExpression>nil()) : null;
    JCAnnotation sefAnnotation = job.checkerFramework.generatePure() ? maker.Annotation(genTypeRef(job.builderType, CheckerFrameworkVersion.NAME__PURE), List.<JCExpression>nil()) : null;
    if (sefAnnotation != null)
        annotations = annotations.prepend(sefAnnotation);
    if (overrideAnnotation != null)
        annotations = annotations.prepend(overrideAnnotation);
    JCModifiers modifiers = maker.Modifiers(Flags.PROTECTED | Flags.ABSTRACT, annotations);
    Name name = job.toName(SELF_METHOD);
    JCExpression returnType = maker.Ident(job.toName(builderGenericName));
    returnType = addCheckerFrameworkReturnsReceiver(returnType, maker, job.builderType, job.checkerFramework);
    return maker.MethodDef(modifiers, name, returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) Name(com.sun.tools.javac.util.Name)

Example 58 with JCModifiers

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

the class HandleSuperBuilder method generateBuilderAbstractClass.

/**
 * Creates and returns the abstract builder class and injects it into the annotated class.
 */
private JavacNode generateBuilderAbstractClass(SuperBuilderJob job, JCExpression superclassBuilderClass, List<JCExpression> superclassTypeParams, String classGenericName, String builderGenericName) {
    JavacTreeMaker maker = job.parentType.getTreeMaker();
    JCModifiers mods = maker.Modifiers(Flags.STATIC | Flags.ABSTRACT | Flags.PUBLIC);
    // Keep any type params of the annotated class.
    ListBuffer<JCTypeParameter> allTypeParams = new ListBuffer<JCTypeParameter>();
    allTypeParams.appendList(copyTypeParams(job.sourceNode, job.typeParams));
    // Add builder-specific type params required for inheritable builders.
    // 1. The return type for the build() method, named "C", which extends the annotated class.
    JCExpression annotatedClass = namePlusTypeParamsToTypeReference(maker, job.parentType, job.typeParams);
    allTypeParams.append(maker.TypeParameter(job.toName(classGenericName), List.<JCExpression>of(annotatedClass)));
    // 2. The return type for all setter methods, named "B", which extends this builder class.
    Name builderClassName = job.toName(job.builderClassName);
    ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(job.typeParams, maker, job.sourceNode);
    typeParamsForBuilder.append(maker.Ident(job.toName(classGenericName)));
    typeParamsForBuilder.append(maker.Ident(job.toName(builderGenericName)));
    JCTypeApply typeApply = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, job.parentType, builderClassName, false, List.<JCTypeParameter>nil()), typeParamsForBuilder.toList());
    allTypeParams.append(maker.TypeParameter(job.toName(builderGenericName), List.<JCExpression>of(typeApply)));
    JCExpression extending = null;
    if (superclassBuilderClass != null) {
        // If the annotated class extends another class, we want this builder to extend the builder of the superclass.
        // 1. Add the type parameters of the superclass.
        typeParamsForBuilder = getTypeParamExpressions(superclassTypeParams, maker, job.sourceNode);
        // 2. Add the builder type params <C, B>.
        typeParamsForBuilder.append(maker.Ident(job.toName(classGenericName)));
        typeParamsForBuilder.append(maker.Ident(job.toName(builderGenericName)));
        extending = maker.TypeApply(superclassBuilderClass, typeParamsForBuilder.toList());
    }
    JCClassDecl builder = maker.ClassDef(mods, builderClassName, allTypeParams.toList(), extending, List.<JCExpression>nil(), List.<JCTree>nil());
    recursiveSetGeneratedBy(builder, job.sourceNode);
    return injectType(job.parentType, builder);
}
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) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) Name(com.sun.tools.javac.util.Name)

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