Search in sources :

Example 11 with AllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.AllocationExpression in project lombok by rzwitserloot.

the class EclipseJavaUtilListSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) {
    if (useGuavaInstead(builderType)) {
        guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName);
        return;
    }
    List<Statement> switchContents = new ArrayList<Statement>();
    /* case 0: (empty) break; */
    {
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0));
        MessageSend invoke = new MessageSend();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        invoke.selector = "emptyList".toCharArray();
        switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    /* case 1: (singleton) break; */
    {
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0));
        FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        MessageSend thisDotFieldGet0 = new MessageSend();
        thisDotFieldGet0.receiver = thisDotField;
        thisDotFieldGet0.selector = new char[] { 'g', 'e', 't' };
        thisDotFieldGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
        Expression[] args = new Expression[] { thisDotFieldGet0 };
        MessageSend invoke = new MessageSend();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        invoke.selector = "singletonList".toCharArray();
        invoke.arguments = args;
        switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    /* default: Create by passing builder field to constructor. */
    {
        switchContents.add(new CaseStatement(null, 0, 0));
        Expression argToUnmodifiable;
        /* new j.u.ArrayList<Generics>(this.pluralName); */
        {
            FieldReference thisDotPluralName = new FieldReference(data.getPluralName(), 0L);
            thisDotPluralName.receiver = new ThisReference(0, 0);
            TypeReference targetTypeExpr = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
            targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs());
            AllocationExpression constructorCall = new AllocationExpression();
            constructorCall.type = targetTypeExpr;
            constructorCall.arguments = new Expression[] { thisDotPluralName };
            argToUnmodifiable = constructorCall;
        }
        /* pluralname = Collections.unmodifiableList(-newlist-); */
        {
            MessageSend unmodInvoke = new MessageSend();
            unmodInvoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            unmodInvoke.selector = "unmodifiableList".toCharArray();
            unmodInvoke.arguments = new Expression[] { argToUnmodifiable };
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), unmodInvoke, 0));
        }
    }
    SwitchStatement switchStat = new SwitchStatement();
    switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]);
    switchStat.expression = getSize(builderType, data.getPluralName(), true);
    TypeReference localShadowerType = new QualifiedTypeReference(Eclipse.fromQualifiedName(data.getTargetFqn()), NULL_POSS);
    localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs());
    LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
    varDefStat.type = localShadowerType;
    statements.add(varDefStat);
    statements.add(switchStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 12 with AllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.AllocationExpression in project lombok by rzwitserloot.

the class EclipseJavaUtilSingularizer method createConstructBuilderVarIfNeeded.

protected Statement createConstructBuilderVarIfNeeded(SingularData data, EclipseNode builderType, boolean mapMode) {
    char[] v1Name, v2Name;
    if (mapMode) {
        String n = new String(data.getPluralName());
        v1Name = (n + "$key").toCharArray();
        v2Name = (n + "$value").toCharArray();
    } else {
        v1Name = data.getPluralName();
        v2Name = null;
    }
    FieldReference thisDotField = new FieldReference(v1Name, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
    thisDotField = new FieldReference(v1Name, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    TypeReference v1Type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
    v1Type = addTypeArgs(1, false, builderType, v1Type, data.getTypeArgs());
    AllocationExpression constructArrayList = new AllocationExpression();
    constructArrayList.type = v1Type;
    Assignment initV1 = new Assignment(thisDotField, constructArrayList, 0);
    Statement thenPart;
    if (mapMode) {
        thisDotField = new FieldReference(v2Name, 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        TypeReference v2Type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
        List<TypeReference> tArgs = data.getTypeArgs();
        if (tArgs != null && tArgs.size() > 1)
            tArgs = Collections.singletonList(tArgs.get(1));
        else
            tArgs = Collections.emptyList();
        v2Type = addTypeArgs(1, false, builderType, v2Type, tArgs);
        constructArrayList = new AllocationExpression();
        constructArrayList.type = v2Type;
        Assignment initV2 = new Assignment(thisDotField, constructArrayList, 0);
        Block b = new Block(0);
        b.statements = new Statement[] { initV1, initV2 };
        thenPart = b;
    } else {
        thenPart = initV1;
    }
    return new IfStatement(cond, thenPart, 0, 0);
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) PostfixExpression(org.eclipse.jdt.internal.compiler.ast.PostfixExpression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Block(org.eclipse.jdt.internal.compiler.ast.Block) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Aggregations

AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)12 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)9 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)8 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)7 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)7 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)6 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)6 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)6 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)6 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)6 ArrayList (java.util.ArrayList)5 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)5 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)5 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)5 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)5 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)4 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)4 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)4 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)4 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)4