Search in sources :

Example 76 with JCStatement

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

the class JavacJavaUtilMapSingularizer method generateSingularMethod.

private void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source));
    Name keyName = builderType.toName(data.getSingularName().toString() + "Key");
    Name valueName = builderType.toName(data.getSingularName().toString() + "Value");
    /* this.pluralname$key.add(singularnameKey); */
    {
        JCExpression thisDotKeyFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$key", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotKeyFieldDotAdd, List.<JCExpression>of(maker.Ident(keyName)));
        statements.append(maker.Exec(invokeAdd));
    }
    /* this.pluralname$value.add(singularnameValue); */
    {
        JCExpression thisDotValueFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$value", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotValueFieldDotAdd, List.<JCExpression>of(maker.Ident(valueName)));
        statements.append(maker.Exec(invokeAdd));
    }
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    Name name = data.getSingularName();
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString()));
    JCExpression paramTypeKey = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
    JCExpression paramTypeValue = cloneParamType(1, maker, data.getTypeArgs(), builderType, source);
    JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, paramTypeKey, null);
    JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, paramTypeValue, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) 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) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 77 with JCStatement

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

the class JavacJavaUtilSingularizer method createJavaUtilSimpleCreationAndFillStatements.

protected List<JCStatement> createJavaUtilSimpleCreationAndFillStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType, JCTree source) {
    List<JCExpression> jceBlank = List.nil();
    Name thisName = builderType.toName("this");
    JCStatement createStat;
    {
        // pluralName = new java.util.TargetType(initialCap);
        List<JCExpression> constructorArgs = List.nil();
        if (addInitialCapacityArg) {
            Name varName = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
            // this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
            // lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
            JCExpression lessThanCutoff = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, varName, nullGuard, true), maker.Literal(CTC_INT, 0x40000000));
            JCExpression integerMaxValue = genJavaLangTypeRef(builderType, "Integer", "MAX_VALUE");
            JCExpression sizeFormulaLeft = maker.Binary(CTC_PLUS, maker.Literal(CTC_INT, 1), getSize(maker, builderType, varName, nullGuard, true));
            JCExpression sizeFormulaRightLeft = maker.Parens(maker.Binary(CTC_MINUS, getSize(maker, builderType, varName, nullGuard, true), maker.Literal(CTC_INT, 3)));
            JCExpression sizeFormulaRight = maker.Binary(CTC_DIV, sizeFormulaRightLeft, maker.Literal(CTC_INT, 3));
            JCExpression sizeFormula = maker.Binary(CTC_PLUS, sizeFormulaLeft, sizeFormulaRight);
            constructorArgs = List.<JCExpression>of(maker.Conditional(lessThanCutoff, sizeFormula, integerMaxValue));
        }
        JCExpression targetTypeExpr = chainDots(builderType, "java", "util", targetType);
        targetTypeExpr = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
        JCExpression constructorCall = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
        if (defineVar) {
            JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
            localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs(), source);
            createStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, constructorCall);
        } else {
            createStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), constructorCall));
        }
    }
    JCStatement fillStat;
    {
        if (mapMode) {
            // for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
            Name ivar = builderType.toName("$i");
            Name keyVarName = builderType.toName(data.getPluralName() + "$key");
            JCExpression pluralnameDotPut = maker.Select(maker.Ident(data.getPluralName()), builderType.toName("put"));
            JCExpression arg1 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$key", "get"), List.<JCExpression>of(maker.Ident(ivar)));
            JCExpression arg2 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$value", "get"), List.<JCExpression>of(maker.Ident(ivar)));
            JCStatement putStatement = maker.Exec(maker.Apply(jceBlank, pluralnameDotPut, List.of(arg1, arg2)));
            JCStatement forInit = maker.VarDef(maker.Modifiers(0), ivar, maker.TypeIdent(CTC_INT), maker.Literal(CTC_INT, 0));
            JCExpression checkExpr = maker.Binary(CTC_LESS_THAN, maker.Ident(ivar), getSize(maker, builderType, keyVarName, nullGuard, true));
            JCExpression incrementExpr = maker.Unary(CTC_POSTINC, maker.Ident(ivar));
            fillStat = maker.ForLoop(List.of(forInit), checkExpr, List.of(maker.Exec(incrementExpr)), putStatement);
        } else {
            // pluralname.addAll(this.pluralname);
            JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
            fillStat = maker.Exec(maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("addAll")), List.of(thisDotPluralName)));
        }
        if (nullGuard) {
            JCExpression thisDotField = maker.Select(maker.Ident(thisName), mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName());
            JCExpression nullCheck = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
            fillStat = maker.If(nullCheck, fillStat, null);
        }
    }
    JCStatement unmodifiableStat;
    {
        // pluralname = Collections.unmodifiableInterfaceType(pluralname);
        JCExpression arg = maker.Ident(data.getPluralName());
        JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiable" + data.getTargetSimpleType()), List.of(arg));
        unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
    }
    return List.of(createStat, fillStat, unmodifiableStat);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Example 78 with JCStatement

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

the class PrettyPrinter method printCatch.

private void printCatch(JCCatch catchBlock) {
    print(" catch (");
    // ExprType1 | ExprType2 handled via JCTypeUnion.
    printVarDefInline(catchBlock.param);
    println(") {");
    indent++;
    for (JCStatement stat : catchBlock.body.stats) print(stat);
    indent--;
    aPrint("}");
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 79 with JCStatement

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

the class PrettyPrinter method visitForLoop.

@Override
public void visitForLoop(JCForLoop tree) {
    aPrint("for (");
    if (tree.init.nonEmpty()) {
        // ForInit is either a StatementExpressionList or a LocalVariableDeclaration
        if (tree.init.head instanceof JCVariableDecl) {
            boolean first = true;
            int dims = 0;
            for (JCStatement i : tree.init) {
                JCVariableDecl vd = (JCVariableDecl) i;
                if (first) {
                    printVarDefInline(vd);
                    dims = dims(vd.vartype);
                } else {
                    print(", ");
                    print(vd.name);
                    int dimDiff = dims(vd.vartype) - dims;
                    for (int j = 0; j < dimDiff; j++) print("[]");
                    if (vd.init != null) {
                        print(" = ");
                        print(vd.init);
                    }
                }
                first = false;
            }
        } else {
            boolean first = true;
            for (JCStatement exprStatement : tree.init) {
                if (!first)
                    print(", ");
                first = false;
                print(((JCExpressionStatement) exprStatement).expr);
            }
        }
    }
    print("; ");
    if (tree.cond != null)
        print(tree.cond);
    print("; ");
    boolean first = true;
    for (JCExpressionStatement exprStatement : tree.step) {
        if (!first)
            print(", ");
        first = false;
        print(exprStatement.expr);
    }
    print(") ");
    print(tree.body);
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 80 with JCStatement

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

the class JavacHandlerUtil method generateNullCheck.

/**
	 * Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the
	 * variable name as message.
	 * 
	 * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
	 */
public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) {
    NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
    if (exceptionType == null)
        exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
    JCVariableDecl varDecl = (JCVariableDecl) variable.get();
    if (isPrimitive(varDecl.vartype))
        return null;
    Name fieldName = varDecl.name;
    JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType());
    JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(exceptionType.toExceptionMessage(fieldName.toString()))), null);
    JCStatement throwStatement = maker.Throw(exception);
    JCBlock throwBlock = maker.Block(0, List.of(throwStatement));
    return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), throwBlock, null);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

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