Search in sources :

Example 66 with JavacTreeMaker

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

the class HandleStandardException method generateMsgOnlyConstructor.

private void generateMsgOnlyConstructor(JavacNode typeNode, AccessLevel level, JavacNode source) {
    if (hasConstructor(typeNode, String.class) != MemberExistsResult.NOT_EXISTS)
        return;
    JavacTreeMaker maker = typeNode.getTreeMaker();
    List<JCExpression> args = List.<JCExpression>of(maker.Ident(typeNode.toName("message")), maker.Literal(CTC_BOT, null));
    JCStatement thisCall = maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("this")), args));
    JCMethodDecl constr = createConstructor(level, typeNode, true, false, source, List.of(thisCall));
    injectMethod(typeNode, constr);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker)

Example 67 with JavacTreeMaker

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

the class HandleStandardException method createConstructor.

@SuppressWarnings("deprecation")
private static JCMethodDecl createConstructor(AccessLevel level, JavacNode typeNode, boolean msgParam, boolean causeParam, JavacNode source, List<JCStatement> statements) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    boolean addConstructorProperties;
    if ((!msgParam && !causeParam) || isLocalType(typeNode) || !LombokOptionsFactory.getDelombokOptions(typeNode.getContext()).getFormatPreferences().generateConstructorProperties()) {
        addConstructorProperties = false;
    } else {
        Boolean v = typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_ADD_CONSTRUCTOR_PROPERTIES);
        addConstructorProperties = v != null ? v.booleanValue() : Boolean.FALSE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
    }
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    if (msgParam) {
        Name fieldName = typeNode.toName("message");
        long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
        JCExpression pType = genJavaLangTypeRef(typeNode, "String");
        JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), fieldName, pType, null);
        params.append(param);
    }
    if (causeParam) {
        Name fieldName = typeNode.toName("cause");
        long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
        JCExpression pType = genJavaLangTypeRef(typeNode, "Throwable");
        JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), fieldName, pType, null);
        params.append(param);
    }
    JCModifiers mods = maker.Modifiers(toJavacModifier(level), List.<JCAnnotation>nil());
    if (addConstructorProperties)
        addConstructorProperties(mods, typeNode, msgParam, causeParam);
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, statements), null), source);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer) Name(com.sun.tools.javac.util.Name)

Example 68 with JavacTreeMaker

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

the class HandleStandardException method generateNoArgsConstructor.

private void generateNoArgsConstructor(JavacNode typeNode, AccessLevel level, JavacNode source) {
    if (hasConstructor(typeNode) != MemberExistsResult.NOT_EXISTS)
        return;
    JavacTreeMaker maker = typeNode.getTreeMaker();
    List<JCExpression> args = List.<JCExpression>of(maker.Literal(CTC_BOT, null), maker.Literal(CTC_BOT, null));
    JCStatement thisCall = maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("this")), args));
    JCMethodDecl constr = createConstructor(level, typeNode, false, false, source, List.of(thisCall));
    injectMethod(typeNode, constr);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker)

Example 69 with JavacTreeMaker

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

the class HandleStandardException method generateCauseOnlyConstructor.

private void generateCauseOnlyConstructor(JavacNode typeNode, AccessLevel level, JavacNode source) {
    if (hasConstructor(typeNode, Throwable.class) != MemberExistsResult.NOT_EXISTS)
        return;
    JavacTreeMaker maker = typeNode.getTreeMaker();
    Name causeName = typeNode.toName("cause");
    JCExpression causeDotGetMessage = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(causeName), typeNode.toName("getMessage")), List.<JCExpression>nil());
    JCExpression msgExpression = maker.Conditional(maker.Binary(CTC_NOT_EQUAL, maker.Ident(causeName), maker.Literal(CTC_BOT, null)), causeDotGetMessage, maker.Literal(CTC_BOT, null));
    List<JCExpression> args = List.<JCExpression>of(msgExpression, maker.Ident(causeName));
    JCStatement thisCall = maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("this")), args));
    JCMethodDecl constr = createConstructor(level, typeNode, false, true, source, List.of(thisCall));
    injectMethod(typeNode, constr);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) Name(com.sun.tools.javac.util.Name)

Example 70 with JavacTreeMaker

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

the class HandleToString method createToString.

static JCMethodDecl createToString(JavacNode typeNode, Collection<Included<JavacNode, ToString.Include>> members, boolean includeNames, boolean callSuper, FieldAccess fieldAccess, JavacNode source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil());
    List<JCAnnotation> annsOnMethod = List.of(overrideAnnotation);
    if (getCheckerFrameworkVersion(typeNode).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 = genJavaLangTypeRef(typeNode, "String");
    boolean first = true;
    String typeName = getTypeName(typeNode);
    boolean isEnum = typeNode.isEnumType();
    String infix = ", ";
    String suffix = ")";
    String prefix;
    if (callSuper) {
        prefix = "(super=";
    } else if (members.isEmpty()) {
        prefix = isEnum ? "" : "()";
    } else if (includeNames) {
        Included<JavacNode, ToString.Include> firstMember = members.iterator().next();
        String name = firstMember.getInc() == null ? "" : firstMember.getInc().name();
        if (name.isEmpty())
            name = firstMember.getNode().getName();
        prefix = "(" + name + "=";
    } else {
        prefix = "(";
    }
    JCExpression current;
    if (!isEnum) {
        current = maker.Literal(typeName + prefix);
    } else {
        current = maker.Binary(CTC_PLUS, maker.Literal(typeName + "."), maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("this")), typeNode.toName("name")), List.<JCExpression>nil()));
        if (!prefix.isEmpty())
            current = maker.Binary(CTC_PLUS, current, maker.Literal(prefix));
    }
    if (callSuper) {
        JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil());
        current = maker.Binary(CTC_PLUS, current, callToSuper);
        first = false;
    }
    for (Included<JavacNode, ToString.Include> member : members) {
        JCExpression expr;
        JCExpression memberAccessor;
        JavacNode memberNode = member.getNode();
        if (memberNode.getKind() == Kind.METHOD) {
            memberAccessor = createMethodAccessor(maker, memberNode);
        } else {
            memberAccessor = createFieldAccessor(maker, memberNode, fieldAccess);
        }
        JCExpression memberType = getFieldType(memberNode, fieldAccess);
        // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option.
        @SuppressWarnings("unused") boolean fieldIsPrimitive = memberType instanceof JCPrimitiveTypeTree;
        boolean fieldIsPrimitiveArray = memberType instanceof JCArrayTypeTree && ((JCArrayTypeTree) memberType).elemtype instanceof JCPrimitiveTypeTree;
        boolean fieldIsObjectArray = !fieldIsPrimitiveArray && memberType instanceof JCArrayTypeTree;
        if (fieldIsPrimitiveArray || fieldIsObjectArray) {
            JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString");
            expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(memberAccessor));
        } else
            expr = memberAccessor;
        if (first) {
            current = maker.Binary(CTC_PLUS, current, expr);
            first = false;
            continue;
        }
        if (includeNames) {
            String n = member.getInc() == null ? "" : member.getInc().name();
            if (n.isEmpty())
                n = memberNode.getName();
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + n + "="));
        } else {
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix));
        }
        current = maker.Binary(CTC_PLUS, current, expr);
    }
    if (!first)
        current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix));
    JCStatement returnStatement = maker.Return(current);
    JCBlock body = maker.Block(0, List.of(returnStatement));
    JCMethodDecl methodDef = maker.MethodDef(mods, typeNode.toName("toString"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
    createRelevantNonNullAnnotation(typeNode, methodDef);
    return recursiveSetGeneratedBy(methodDef, source);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ToString(lombok.ToString) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) 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) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)

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