Search in sources :

Example 91 with JavacTreeMaker

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

the class JavacGuavaSingularizer method generateFields.

@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JavacNode source) {
    JavacTreeMaker maker = builderType.getTreeMaker();
    String simpleTypeName = getSimpleTargetTypeName(data);
    JCExpression type = JavacHandlerUtil.chainDots(builderType, "com", "google", "common", "collect", simpleTypeName, "Builder");
    type = addTypeArgs(getTypeArgumentsCount(), false, builderType, type, data.getTypeArgs(), source);
    JCVariableDecl buildField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), data.getPluralName(), type, null);
    return Collections.singletonList(injectFieldAndMarkGenerated(builderType, buildField));
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 92 with JavacTreeMaker

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

the class JavacGuavaSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JavacNode source, ListBuffer<JCStatement> statements, Name targetVariableName, String builderVariable) {
    JavacTreeMaker maker = builderType.getTreeMaker();
    List<JCExpression> jceBlank = List.nil();
    JCExpression varType = chainDotsString(builderType, data.getTargetFqn());
    int argumentsCount = getTypeArgumentsCount();
    varType = addTypeArgs(argumentsCount, false, builderType, varType, data.getTypeArgs(), source);
    JCExpression empty;
    {
        // ImmutableX.of()
        JCExpression emptyMethod = chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), "of");
        List<JCExpression> invokeTypeArgs = createTypeArgs(argumentsCount, false, builderType, data.getTypeArgs(), source);
        empty = maker.Apply(invokeTypeArgs, emptyMethod, jceBlank);
    }
    JCExpression invokeBuild;
    {
        // this.pluralName.build();
        invokeBuild = maker.Apply(jceBlank, chainDots(builderType, builderVariable, data.getPluralName().toString(), "build"), jceBlank);
    }
    JCExpression isNull;
    {
        // this.pluralName == null
        isNull = maker.Binary(CTC_EQUAL, maker.Select(maker.Ident(builderType.toName(builderVariable)), data.getPluralName()), maker.Literal(CTC_BOT, null));
    }
    // this.pluralName == null ? ImmutableX.of() : this.pluralName.build()
    JCExpression init = maker.Conditional(isNull, empty, invokeBuild);
    JCStatement jcs = maker.VarDef(maker.Modifiers(0L), data.getPluralName(), varType, init);
    statements.append(jcs);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 93 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, JavacNode 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 94 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, JavacNode source, ListBuffer<JCStatement> statements, Name targetVariableName, String builderVariable) {
    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, builderVariable, 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, builderVariable);
        JCCase defaultCase = maker.Case(null, defStats);
        cases.append(defaultCase);
    }
    JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true, false, builderVariable), cases.toList());
    JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
    localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
    JCStatement varDefStat = maker.VarDef(maker.Modifiers(0L), 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)

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