Search in sources :

Example 21 with JavacTreeMaker

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

the class JavacJavaUtilListSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements, Name targetVariableName) {
    if (useGuavaInstead(builderType)) {
        guavaListSetSingularizer.appendBuildCode(data, builderType, source, statements, targetVariableName);
        return;
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    List<JCExpression> jceBlank = List.nil();
    ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
    /* case 0: (empty); break; */
    {
        JCStatement assignStat;
        {
            // pluralName = java.util.Collections.emptyList();
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "emptyList"), jceBlank);
            assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
        }
        JCStatement breakStat = maker.Break(null);
        JCCase emptyCase = maker.Case(maker.Literal(CTC_INT, 0), List.of(assignStat, breakStat));
        cases.append(emptyCase);
    }
    /* case 1: (singletonList); break; */
    {
        JCStatement assignStat;
        {
            // pluralName = java.util.Collections.singletonList(this.pluralName.get(0));
            JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
            JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName().toString(), "get"), List.of(zeroLiteral));
            List<JCExpression> args = List.of(arg);
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "singletonList"), args);
            assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
        }
        JCStatement breakStat = maker.Break(null);
        JCCase singletonCase = maker.Case(maker.Literal(CTC_INT, 1), List.of(assignStat, breakStat));
        cases.append(singletonCase);
    }
    /* default: Create with right size, then addAll */
    {
        List<JCStatement> defStats = createListCopy(maker, data, builderType, source);
        JCCase defaultCase = maker.Case(null, defStats);
        cases.append(defaultCase);
    }
    JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true, false), cases.toList());
    JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
    localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
    JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null);
    statements.append(varDefStat);
    statements.append(switchStat);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Example 22 with JavacTreeMaker

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

the class JavacJavaUtilMapSingularizer method generateFields.

@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JCTree source) {
    if (useGuavaInstead(builderType)) {
        return guavaMapSingularizer.generateFields(data, builderType, source);
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    JCVariableDecl buildKeyField;
    {
        JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
        type = addTypeArgs(1, false, builderType, type, data.getTypeArgs(), source);
        buildKeyField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$key"), type, null);
    }
    JCVariableDecl buildValueField;
    {
        JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
        List<JCExpression> tArgs = data.getTypeArgs();
        if (tArgs != null && tArgs.size() > 1)
            tArgs = tArgs.tail;
        else
            tArgs = List.nil();
        type = addTypeArgs(1, false, builderType, type, tArgs, source);
        buildValueField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$value"), type, null);
    }
    JavacNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
    JavacNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
    return Arrays.asList(keyFieldNode, valueFieldNode);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 23 with JavacTreeMaker

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

the class JavacHandlerUtil method addSuppressWarningsAll.

public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) {
    if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings())
        return;
    addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));
    if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
        JavacTreeMaker maker = node.getTreeMaker();
        JCExpression arg = maker.Assign(maker.Ident(node.toName("justification")), maker.Literal("generated code"));
        addAnnotation(mods, node, pos, source, context, "edu.umd.cs.findbugs.annotations.SuppressFBWarnings", arg);
    }
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 24 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 25 with JavacTreeMaker

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

the class JavacJavaUtilMapSingularizer method generateMethods.

@Override
public void generateMethods(SingularData data, JavacNode builderType, JCTree source, boolean fluent, boolean chain) {
    if (useGuavaInstead(builderType)) {
        guavaMapSingularizer.generateMethods(data, builderType, source, fluent, chain);
        return;
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    Symtab symbolTable = builderType.getSymbolTable();
    JCExpression returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    JCStatement returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generatePluralMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateClearMethod(maker, returnType, returnStatement, data, builderType, source);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

JavacTreeMaker (lombok.javac.JavacTreeMaker)39 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)33 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)18 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 Name (com.sun.tools.javac.util.Name)17 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)16 JavacNode (lombok.javac.JavacNode)15 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)13 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)13 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)10 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)9 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)8 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)5 ArrayList (java.util.ArrayList)5 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 Context (com.sun.tools.javac.util.Context)4 List (com.sun.tools.javac.util.List)4