Example usage for org.eclipse.jdt.internal.compiler.ast ForStatement ForStatement

List of usage examples for org.eclipse.jdt.internal.compiler.ast ForStatement ForStatement

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast ForStatement ForStatement.

Prototype

public ForStatement(Statement[] initializations, Expression condition, Statement[] increments, Statement action,
            boolean neededScope, int s, int e) 

Source Link

Usage

From source file:lombok.eclipse.handlers.singulars.EclipseJavaUtilSingularizer.java

License:Open Source License

protected List<Statement> createJavaUtilSimpleCreationAndFillStatements(SingularData data,
        EclipseNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg,
        boolean nullGuard, String targetType) {
    char[] varName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();

    Statement createStat;/*from w w  w  . j  a v a 2s  . com*/
    {
        // pluralName = new java.util.TargetType(initialCap);
        Expression[] constructorArgs = null;
        if (addInitialCapacityArg) {
            // 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
            Expression lessThanCutoff = new BinaryExpression(getSize(builderType, varName, nullGuard),
                    makeIntLiteral("0x40000000".toCharArray(), null), OperatorIds.LESS);
            FieldReference integerMaxValue = new FieldReference("MAX_VALUE".toCharArray(), 0L);
            integerMaxValue.receiver = new QualifiedNameReference(TypeConstants.JAVA_LANG_INTEGER, NULL_POSS, 0,
                    0);
            Expression sizeFormulaLeft = new BinaryExpression(makeIntLiteral(new char[] { '1' }, null),
                    getSize(builderType, varName, nullGuard), OperatorIds.PLUS);
            Expression sizeFormulaRightLeft = new BinaryExpression(getSize(builderType, varName, nullGuard),
                    makeIntLiteral(new char[] { '3' }, null), OperatorIds.MINUS);
            Expression sizeFormulaRight = new BinaryExpression(sizeFormulaRightLeft,
                    makeIntLiteral(new char[] { '3' }, null), OperatorIds.DIVIDE);
            Expression sizeFormula = new BinaryExpression(sizeFormulaLeft, sizeFormulaRight, OperatorIds.PLUS);
            Expression cond = new ConditionalExpression(lessThanCutoff, sizeFormula, integerMaxValue);
            constructorArgs = new Expression[] { cond };
        }

        TypeReference targetTypeRef = new QualifiedTypeReference(
                new char[][] { TypeConstants.JAVA, TypeConstants.UTIL, targetType.toCharArray() }, NULL_POSS);
        targetTypeRef = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeRef, data.getTypeArgs());
        AllocationExpression constructorCall = new AllocationExpression();
        constructorCall.type = targetTypeRef;
        constructorCall.arguments = constructorArgs;

        if (defineVar) {
            TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()),
                    NULL_POSS);
            localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType,
                    data.getTypeArgs());
            LocalDeclaration localShadowerDecl = new LocalDeclaration(data.getPluralName(), 0, 0);
            localShadowerDecl.type = localShadowerType;
            localShadowerDecl.initialization = constructorCall;
            createStat = localShadowerDecl;
        } else {
            createStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0);
        }
    }

    Statement 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));
            char[] iVar = new char[] { '$', 'i' };
            MessageSend pluralnameDotPut = new MessageSend();
            pluralnameDotPut.selector = new char[] { 'p', 'u', 't' };
            pluralnameDotPut.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotKey = new FieldReference(varName, 0L);
            thisDotKey.receiver = new ThisReference(0, 0);
            FieldReference thisDotValue = new FieldReference(
                    (new String(data.getPluralName()) + "$value").toCharArray(), 0L);
            thisDotValue.receiver = new ThisReference(0, 0);
            MessageSend keyArg = new MessageSend();
            keyArg.receiver = thisDotKey;
            keyArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            keyArg.selector = new char[] { 'g', 'e', 't' };
            MessageSend valueArg = new MessageSend();
            valueArg.receiver = thisDotValue;
            valueArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            valueArg.selector = new char[] { 'g', 'e', 't' };
            pluralnameDotPut.arguments = new Expression[] { keyArg, valueArg };

            LocalDeclaration forInit = new LocalDeclaration(iVar, 0, 0);
            forInit.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
            forInit.initialization = makeIntLiteral(new char[] { '0' }, null);
            Expression checkExpr = new BinaryExpression(new SingleNameReference(iVar, 0L),
                    getSize(builderType, varName, nullGuard), OperatorIds.LESS);
            Expression incrementExpr = new PostfixExpression(new SingleNameReference(iVar, 0L), IntLiteral.One,
                    OperatorIds.PLUS, 0);
            fillStat = new ForStatement(new Statement[] { forInit }, checkExpr,
                    new Statement[] { incrementExpr }, pluralnameDotPut, true, 0, 0);
        } else {
            // pluralname.addAll(this.pluralname);
            MessageSend pluralnameDotAddAll = new MessageSend();
            pluralnameDotAddAll.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' };
            pluralnameDotAddAll.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotPluralname = new FieldReference(varName, 0L);
            thisDotPluralname.receiver = new ThisReference(0, 0);
            pluralnameDotAddAll.arguments = new Expression[] { thisDotPluralname };
            fillStat = pluralnameDotAddAll;
        }

        if (nullGuard) {
            FieldReference thisDotField = new FieldReference(varName, 0L);
            thisDotField.receiver = new ThisReference(0, 0);
            Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
            fillStat = new IfStatement(cond, fillStat, 0, 0);
        }
    }

    Statement unmodifiableStat;
    {
        // pluralname = Collections.unmodifiableInterfaceType(pluralname);
        Expression arg = new SingleNameReference(data.getPluralName(), 0L);
        MessageSend invoke = new MessageSend();
        invoke.arguments = new Expression[] { arg };
        invoke.selector = ("unmodifiable" + data.getTargetSimpleType()).toCharArray();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        unmodifiableStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), invoke, 0);
    }

    return Arrays.asList(createStat, fillStat, unmodifiableStat);
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeStatementFor() {
    // ForStatement ::= 'for' '(' ForInitopt ';' Expressionopt ';' ForUpdateopt ')' Statement
    // ForStatementNoShortIf ::= 'for' '(' ForInitopt ';' Expressionopt ';' ForUpdateopt ')' StatementNoShortIf

    int length;//w ww . j  a v a 2 s  .  c  o m
    Expression cond = null;
    Statement[] inits, updates;
    boolean scope = true;

    //statements
    this.astLengthPtr--;
    Statement statement = (Statement) this.astStack[this.astPtr--];

    //updates are on the expresion stack
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) == 0) {
        updates = null;
    } else {
        this.expressionPtr -= length;
        System.arraycopy(this.expressionStack, this.expressionPtr + 1, updates = new Statement[length], 0,
                length);
    }

    if (this.expressionLengthStack[this.expressionLengthPtr--] != 0)
        cond = this.expressionStack[this.expressionPtr--];

    //inits may be on two different stacks
    if ((length = this.astLengthStack[this.astLengthPtr--]) == 0) {
        inits = null;
        scope = false;
    } else {
        if (length == -1) { //on this.expressionStack
            scope = false;
            length = this.expressionLengthStack[this.expressionLengthPtr--];
            this.expressionPtr -= length;
            System.arraycopy(this.expressionStack, this.expressionPtr + 1, inits = new Statement[length], 0,
                    length);
        } else { //on this.astStack
            this.astPtr -= length;
            System.arraycopy(this.astStack, this.astPtr + 1, inits = new Statement[length], 0, length);
        }
    }
    pushOnAstStack(new ForStatement(inits, cond, updates, statement, scope, this.intStack[this.intPtr--],
            this.endStatementPosition));
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lifting.ArrayTranslations.java

License:Open Source License

/**
 *    for(int i0=0;i0 < role.length;i0++)
 *   {/*from   ww  w  .  j a  v a2s .  c o m*/
 *      result = new Base[role.length][][];
 *      for(...){
 *         ...
 *            for(int i2=0;i2 < role[i0][i1].length;i2++){
 *                 result[i0][i1][i2] = translate(role[i0][i1][i2]);
 *             }
 *          ...
 *       }
 *   }
 * @param currentDimension counter for recursion only. invoke initially with 0.
 * @param arrayDimensions maximum of dimensions of the array
 * @return the new created ForStatement with all subcycles
 */
private ForStatement generateForStatement(int currentDimension, int arrayDimensions, AstGenerator gen) {

    Statement[] init = new Statement[1];
    char[] name = generateArrayIndexName(currentDimension);

    LocalDeclaration initializer = new LocalDeclaration(name, 0, 0);
    initializer.initialization = IntLiteral.buildIntLiteral("0".toCharArray(), 0, 0); //$NON-NLS-1$
    initializer.type = new SingleTypeReference(TypeConstants.INT, 0);
    init[0] = initializer;

    SingleNameReference condLeft = new SingleNameReference(name, 0);

    FieldReference condRight = new FieldReference(IOTConstants.LENGTH, 0);
    SingleNameReference roleNameReference = gen.singleNameReference(ROLE_ARRAY_ARG);
    decapsulationInput(roleNameReference);

    Expression lastArrayReference = roleNameReference;

    for (int idx = 0; idx < currentDimension; idx++) {
        SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx), 0);
        ArrayReference nextArray = new ArrayReference(lastArrayReference, pos);
        lastArrayReference = nextArray;
    }
    condRight.receiver = lastArrayReference;

    Expression cond = new BinaryExpression(condLeft, condRight, OperatorIds.LESS);
    Statement[] inc = new Statement[1];

    inc[0] = new PostfixExpression(new SingleNameReference(name, 0), IntLiteral.One, OperatorIds.PLUS, 0);

    Block action = new Block(0);

    // result = new Base[role.length][][];
    Assignment arrayInstantiation = generateArrayInstantiation(currentDimension + 1, arrayDimensions, gen);

    // if(r1 == null)continue;
    IfStatement ifStatement = generateIfStatement(currentDimension + 1, arrayDimensions);

    if (currentDimension < arrayDimensions - 1) {
        ForStatement innerForStatement = generateForStatement(currentDimension + 1, arrayDimensions, gen);
        action.statements = new Statement[3];
        action.statements[0] = ifStatement;
        action.statements[1] = arrayInstantiation;
        action.statements[2] = innerForStatement;
    } else {
        action.statements = new Statement[2];
        action.statements[0] = ifStatement;
        action.statements[1] = arrayInstantiation;
    }

    // for(;i0 < role.length;i0++)
    ForStatement outerForStatement = new ForStatement(init, cond, inc, action, true, 0, 0);

    return outerForStatement;
}

From source file:org.nabucco.framework.mda.template.java.extract.statement.JavaAstStatementExtractorVisitor.java

License:Open Source License

@Override
public boolean visit(ForStatement forStatement, BlockScope scope) {

    Expression condition = copy(forStatement.condition, scope);
    Statement action = copy(forStatement.action, scope);
    Statement[] initializations = copy(forStatement.initializations, scope);
    Statement[] increments = copy(forStatement.increments, scope);

    ForStatement forCopy = new ForStatement(initializations, condition, increments, action, false,
            forStatement.sourceStart, forStatement.sourceEnd);

    this.statement = forCopy;

    return false;
}