Search in sources :

Example 81 with JCAnnotation

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

the class JavacHandlerUtil method addCheckerFrameworkReturnsReceiver.

static JCExpression addCheckerFrameworkReturnsReceiver(JCExpression returnType, JavacTreeMaker maker, JavacNode typeNode, CheckerFrameworkVersion cfv) {
    if (cfv.generateReturnsReceiver()) {
        JCAnnotation rrAnnotation = maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__RETURNS_RECEIVER), List.<JCExpression>nil());
        returnType = maker.AnnotatedType(List.of(rrAnnotation), returnType);
    }
    return returnType;
}
Also used : JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 82 with JCAnnotation

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

the class JavacHandlerUtil method cloneType0.

private static JCExpression cloneType0(JavacTreeMaker maker, JCTree in) {
    if (in == null)
        return null;
    if (in instanceof JCPrimitiveTypeTree) {
        return maker.TypeIdent(TypeTag.typeTag(in));
    }
    if (in instanceof JCIdent) {
        return maker.Ident(((JCIdent) in).name);
    }
    if (in instanceof JCFieldAccess) {
        JCFieldAccess fa = (JCFieldAccess) in;
        return maker.Select(cloneType0(maker, fa.selected), fa.name);
    }
    if (in instanceof JCArrayTypeTree) {
        JCArrayTypeTree att = (JCArrayTypeTree) in;
        return maker.TypeArray(cloneType0(maker, att.elemtype));
    }
    if (in instanceof JCTypeApply) {
        JCTypeApply ta = (JCTypeApply) in;
        ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
        for (JCExpression typeArg : ta.arguments) {
            lb.append(cloneType0(maker, typeArg));
        }
        return maker.TypeApply(cloneType0(maker, ta.clazz), lb.toList());
    }
    if (in instanceof JCWildcard) {
        JCWildcard w = (JCWildcard) in;
        JCExpression newInner = cloneType0(maker, w.inner);
        TypeBoundKind newKind;
        switch(w.getKind()) {
            case SUPER_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.SUPER);
                break;
            case EXTENDS_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.EXTENDS);
                break;
            default:
            case UNBOUNDED_WILDCARD:
                newKind = maker.TypeBoundKind(BoundKind.UNBOUND);
                break;
        }
        return maker.Wildcard(newKind, newInner);
    }
    if (JCAnnotatedTypeReflect.is(in)) {
        JCExpression underlyingType = cloneType0(maker, JCAnnotatedTypeReflect.getUnderlyingType(in));
        List<JCAnnotation> anns = copyAnnotations(JCAnnotatedTypeReflect.getAnnotations(in));
        return JCAnnotatedTypeReflect.create(anns, underlyingType);
    }
    // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline.
    return (JCExpression) in;
}
Also used : JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree) TypeBoundKind(com.sun.tools.javac.tree.JCTree.TypeBoundKind)

Example 83 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation 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 84 with JCAnnotation

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

the class HandleSuperBuilder method generateBuilderMethod.

private JCMethodDecl generateBuilderMethod(SuperBuilderJob job) {
    JavacTreeMaker maker = job.getTreeMaker();
    JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderImplClassName), false, job.typeParams), List.<JCExpression>nil(), null);
    JCStatement statement = maker.Return(call);
    JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
    int modifiers = Flags.PUBLIC;
    modifiers |= Flags.STATIC;
    // Add any type params of the annotated class to the return type.
    ListBuffer<JCExpression> typeParameterNames = new ListBuffer<JCExpression>();
    typeParameterNames.appendList(typeParameterNames(maker, job.typeParams));
    // Now add the <?, ?>.
    JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
    typeParameterNames.append(wildcard);
    typeParameterNames.append(wildcard);
    // And return type annotations.
    List<JCAnnotation> annsOnParamType = List.nil();
    if (job.checkerFramework.generateUnique())
        annsOnParamType = List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__UNIQUE), List.<JCExpression>nil()));
    JCTypeApply returnType = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderAbstractClassName), false, List.<JCTypeParameter>nil(), annsOnParamType), typeParameterNames.toList());
    List<JCAnnotation> annsOnMethod = job.checkerFramework.generateSideEffectFree() ? List.of(maker.Annotation(genTypeRef(job.parentType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil())) : List.<JCAnnotation>nil();
    JCMethodDecl methodDef = maker.MethodDef(maker.Modifiers(modifiers, annsOnMethod), job.toName(job.builderMethodName), returnType, copyTypeParams(job.sourceNode, job.typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
    createRelevantNonNullAnnotation(job.parentType, methodDef);
    return methodDef;
}
Also used : JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) 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) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 85 with JCAnnotation

use of com.sun.tools.javac.tree.JCTree.JCAnnotation 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)

Aggregations

JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)93 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)64 Name (com.sun.tools.javac.util.Name)35 ListBuffer (com.sun.tools.javac.util.ListBuffer)34 JavacTreeMaker (lombok.javac.JavacTreeMaker)33 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)29 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)29 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)28 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)27 JavacNode (lombok.javac.JavacNode)27 JCTree (com.sun.tools.javac.tree.JCTree)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)22 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)21 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)13 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)11 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)11 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)11 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)10 ArrayList (java.util.ArrayList)10