Example usage for org.eclipse.jdt.core.dom AST newArrayAccess

List of usage examples for org.eclipse.jdt.core.dom AST newArrayAccess

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom AST newArrayAccess.

Prototype

public ArrayAccess newArrayAccess() 

Source Link

Document

Creates and returns a new unparented array access expression node owned by this AST.

Usage

From source file:com.google.devtools.j2objc.translate.ASTFactory.java

License:Apache License

public static ArrayAccess newArrayAccess(AST ast, IVariableBinding array, IVariableBinding index) {
    ITypeBinding arrayType = array.getType();
    assert arrayType.isArray();
    ArrayAccess access = ast.newArrayAccess();
    access.setArray(newSimpleName(ast, array));
    access.setIndex(newSimpleName(ast, index));
    Types.addBinding(access, arrayType.getComponentType());
    return access;
}

From source file:org.jboss.forge.roaster.model.impl.expressions.ArrayAccessImpl.java

License:Open Source License

@Override
public ArrayAccess materialize(AST ast) {
    if (isMaterialized()) {
        return axx;
    }//w w  w .j ava2 s .co  m
    axx = ast.newArrayAccess();
    axx.setIndex(wireAndGetExpression(index, this, ast));
    if (target != null) {
        axx.setArray(wireAndGetExpression(target, this, ast));
    }
    return axx;
}

From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java

License:Open Source License

/** Create the body of an assignment method, which backs up the field
 *  before a new value is assigned to it.
 *
 *  @param ast The {@link AST} object.//from  w  w  w.  ja v a2  s .co  m
 *  @param state The current state of the type analyzer.
 *  @param fieldName The name of the field.
 *  @param fieldType The type of the left-hand side (with <tt>indices</tt>
 *   dimensions less than the original field type).
 *  @param indices The number of indices.
 *  @param special Whether to handle special assign operators.
 *  @return The body of the assignment method.
 */
private Block _createAssignmentBlock(AST ast, TypeAnalyzerState state, String fieldName, Type fieldType,
        int indices, boolean special) {
    Block block = ast.newBlock();

    // Test if the checkpoint object is not null.
    IfStatement ifStatement = ast.newIfStatement();
    InfixExpression testExpression = ast.newInfixExpression();

    InfixExpression condition1 = ast.newInfixExpression();
    condition1.setLeftOperand(ast.newSimpleName(CHECKPOINT_NAME));
    condition1.setOperator(InfixExpression.Operator.NOT_EQUALS);
    condition1.setRightOperand(ast.newNullLiteral());

    InfixExpression condition2 = ast.newInfixExpression();
    MethodInvocation getTimestamp = ast.newMethodInvocation();
    getTimestamp.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
    getTimestamp.setName(ast.newSimpleName("getTimestamp"));
    condition2.setLeftOperand(getTimestamp);
    condition2.setOperator(InfixExpression.Operator.GREATER);
    condition2.setRightOperand(ast.newNumberLiteral("0"));

    testExpression.setLeftOperand(condition1);
    testExpression.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
    testExpression.setRightOperand(condition2);
    ifStatement.setExpression(testExpression);

    // The "then" branch.
    Block thenBranch = ast.newBlock();

    // Method call to store old value.
    MethodInvocation recordInvocation = ast.newMethodInvocation();
    recordInvocation.setExpression(ast.newSimpleName(_getRecordName(fieldName)));
    recordInvocation.setName(ast.newSimpleName("add"));

    // If there are indices, create an integer array of those indices,
    // and add it as an argument.
    if (indices == 0) {
        recordInvocation.arguments().add(ast.newNullLiteral());
    } else {
        ArrayCreation arrayCreation = ast.newArrayCreation();
        ArrayType arrayType = ast.newArrayType(ast.newPrimitiveType(PrimitiveType.INT));
        ArrayInitializer initializer = ast.newArrayInitializer();

        for (int i = 0; i < indices; i++) {
            initializer.expressions().add(ast.newSimpleName("index" + i));
        }

        arrayCreation.setType(arrayType);
        arrayCreation.setInitializer(initializer);
        recordInvocation.arguments().add(arrayCreation);
    }

    // If there are indices, add them ("index0", "index1", ...) after the
    // field.
    Expression field = ast.newSimpleName(fieldName);

    if (indices > 0) {
        for (int i = 0; i < indices; i++) {
            ArrayAccess arrayAccess = ast.newArrayAccess();
            arrayAccess.setArray(field);
            arrayAccess.setIndex(ast.newSimpleName("index" + i));
            field = arrayAccess;
        }
    }

    // Set the field as the next argument.
    recordInvocation.arguments().add(field);

    // Get current timestamp from the checkpoint object.
    MethodInvocation timestampGetter = ast.newMethodInvocation();
    timestampGetter.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
    timestampGetter.setName(ast.newSimpleName("getTimestamp"));

    // Set the timestamp as the next argument.
    recordInvocation.arguments().add(timestampGetter);

    // The statement of the method call.
    ExpressionStatement recordStatement = ast.newExpressionStatement(recordInvocation);
    thenBranch.statements().add(recordStatement);

    ifStatement.setThenStatement(thenBranch);
    block.statements().add(ifStatement);

    // Finally, assign the new value to the field.
    Assignment assignment = ast.newAssignment();
    assignment.setLeftHandSide((Expression) ASTNode.copySubtree(ast, field));
    assignment.setRightHandSide(ast.newSimpleName("newValue"));
    assignment.setOperator(Assignment.Operator.ASSIGN);

    // Set the checkpoint object of the new value, if necessary.
    Class c;

    try {
        c = fieldType.toClass(state.getClassLoader());
    } catch (ClassNotFoundException e) {
        throw new ASTClassNotFoundException(fieldType.getName());
    }

    if (hasMethod(c, _getSetCheckpointMethodName(false), new Class[] { Checkpoint.class })
            || state.getCrossAnalyzedTypes().contains(c.getName())) {
        block.statements().add(_createSetCheckpointInvocation(ast));
    } else {
        addToLists(_fixSetCheckpoint, c.getName(), block);
    }

    // Return the result of the assignment.
    if (special && _assignOperators.containsKey(fieldType.getName())) {
        String[] operators = _assignOperators.get(fieldType.getName());

        SwitchStatement switchStatement = ast.newSwitchStatement();
        switchStatement.setExpression(ast.newSimpleName("operator"));

        boolean isPostfix = true;

        for (int i = 0; i < operators.length; i++) {
            String operator = operators[i];

            SwitchCase switchCase = ast.newSwitchCase();
            switchCase.setExpression(ast.newNumberLiteral(Integer.toString(i)));
            switchStatement.statements().add(switchCase);

            ReturnStatement returnStatement = ast.newReturnStatement();

            if (operator.equals("=")) {
                Assignment newAssignment = (Assignment) ASTNode.copySubtree(ast, assignment);
                returnStatement.setExpression(newAssignment);
            } else if (operator.equals("++") || operator.equals("--")) {
                Expression expression;

                if (isPostfix) {
                    PostfixExpression postfix = ast.newPostfixExpression();
                    postfix.setOperand((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide()));
                    postfix.setOperator(PostfixExpression.Operator.toOperator(operator));
                    expression = postfix;

                    // Produce prefix operators next time.
                    if (operator.equals("--")) {
                        isPostfix = false;
                    }
                } else {
                    PrefixExpression prefix = ast.newPrefixExpression();
                    prefix.setOperand((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide()));
                    prefix.setOperator(PrefixExpression.Operator.toOperator(operator));
                    expression = prefix;
                }

                returnStatement.setExpression(expression);
            } else {
                Assignment newAssignment = (Assignment) ASTNode.copySubtree(ast, assignment);
                newAssignment.setOperator(Assignment.Operator.toOperator(operator));
                returnStatement.setExpression(newAssignment);
            }

            switchStatement.statements().add(returnStatement);
        }

        // The default statement: just return the old value.
        // This case should not be reached.
        SwitchCase defaultCase = ast.newSwitchCase();
        defaultCase.setExpression(null);
        switchStatement.statements().add(defaultCase);

        ReturnStatement defaultReturn = ast.newReturnStatement();
        defaultReturn.setExpression((Expression) ASTNode.copySubtree(ast, assignment.getLeftHandSide()));
        switchStatement.statements().add(defaultReturn);

        block.statements().add(switchStatement);
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(assignment);
        block.statements().add(returnStatement);
    }

    return block;
}

From source file:ptolemy.backtrack.eclipse.ast.transform.AssignmentTransformer.java

License:Open Source License

/** Create a backup method that backs up an array (possibly with some given
 *  indices) in memory, and return the same array to be aliased.
 *
 *  @param ast The {@link AST} object./*from   w w w. j ava  2 s  . co m*/
 *  @param root The root of the AST.
 *  @param state The current state of the type analyzer.
 *  @param fieldName The name of the field to be backed up.
 *  @param fieldType The type of the field.
 *  @param indices The number of indices.
 *  @param isStatic Whether the field is static.
 *  @return The declaration of the backup method.
 */
private MethodDeclaration _createBackupMethod(AST ast, CompilationUnit root, TypeAnalyzerState state,
        String fieldName, Type fieldType, int indices, boolean isStatic) {
    Class currentClass = state.getCurrentClass();
    ClassLoader loader = state.getClassLoader();
    String methodName = _getBackupMethodName(fieldName);

    // Check if the method is duplicated (possibly because the source
    // program is refactored twice).
    if (_isMethodDuplicated(currentClass, methodName, fieldType, indices, isStatic, loader, false)) {
        throw new ASTDuplicatedMethodException(currentClass.getName(), methodName);
    }

    // Get the type of the new value. The return value has the same
    // type.
    for (int i = 0; i < indices; i++) {
        try {
            fieldType = fieldType.removeOneDimension();
        } catch (ClassNotFoundException e) {
            throw new ASTClassNotFoundException(fieldType);
        }
    }

    String fieldTypeName = fieldType.getName();

    MethodDeclaration method = ast.newMethodDeclaration();
    method.setName(ast.newSimpleName(methodName));
    method.setReturnType2(createType(ast, getClassName(fieldTypeName, state, root)));

    // If the field is static, add a checkpoint object argument.
    if (isStatic) {
        // Add a "$CHECKPOINT" argument.
        SingleVariableDeclaration checkpoint = ast.newSingleVariableDeclaration();
        String checkpointType = getClassName(Checkpoint.class, state, root);
        checkpoint.setType(ast.newSimpleType(createName(ast, checkpointType)));
        checkpoint.setName(ast.newSimpleName(CHECKPOINT_NAME));
        method.parameters().add(checkpoint);
    }

    // Add all the indices.
    for (int i = 0; i < indices; i++) {
        SingleVariableDeclaration index = ast.newSingleVariableDeclaration();
        index.setType(ast.newPrimitiveType(PrimitiveType.INT));
        index.setName(ast.newSimpleName("index" + i));
        method.parameters().add(index);
    }

    Block body = ast.newBlock();
    method.setBody(body);

    // The first statement: backup the whole array.
    MethodInvocation backup = ast.newMethodInvocation();
    backup.setExpression(ast.newSimpleName(_getRecordName(fieldName)));
    backup.setName(ast.newSimpleName("backup"));

    if (indices == 0) {
        backup.arguments().add(ast.newNullLiteral());
    } else {
        ArrayCreation arrayCreation = ast.newArrayCreation();
        ArrayType arrayType = ast.newArrayType(ast.newPrimitiveType(PrimitiveType.INT));
        ArrayInitializer initializer = ast.newArrayInitializer();

        for (int i = 0; i < indices; i++) {
            initializer.expressions().add(ast.newSimpleName("index" + i));
        }

        arrayCreation.setType(arrayType);
        arrayCreation.setInitializer(initializer);
        backup.arguments().add(arrayCreation);
    }

    //If there are indices, add them ("index0", "index1", ...) after the
    // field.
    Expression field = ast.newSimpleName(fieldName);

    if (indices > 0) {
        for (int i = 0; i < indices; i++) {
            ArrayAccess arrayAccess = ast.newArrayAccess();
            arrayAccess.setArray(field);
            arrayAccess.setIndex(ast.newSimpleName("index" + i));
            field = arrayAccess;
        }
    }

    // Set the field as the next argument.
    backup.arguments().add(field);

    // Get current timestamp from the checkpoint object.
    MethodInvocation timestampGetter = ast.newMethodInvocation();
    timestampGetter.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
    timestampGetter.setName(ast.newSimpleName("getTimestamp"));

    // Set the timestamp as the next argument.
    backup.arguments().add(timestampGetter);

    body.statements().add(ast.newExpressionStatement(backup));

    // The second statement: return the array.
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression((Expression) ASTNode.copySubtree(ast, field));
    body.statements().add(returnStatement);

    // If the field is static, the method is also static; the method
    // is also private.
    List modifiers = method.modifiers();
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    if (isStatic) {
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    }

    return method;
}