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

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

Introduction

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

Prototype

public PostfixExpression newPostfixExpression() 

Source Link

Document

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

Usage

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

License:Apache License

public static PostfixExpression newPostfixExpression(AST ast, IVariableBinding var,
        PostfixExpression.Operator op) {
    PostfixExpression expr = ast.newPostfixExpression();
    expr.setOperator(op);/*  w w  w.j  a va 2  s  . co  m*/
    expr.setOperand(newSimpleName(ast, var));
    Types.addBinding(expr, var.getType());
    return expr;
}

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

License:Open Source License

@Override
public PostfixExpression materialize(AST ast) {
    if (isMaterialized()) {
        return post;
    }//from  w w  w  . jav  a 2s  . c  o m
    post = ast.newPostfixExpression();
    post.setOperator(PostfixExpression.Operator.toOperator(op.getOp()));

    if (arg != null) {
        post.setOperand(wireAndGetExpression(arg, this, ast));
    }
    return post;
}

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./* w  w w . j  a v  a  2 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:refactorer.Refactorer.java

License:Apache License

@SuppressWarnings("unchecked")
protected void processTypeDeclaration(AbstractTypeDeclaration type) {
    if (type == null)
        return;//from w  w  w.  j  a  v  a  2s.  c  om

    List<BodyDeclaration> bodies = type.bodyDeclarations();
    for (BodyDeclaration body : bodies) {
        processBodyDeclaration(body);
    }

    if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) type;

        if (typeDeclaration.getSuperclassType() != null) {
            ITypeBinding superclass = typeDeclaration.getSuperclassType().resolveBinding();
            if (superclass != null) {
                if ("javax.media.opengl.GLCanvas".equals(superclass.getQualifiedName())) {
                    addImportIfRequired("javax.media.opengl.awt.GLCanvas");
                } else if ("javax.media.opengl.GLJPanel".equals(superclass.getQualifiedName())) {
                    addImportIfRequired("javax.media.opengl.awt.GLJPanel");
                }
            }
        }

        for (Type interfaceType : (List<Type>) typeDeclaration.superInterfaceTypes()) {
            ITypeBinding binding = interfaceType.resolveBinding();
            if (binding != null) {
                String interfaceName = binding.getQualifiedName();
                if ("javax.media.opengl.GLEventListener".equals(interfaceName)) {
                    for (int i = bodies.size() - 1; i >= 0; i--) {
                        BodyDeclaration body = bodies.get(i);
                        if (body.getNodeType() == ASTNode.METHOD_DECLARATION) {
                            MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                            if ("displayChanged".equals(methodDeclaration.getName().getFullyQualifiedName())) {
                                bodies.remove(i);
                            }
                        }
                    }

                    AST ast = typeDeclaration.getAST();
                    MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
                    methodDeclaration.setName(ast.newSimpleName("dispose"));
                    methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                    methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
                    Block block = ast.newBlock();
                    methodDeclaration.setBody(block);
                    SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration();
                    parameter.setName(ast.newSimpleName("glDrawable"));
                    parameter.setType(ast.newSimpleType(ast.newName("GLAutoDrawable")));
                    methodDeclaration.parameters().add(parameter);
                    bodies.add(methodDeclaration);

                    if ("performance.VBORenderer.GLDisplay.MyGLEventListener"
                            .equals(type.resolveBinding().getQualifiedName())) {
                        ForStatement forStatement = ast.newForStatement();

                        VariableDeclarationFragment forInitializerFragment = ast
                                .newVariableDeclarationFragment();
                        forInitializerFragment.setName(ast.newSimpleName("i"));
                        forInitializerFragment.setInitializer(ast.newNumberLiteral("0"));
                        VariableDeclarationExpression forInitializer = ast
                                .newVariableDeclarationExpression(forInitializerFragment);
                        forInitializer.setType(ast.newPrimitiveType(PrimitiveType.INT));
                        forStatement.initializers().add(forInitializer);

                        InfixExpression forExpression = ast.newInfixExpression();
                        forExpression.setLeftOperand(ast.newSimpleName("i"));
                        forExpression.setOperator(InfixExpression.Operator.LESS);
                        MethodInvocation rightOperand = ast.newMethodInvocation();
                        rightOperand.setExpression(ast.newSimpleName("eventListeners"));
                        rightOperand.setName(ast.newSimpleName("size"));
                        forExpression.setRightOperand(rightOperand);
                        forStatement.setExpression(forExpression);

                        PostfixExpression forUpdater = ast.newPostfixExpression();
                        forUpdater.setOperand(ast.newSimpleName("i"));
                        forUpdater.setOperator(PostfixExpression.Operator.INCREMENT);
                        forStatement.updaters().add(forUpdater);

                        Block forBlock = ast.newBlock();
                        MethodInvocation mi1 = ast.newMethodInvocation();
                        mi1.setName(ast.newSimpleName("dispose"));
                        mi1.arguments().add(ast.newName("glDrawable"));
                        CastExpression castExpression = ast.newCastExpression();
                        castExpression.setType(ast.newSimpleType(ast.newName("GLEventListener")));
                        MethodInvocation mi2 = ast.newMethodInvocation();
                        mi2.setExpression(ast.newName("eventListeners"));
                        mi2.setName(ast.newSimpleName("get"));
                        mi2.arguments().add(ast.newName("i"));
                        castExpression.setExpression(mi2);
                        ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
                        parenthesizedExpression.setExpression(castExpression);
                        mi1.setExpression(parenthesizedExpression);
                        forBlock.statements().add(ast.newExpressionStatement(mi1));
                        forStatement.setBody(forBlock);

                        block.statements().add(forStatement);
                    }
                } else if ("com.sun.opengl.impl.packrect.BackingStoreManager".equals(interfaceName)) {
                    AST ast = typeDeclaration.getAST();
                    MethodDeclaration newMethodDeclaration = ast.newMethodDeclaration();
                    newMethodDeclaration.setName(ast.newSimpleName("canCompact"));
                    newMethodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                    newMethodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
                    Block block = ast.newBlock();
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(ast.newBooleanLiteral(false));
                    block.statements().add(returnStatement);
                    newMethodDeclaration.setBody(block);
                    bodies.add(newMethodDeclaration);

                    if ("gov.nasa.worldwind.util.TextureAtlas.AtlasBackingStore"
                            .equals(type.resolveBinding().getQualifiedName())) {
                        for (BodyDeclaration body : (List<BodyDeclaration>) typeDeclaration
                                .bodyDeclarations()) {
                            if (body.getNodeType() == ASTNode.METHOD_DECLARATION) {
                                MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                                if ("additionFailed"
                                        .equals(methodDeclaration.getName().getFullyQualifiedName())) {
                                    methodDeclaration
                                            .setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
                                    Block body2 = methodDeclaration.getBody();
                                    if (!body2.statements().isEmpty()) {
                                        Statement firstStatement = (Statement) body2.statements().get(0);
                                        if (firstStatement.getNodeType() == ASTNode.IF_STATEMENT) {
                                            IfStatement ifStatement = (IfStatement) firstStatement;
                                            Expression expression = ifStatement.getExpression();
                                            ifStatement.setExpression(ast.newBooleanLiteral(true));
                                            ReturnStatement newReturnStatement = ast.newReturnStatement();
                                            newReturnStatement.setExpression(expression);
                                            body2.statements().clear();
                                            body2.statements().add(newReturnStatement);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}