Search in sources :

Example 61 with JavacTreeMaker

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

the class HandleEqualsAndHashCode method createTypeReference.

public JCExpression createTypeReference(JavacNode type, boolean addWildcards) {
    java.util.List<String> list = new ArrayList<String>();
    java.util.List<Integer> genericsCount = addWildcards ? new ArrayList<Integer>() : null;
    list.add(type.getName());
    if (addWildcards)
        genericsCount.add(((JCClassDecl) type.get()).typarams.size());
    boolean staticContext = (((JCClassDecl) type.get()).getModifiers().flags & Flags.STATIC) != 0;
    JavacNode tNode = type.up();
    while (tNode != null && tNode.getKind() == Kind.TYPE && !tNode.getName().isEmpty()) {
        list.add(tNode.getName());
        if (addWildcards)
            genericsCount.add(staticContext ? 0 : ((JCClassDecl) tNode.get()).typarams.size());
        if (!staticContext)
            staticContext = (((JCClassDecl) tNode.get()).getModifiers().flags & Flags.STATIC) != 0;
        tNode = tNode.up();
    }
    Collections.reverse(list);
    if (addWildcards)
        Collections.reverse(genericsCount);
    JavacTreeMaker maker = type.getTreeMaker();
    JCExpression chain = maker.Ident(type.toName(list.get(0)));
    if (addWildcards)
        chain = wildcardify(maker, chain, genericsCount.get(0));
    for (int i = 1; i < list.size(); i++) {
        chain = maker.Select(chain, type.toName(list.get(i)));
        if (addWildcards)
            chain = wildcardify(maker, chain, genericsCount.get(i));
    }
    return chain;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) ArrayList(java.util.ArrayList) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode)

Example 62 with JavacTreeMaker

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

the class HandleEqualsAndHashCode method createResultCalculation.

public JCExpressionStatement createResultCalculation(JavacNode typeNode, JCExpression expr) {
    /* result = result * PRIME + expr; */
    JavacTreeMaker maker = typeNode.getTreeMaker();
    Name resultName = typeNode.toName(RESULT_NAME);
    JCExpression mult = maker.Binary(CTC_MUL, maker.Ident(resultName), maker.Ident(typeNode.toName(PRIME_NAME)));
    JCExpression add = maker.Binary(CTC_PLUS, mult, expr);
    return maker.Exec(maker.Assign(maker.Ident(resultName), add));
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Name(com.sun.tools.javac.util.Name)

Example 63 with JavacTreeMaker

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

the class HandleEqualsAndHashCode method createHashCode.

public JCMethodDecl createHashCode(JavacNode typeNode, java.util.List<Included<JavacNode, EqualsAndHashCode.Include>> members, boolean callSuper, boolean cacheHashCode, FieldAccess fieldAccess, JavacNode source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil());
    List<JCAnnotation> annsOnMethod = List.of(overrideAnnotation);
    CheckerFrameworkVersion checkerFramework = getCheckerFrameworkVersion(typeNode);
    if (cacheHashCode && checkerFramework.generatePure()) {
        annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__PURE), List.<JCExpression>nil()));
    } else if (checkerFramework.generateSideEffectFree()) {
        annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil()));
    }
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC, annsOnMethod);
    JCExpression returnType = maker.TypeIdent(CTC_INT);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    Name primeName = typeNode.toName(PRIME_NAME);
    Name resultName = typeNode.toName(RESULT_NAME);
    long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext());
    boolean isEmpty = members.isEmpty();
    /* if (this.$hashCodeCache != 0) return this.$hashCodeCache; */
    {
        if (cacheHashCode) {
            JCIdent receiver = maker.Ident(typeNode.toName("this"));
            JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME));
            JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, cacheHashCodeFieldAccess, maker.Literal(CTC_INT, 0));
            statements.append(maker.If(cacheNotZero, maker.Return(cacheHashCodeFieldAccess), null));
        }
    }
    /* final int PRIME = X; */
    {
        if (!isEmpty) {
            statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode())));
        }
    }
    /* int result = ... */
    {
        final JCExpression init;
        if (callSuper) {
            /* ... super.hashCode(); */
            init = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("hashCode")), List.<JCExpression>nil());
        } else {
            /* ... 1; */
            init = maker.Literal(1);
        }
        statements.append(maker.VarDef(maker.Modifiers(isEmpty && !cacheHashCode ? finalFlag : 0L), resultName, maker.TypeIdent(CTC_INT), init));
    }
    for (Included<JavacNode, EqualsAndHashCode.Include> member : members) {
        JavacNode memberNode = member.getNode();
        JCExpression fType = unnotate(getFieldType(memberNode, fieldAccess));
        boolean isMethod = memberNode.getKind() == Kind.METHOD;
        JCExpression fieldAccessor = isMethod ? createMethodAccessor(maker, memberNode) : createFieldAccessor(maker, memberNode, fieldAccess);
        if (fType instanceof JCPrimitiveTypeTree) {
            switch(((JCPrimitiveTypeTree) fType).getPrimitiveTypeKind()) {
                case BOOLEAN:
                    /* this.fieldName ? X : Y */
                    statements.append(createResultCalculation(typeNode, maker.Parens(maker.Conditional(fieldAccessor, maker.Literal(HandlerUtil.primeForTrue()), maker.Literal(HandlerUtil.primeForFalse())))));
                    break;
                case LONG:
                    {
                        Name dollarFieldName = memberNode.toName((isMethod ? "$$" : "$") + memberNode.getName());
                        statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, maker.TypeIdent(CTC_LONG), fieldAccessor));
                        statements.append(createResultCalculation(typeNode, longToIntForHashCode(maker, maker.Ident(dollarFieldName), maker.Ident(dollarFieldName))));
                    }
                    break;
                case FLOAT:
                    /* Float.floatToIntBits(this.fieldName) */
                    statements.append(createResultCalculation(typeNode, maker.Apply(List.<JCExpression>nil(), genJavaLangTypeRef(typeNode, "Float", "floatToIntBits"), List.of(fieldAccessor))));
                    break;
                case DOUBLE:
                    {
                        /* longToIntForHashCode(Double.doubleToLongBits(this.fieldName)) */
                        Name dollarFieldName = memberNode.toName((isMethod ? "$$" : "$") + memberNode.getName());
                        JCExpression init = maker.Apply(List.<JCExpression>nil(), genJavaLangTypeRef(typeNode, "Double", "doubleToLongBits"), List.of(fieldAccessor));
                        statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, maker.TypeIdent(CTC_LONG), init));
                        statements.append(createResultCalculation(typeNode, longToIntForHashCode(maker, maker.Ident(dollarFieldName), maker.Ident(dollarFieldName))));
                    }
                    break;
                default:
                case BYTE:
                case SHORT:
                case INT:
                case CHAR:
                    /* just the field */
                    statements.append(createResultCalculation(typeNode, fieldAccessor));
                    break;
            }
        } else if (fType instanceof JCArrayTypeTree) {
            JCArrayTypeTree array = (JCArrayTypeTree) fType;
            /* java.util.Arrays.deepHashCode(this.fieldName) //use just hashCode() for primitive arrays. */
            boolean multiDim = unnotate(array.elemtype) instanceof JCArrayTypeTree;
            boolean primitiveArray = unnotate(array.elemtype) instanceof JCPrimitiveTypeTree;
            boolean useDeepHC = multiDim || !primitiveArray;
            JCExpression hcMethod = chainDots(typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode");
            statements.append(createResultCalculation(typeNode, maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(fieldAccessor))));
        } else /* objects */
        {
            /* final java.lang.Object $fieldName = this.fieldName; */
            /* ($fieldName == null ? NULL_PRIME : $fieldName.hashCode()) */
            Name dollarFieldName = memberNode.toName((isMethod ? "$$" : "$") + memberNode.getName());
            statements.append(maker.VarDef(maker.Modifiers(finalFlag), dollarFieldName, genJavaLangTypeRef(typeNode, "Object"), fieldAccessor));
            JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(dollarFieldName), typeNode.toName("hashCode")), List.<JCExpression>nil());
            JCExpression thisEqualsNull = maker.Binary(CTC_EQUAL, maker.Ident(dollarFieldName), maker.Literal(CTC_BOT, null));
            statements.append(createResultCalculation(typeNode, maker.Parens(maker.Conditional(thisEqualsNull, maker.Literal(HandlerUtil.primeForNull()), hcCall))));
        }
    }
    /* 
		 * if (result == 0) result = Integer.MIN_VALUE;
		 * this.$hashCodeCache = result;
		 * 
		 */
    {
        if (cacheHashCode) {
            statements.append(maker.If(maker.Binary(CTC_EQUAL, maker.Ident(resultName), maker.Literal(CTC_INT, 0)), maker.Exec(maker.Assign(maker.Ident(resultName), genJavaLangTypeRef(typeNode, "Integer", "MIN_VALUE"))), null));
            JCFieldAccess cacheHashCodeFieldAccess = maker.Select(maker.Ident(typeNode.toName("this")), typeNode.toName(HASH_CODE_CACHE_NAME));
            statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, maker.Ident(resultName))));
        }
    }
    /* return result; */
    {
        statements.append(maker.Return(maker.Ident(resultName)));
    }
    JCBlock body = maker.Block(0, statements.toList());
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("hashCode"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source);
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) CheckerFrameworkVersion(lombok.core.configuration.CheckerFrameworkVersion) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)

Example 64 with JavacTreeMaker

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

the class HandleEqualsAndHashCode method createCanEqual.

public JCMethodDecl createCanEqual(JavacNode typeNode, JavacNode source, List<JCAnnotation> onParam) {
    /* protected boolean canEqual(final java.lang.Object other) {
		 *     return other instanceof Outer.Inner.MyType;
		 * }
		 */
    JavacTreeMaker maker = typeNode.getTreeMaker();
    List<JCAnnotation> annsOnMethod = List.nil();
    CheckerFrameworkVersion checkerFramework = getCheckerFrameworkVersion(typeNode);
    if (checkerFramework.generatePure()) {
        annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(typeNode, CheckerFrameworkVersion.NAME__PURE), List.<JCExpression>nil()));
    }
    JCModifiers mods = maker.Modifiers(Flags.PROTECTED, annsOnMethod);
    JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN);
    Name canEqualName = typeNode.toName("canEqual");
    JCExpression objectType = genJavaLangTypeRef(typeNode, "Object");
    Name otherName = typeNode.toName("other");
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
    JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null);
    createRelevantNullableAnnotation(typeNode, param);
    List<JCVariableDecl> params = List.of(param);
    JCBlock body = maker.Block(0, List.<JCStatement>of(maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode, false)))));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) CheckerFrameworkVersion(lombok.core.configuration.CheckerFrameworkVersion) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 65 with JavacTreeMaker

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

the class HandleJacksonized method handle.

@Override
public void handle(AnnotationValues<Jacksonized> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.JACKSONIZED_FLAG_USAGE, "@Jacksonized");
    JavacNode annotatedNode = annotationNode.up();
    deleteAnnotationIfNeccessary(annotationNode, Jacksonized.class);
    JavacNode tdNode;
    if (annotatedNode.getKind() != Kind.TYPE)
        // @Jacksonized on a constructor or a static factory method.
        tdNode = annotatedNode.up();
    else
        // @Jacksonized on the class.
        tdNode = annotatedNode;
    JCClassDecl td = (JCClassDecl) tdNode.get();
    JavacNode builderAnnotationNode = findAnnotation(Builder.class, annotatedNode);
    JavacNode superBuilderAnnotationNode = findAnnotation(SuperBuilder.class, annotatedNode);
    if (builderAnnotationNode == null && superBuilderAnnotationNode == null) {
        annotationNode.addWarning("@Jacksonized requires @Builder or @SuperBuilder for it to mean anything.");
        return;
    }
    if (builderAnnotationNode != null && superBuilderAnnotationNode != null) {
        annotationNode.addError("@Jacksonized cannot process both @Builder and @SuperBuilder on the same class.");
        return;
    }
    boolean isAbstract = (td.mods.flags & Flags.ABSTRACT) != 0;
    if (isAbstract) {
        annotationNode.addError("Builders on abstract classes cannot be @Jacksonized (the builder would never be used).");
        return;
    }
    AnnotationValues<Builder> builderAnnotation = builderAnnotationNode != null ? createAnnotation(Builder.class, builderAnnotationNode) : null;
    AnnotationValues<SuperBuilder> superBuilderAnnotation = superBuilderAnnotationNode != null ? createAnnotation(SuperBuilder.class, superBuilderAnnotationNode) : null;
    String setPrefix = builderAnnotation != null ? builderAnnotation.getInstance().setterPrefix() : superBuilderAnnotation.getInstance().setterPrefix();
    String buildMethodName = builderAnnotation != null ? builderAnnotation.getInstance().buildMethodName() : superBuilderAnnotation.getInstance().buildMethodName();
    JavacTreeMaker maker = annotatedNode.getTreeMaker();
    // Now lets find the generated builder class.
    String builderClassName = getBuilderClassName(annotationNode, annotatedNode, td, builderAnnotation, maker);
    JCClassDecl builderClass = null;
    for (JCTree member : td.getMembers()) {
        if (member instanceof JCClassDecl && ((JCClassDecl) member).getSimpleName().contentEquals(builderClassName)) {
            builderClass = (JCClassDecl) member;
            break;
        }
    }
    if (builderClass == null) {
        annotationNode.addError("Could not find @(Super)Builder's generated builder class for @Jacksonized processing. If there are other compiler errors, fix them first.");
        return;
    }
    // Insert @JsonDeserialize on annotated class.
    if (hasAnnotation("com.fasterxml.jackson.databind.annotation.JsonDeserialize", tdNode)) {
        annotationNode.addError("@JsonDeserialize already exists on class. Either delete @JsonDeserialize, or remove @Jacksonized and manually configure Jackson.");
        return;
    }
    JCExpression jsonDeserializeType = chainDots(annotatedNode, "com", "fasterxml", "jackson", "databind", "annotation", "JsonDeserialize");
    JCExpression builderClassExpression = namePlusTypeParamsToTypeReference(maker, tdNode, annotationNode.toName(builderClassName), false, List.<JCTypeParameter>nil());
    JCFieldAccess builderClassReference = maker.Select(builderClassExpression, annotatedNode.toName("class"));
    JCExpression assign = maker.Assign(maker.Ident(annotationNode.toName("builder")), builderClassReference);
    JCAnnotation annotationJsonDeserialize = maker.Annotation(jsonDeserializeType, List.of(assign));
    recursiveSetGeneratedBy(annotationJsonDeserialize, annotationNode);
    td.mods.annotations = td.mods.annotations.append(annotationJsonDeserialize);
    // Copy annotations from the class to the builder class.
    List<JCAnnotation> copyableAnnotations = findJacksonAnnotationsOnClass(tdNode);
    List<JCAnnotation> copiedAnnotations = copyAnnotations(copyableAnnotations);
    for (JCAnnotation anno : copiedAnnotations) {
        recursiveSetGeneratedBy(anno, annotationNode);
    }
    builderClass.mods.annotations = builderClass.mods.annotations.appendList(copiedAnnotations);
    // Insert @JsonPOJOBuilder on the builder class.
    JCExpression jsonPOJOBuilderType = chainDots(annotatedNode, "com", "fasterxml", "jackson", "databind", "annotation", "JsonPOJOBuilder");
    JCExpression withPrefixExpr = maker.Assign(maker.Ident(annotationNode.toName("withPrefix")), maker.Literal(setPrefix));
    JCExpression buildMethodNameExpr = maker.Assign(maker.Ident(annotationNode.toName("buildMethodName")), maker.Literal(buildMethodName));
    JCAnnotation annotationJsonPOJOBuilder = maker.Annotation(jsonPOJOBuilderType, List.of(withPrefixExpr, buildMethodNameExpr));
    recursiveSetGeneratedBy(annotationJsonPOJOBuilder, annotatedNode);
    builderClass.mods.annotations = builderClass.mods.annotations.append(annotationJsonPOJOBuilder);
    // @SuperBuilder? Make it package-private!
    if (superBuilderAnnotationNode != null)
        builderClass.mods.flags = builderClass.mods.flags & ~Flags.PRIVATE;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) SuperBuilder(lombok.experimental.SuperBuilder) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) SuperBuilder(lombok.experimental.SuperBuilder) Builder(lombok.Builder) JCTree(com.sun.tools.javac.tree.JCTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

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