Search in sources :

Example 21 with JCStatement

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

the class JavacJavaUtilListSingularizer method createListCopy.

private List<JCStatement> createListCopy(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) {
    List<JCExpression> jceBlank = List.nil();
    Name thisName = builderType.toName("this");
    JCExpression argToUnmodifiable;
    {
        // new java.util.ArrayList<Generics>(this.pluralName);
        List<JCExpression> constructorArgs = List.nil();
        JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
        constructorArgs = List.<JCExpression>of(thisDotPluralName);
        JCExpression targetTypeExpr = chainDots(builderType, "java", "util", "ArrayList");
        targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
        argToUnmodifiable = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
    }
    JCStatement unmodifiableStat;
    {
        // pluralname = Collections.unmodifiableInterfaceType(-newlist-);
        JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiableList"), List.of(argToUnmodifiable));
        unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
    }
    return List.of(unmodifiableStat);
}
Also used : 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) Name(com.sun.tools.javac.util.Name)

Example 22 with JCStatement

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

the class JavacJavaUtilMapSingularizer method generateClearMethod.

private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    List<JCVariableDecl> params = List.nil();
    List<JCExpression> jceBlank = List.nil();
    JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key");
    JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear");
    JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear");
    JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank));
    JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank));
    JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null));
    JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall));
    JCStatement ifSetCallClear = maker.If(cond, clearCalls, null);
    List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
    JCBlock body = maker.Block(0, statements);
    Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 23 with JCStatement

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

the class JavacJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.

protected List<JCStatement> createJavaUtilSetMapInitialCapacitySwitchStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType, JCTree source) {
    List<JCExpression> jceBlank = List.nil();
    ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
    if (emptyCollectionMethod != null) {
        // case 0: (empty); break;
        JCStatement assignStat;
        {
            // pluralName = java.util.Collections.emptyCollectionMethod();
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", emptyCollectionMethod), 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);
    }
    if (singletonCollectionMethod != null) {
        // case 1: (singleton); break;
        JCStatement assignStat;
        {
            // !mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName.get(0));
            //  mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName$key.get(0), this.pluralName$value.get(0));
            JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
            JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + (mapMode ? "$key" : ""), "get"), List.of(zeroLiteral));
            List<JCExpression> args;
            if (mapMode) {
                JCExpression zeroLiteralClone = maker.Literal(CTC_INT, 0);
                JCExpression arg2 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + (mapMode ? "$value" : ""), "get"), List.of(zeroLiteralClone));
                args = List.of(arg, arg2);
            } else {
                args = List.of(arg);
            }
            JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", singletonCollectionMethod), 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:
        List<JCStatement> statements = createJavaUtilSimpleCreationAndFillStatements(maker, data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType, source);
        JCCase defaultCase = maker.Case(null, statements);
        cases.append(defaultCase);
    }
    JCStatement switchStat = maker.Switch(getSize(maker, builderType, mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName(), true, false), cases.toList());
    JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
    localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs(), source);
    JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null);
    return List.of(varDefStat, switchStat);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Example 24 with JCStatement

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

the class JavacJavaUtilSingularizer method createConstructBuilderVarIfNeeded.

protected JCStatement createConstructBuilderVarIfNeeded(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, JCTree source) {
    List<JCExpression> jceBlank = List.nil();
    Name v1Name = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
    Name v2Name = mapMode ? builderType.toName(data.getPluralName() + "$value") : null;
    JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
    JCExpression cond = maker.Binary(CTC_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
    thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
    JCExpression v1Type = chainDots(builderType, "java", "util", "ArrayList");
    v1Type = addTypeArgs(1, false, builderType, v1Type, data.getTypeArgs(), source);
    JCExpression constructArrayList = maker.NewClass(null, jceBlank, v1Type, jceBlank, null);
    JCStatement initV1 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
    JCStatement thenPart;
    if (mapMode) {
        thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v2Name);
        JCExpression v2Type = chainDots(builderType, "java", "util", "ArrayList");
        List<JCExpression> tArgs = data.getTypeArgs();
        if (tArgs != null && tArgs.tail != null)
            tArgs = tArgs.tail;
        else
            tArgs = List.nil();
        v2Type = addTypeArgs(1, false, builderType, v2Type, tArgs, source);
        constructArrayList = maker.NewClass(null, jceBlank, v2Type, jceBlank, null);
        JCStatement initV2 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
        thenPart = maker.Block(0, List.of(initV1, initV2));
    } else {
        thenPart = initV1;
    }
    return maker.If(cond, thenPart, null);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Example 25 with JCStatement

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

the class PrettyPrinter method visitTry.

@Override
public void visitTry(JCTry tree) {
    aPrint("try ");
    List<?> resources = readObject(tree, "resources", List.nil());
    int len = resources.length();
    switch(len) {
        case 0:
            break;
        case 1:
            print("(");
            JCVariableDecl decl = (JCVariableDecl) resources.get(0);
            flagMod = -1L & ~FINAL;
            printVarDefInline(decl);
            print(") ");
            break;
        default:
            println("(");
            indent++;
            int c = 0;
            for (Object i : resources) {
                align();
                flagMod = -1L & ~FINAL;
                printVarDefInline((JCVariableDecl) i);
                if (++c == len) {
                    print(") ");
                } else {
                    println(";", (JCTree) i);
                }
            }
            indent--;
    }
    println("{");
    indent++;
    for (JCStatement stat : tree.body.stats) print(stat);
    indent--;
    aPrint("}");
    for (JCCatch catchBlock : tree.catchers) {
        printCatch(catchBlock);
    }
    if (tree.finalizer != null) {
        println(" finally {");
        indent++;
        for (JCStatement stat : tree.finalizer.stats) print(stat);
        indent--;
        aPrint("}");
    }
    println(tree);
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)112 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)82 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)37 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)33 JCTree (com.sun.tools.javac.tree.JCTree)32 Name (com.sun.tools.javac.util.Name)32 Type (com.redhat.ceylon.model.typechecker.model.Type)26 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)26 ListBuffer (com.sun.tools.javac.util.ListBuffer)25 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)23 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)18 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)16 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)14 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)14 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)12 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)12 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)12 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)11 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)10