Search in sources :

Example 1 with BreakStatement

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

the class EclipseJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.

protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType) {
    List<Statement> switchContents = new ArrayList<Statement>();
    char[] keyName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();
    if (emptyCollectionMethod != null) {
        // case 0: (empty); break;
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0));
        /* pluralName = java.util.Collections.emptyCollectionMethod(); */
        {
            MessageSend invoke = new MessageSend();
            invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            invoke.selector = emptyCollectionMethod.toCharArray();
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        }
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    if (singletonCollectionMethod != null) {
        // case 1: (singleton); break;
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0));
        /* !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)); */
        {
            FieldReference thisDotKey = new FieldReference(keyName, 0L);
            thisDotKey.receiver = new ThisReference(0, 0);
            MessageSend thisDotKeyGet0 = new MessageSend();
            thisDotKeyGet0.receiver = thisDotKey;
            thisDotKeyGet0.selector = new char[] { 'g', 'e', 't' };
            thisDotKeyGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
            Expression[] args;
            if (mapMode) {
                char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray();
                FieldReference thisDotValue = new FieldReference(valueName, 0L);
                thisDotValue.receiver = new ThisReference(0, 0);
                MessageSend thisDotValueGet0 = new MessageSend();
                thisDotValueGet0.receiver = thisDotValue;
                thisDotValueGet0.selector = new char[] { 'g', 'e', 't' };
                thisDotValueGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
                args = new Expression[] { thisDotKeyGet0, thisDotValueGet0 };
            } else {
                args = new Expression[] { thisDotKeyGet0 };
            }
            MessageSend invoke = new MessageSend();
            invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            invoke.selector = singletonCollectionMethod.toCharArray();
            invoke.arguments = args;
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        }
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    {
        // default:
        switchContents.add(new CaseStatement(null, 0, 0));
        switchContents.addAll(createJavaUtilSimpleCreationAndFillStatements(data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType));
    }
    SwitchStatement switchStat = new SwitchStatement();
    switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]);
    switchStat.expression = getSize(builderType, keyName, true);
    TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
    localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs());
    LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
    varDefStat.type = localShadowerType;
    return Arrays.asList(varDefStat, switchStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) 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) 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) 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) 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 2 with BreakStatement

use of org.eclipse.jdt.internal.compiler.ast.BreakStatement 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)

Aggregations

ArrayList (java.util.ArrayList)2 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)2 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)2 BreakStatement (org.eclipse.jdt.internal.compiler.ast.BreakStatement)2 CaseStatement (org.eclipse.jdt.internal.compiler.ast.CaseStatement)2 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)2 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)2 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)2 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)2 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)2 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)2 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)2 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)2 SwitchStatement (org.eclipse.jdt.internal.compiler.ast.SwitchStatement)2 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 BinaryExpression (org.eclipse.jdt.internal.compiler.ast.BinaryExpression)1 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)1 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)1 ForStatement (org.eclipse.jdt.internal.compiler.ast.ForStatement)1