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

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

Introduction

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

Prototype

public ParenthesizedExpression newParenthesizedExpression() 

Source Link

Document

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

Usage

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

License:Apache License

public static ParenthesizedExpression newParenthesizedExpression(AST ast, Expression expr) {
    ParenthesizedExpression result = ast.newParenthesizedExpression();
    result.setExpression(expr);// w w  w.j a  va  2 s  . c  om
    Types.addBinding(result, Types.getTypeBinding(expr));
    return result;
}

From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java

License:Open Source License

private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);//w w  w  .  j av  a2  s.co  m

    CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    unit.setPackage(getPackageDeclaration(ast, typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IDOFactory", null,
            getHomeImplImports());
    classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
    addHomeImplImport("com.idega.data.IDOFactory");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    if (this.isJDK1_5) {
        MarkerAnnotation annotation = ast.newMarkerAnnotation();
        annotation.setTypeName(ast.newSimpleName("Override"));
        methodConstructor.modifiers().add(annotation);
    }
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    if (this.isJDK1_5) {
        ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class")));
        returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name + "Home")));
        methodConstructor.setReturnType2(returnType);
    } else {
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class")));
    }
    methodConstructor.setName(ast.newSimpleName("getEntityInterfaceClass"));

    classType.bodyDeclarations().add(methodConstructor);

    Block constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    TypeLiteral typeLiteral = ast.newTypeLiteral();
    typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name)));

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(typeLiteral);
    constructorBlock.statements().add(returnStatement);

    // create() method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeImplImport("javax.ejb.CreateException");
    classType.bodyDeclarations().add(methodConstructor);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("createIDO"));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    // findByPrimarKey(Object) method
    methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("findByPrimaryKey"));
    methodConstructor.thrownExceptions().add(ast.newName("FinderException"));
    addHomeImplImport("javax.ejb.FinderException");
    classType.bodyDeclarations().add(methodConstructor);

    SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
    variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
    variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    variableDeclaration.setName(ast.newSimpleName("pk"));
    methodConstructor.parameters().add(variableDeclaration);

    constructorBlock = ast.newBlock();
    methodConstructor.setBody(constructorBlock);

    superMethodInvocation = ast.newSuperMethodInvocation();
    superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO"));
    superMethodInvocation.arguments().add(ast.newSimpleName("pk"));

    ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
    ce.setExpression(superMethodInvocation);

    returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ce);
    constructorBlock.statements().add(returnStatement);

    if (this.isLegacyEntity) {
        // createLegacy() method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("createLegacy"));
        classType.bodyDeclarations().add(methodConstructor);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        TryStatement tryStatement = ast.newTryStatement();
        constructorBlock.statements().add(tryStatement);
        Block tryBlock = ast.newBlock();
        tryStatement.setBody(tryBlock);

        MethodInvocation mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("create"));

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(mi);
        tryBlock.statements().add(returnStatement);

        CatchClause catchClause = ast.newCatchClause();
        tryStatement.catchClauses().add(catchClause);
        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("CreateException"))));
        variableDeclaration.setName(ast.newSimpleName("ce"));
        catchClause.setException(variableDeclaration);
        Block catchBlock = ast.newBlock();
        catchClause.setBody(catchBlock);

        ClassInstanceCreation cc = ast.newClassInstanceCreation();
        cc.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException")));
        mi = ast.newMethodInvocation();
        mi.setExpression(ast.newSimpleName("ce"));
        mi.setName(ast.newSimpleName("getMessage"));
        cc.arguments().add(mi);

        ThrowStatement throwStatement = ast.newThrowStatement();
        throwStatement.setExpression(cc);
        catchBlock.statements().add(throwStatement);

        // findByPrimarKey(int) method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("findByPrimaryKey"));
        methodConstructor.thrownExceptions().add(ast.newName("FinderException"));
        classType.bodyDeclarations().add(methodConstructor);

        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
        variableDeclaration.setName(ast.newSimpleName("id"));
        methodConstructor.parameters().add(variableDeclaration);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        superMethodInvocation = ast.newSuperMethodInvocation();
        superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO"));
        superMethodInvocation.arguments().add(ast.newSimpleName("id"));

        ce = ast.newCastExpression();
        ce.setType(ast.newSimpleType(ast.newSimpleName(name)));
        ce.setExpression(superMethodInvocation);

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ce);
        constructorBlock.statements().add(returnStatement);

        // findByPrimarKeyLegacy(int) method
        methodConstructor = ast.newMethodDeclaration();
        methodConstructor.setConstructor(false);
        methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
        methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
        methodConstructor.setName(ast.newSimpleName("findByPrimaryKeyLegacy"));
        methodConstructor.thrownExceptions().add(ast.newName("SQLException"));
        addHomeImplImport("java.sql.SQLException");
        classType.bodyDeclarations().add(methodConstructor);

        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
        variableDeclaration.setName(ast.newSimpleName("id"));
        methodConstructor.parameters().add(variableDeclaration);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        tryStatement = ast.newTryStatement();
        constructorBlock.statements().add(tryStatement);
        tryBlock = ast.newBlock();
        tryStatement.setBody(tryBlock);

        mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("findByPrimaryKey"));
        mi.arguments().add(ast.newSimpleName("id"));

        returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(mi);
        tryBlock.statements().add(returnStatement);

        catchClause = ast.newCatchClause();
        tryStatement.catchClauses().add(catchClause);
        variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException"))));
        variableDeclaration.setName(ast.newSimpleName("fe"));
        catchClause.setException(variableDeclaration);
        catchBlock = ast.newBlock();
        catchClause.setBody(catchBlock);

        cc = ast.newClassInstanceCreation();
        cc.setType(ast.newSimpleType(ast.newSimpleName("SQLException")));
        mi = ast.newMethodInvocation();
        mi.setExpression(ast.newSimpleName("fe"));
        mi.setName(ast.newSimpleName("getMessage"));
        cc.arguments().add(mi);

        throwStatement = ast.newThrowStatement();
        throwStatement.setExpression(cc);
        catchBlock.statements().add(throwStatement);
    }

    MethodFilter[] validFilter = { new MethodFilter(WizardConstants.EJB_CREATE_START, MethodFilter.TYPE_PREFIX),
            new MethodFilter(WizardConstants.EJB_HOME_START, MethodFilter.TYPE_PREFIX),
            new MethodFilter(WizardConstants.EJB_FIND_START, MethodFilter.TYPE_PREFIX) };
    List methods = filterMethods(getType().getMethods(), validFilter, null);
    for (Iterator iter = methods.iterator(); iter.hasNext();) {
        IMethod method = (IMethod) iter.next();
        String fullMethodName = method.getElementName();
        String methodName = cutAwayEJBSuffix(fullMethodName);
        String[] parameterNames = method.getParameterNames();

        String returnType = method.getReturnType();
        if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)
                || fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) {
            if (!(Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Collection")) != -1)
                    && !(Signature.getSimpleName(Signature.toString(method.getReturnType()))
                            .indexOf(Signature.getSimpleName("java.util.Set")) != -1)) {
                returnType = name;
            }
        }
        if (!returnType.equals(name)) {
            returnType = getReturnType(returnType);
        }

        methodConstructor = getMethodDeclaration(ast, method, methodName, returnType, getHomeImplImports(),
                false);
        classType.bodyDeclarations().add(methodConstructor);

        constructorBlock = ast.newBlock();
        methodConstructor.setBody(constructorBlock);

        constructorBlock.statements().add(getIDOCheckOutStatement(ast, getHomeImplImports()));

        if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)) {
            if (Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) {
                constructorBlock.statements().add(
                        getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements()
                        .add(getObjectReturnStatement(ast, "getEntityCollectionForPrimaryKeys", "ids"));
            } else if (Signature.getSimpleName(Signature.toString(method.getReturnType()))
                    .indexOf(Signature.getSimpleName("java.util.Set")) != -1) {
                constructorBlock.statements().add(
                        getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements()
                        .add(getObjectReturnStatement(ast, "getEntitySetForPrimaryKeys", "ids"));
            } else {
                constructorBlock.statements()
                        .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames));
                constructorBlock.statements().add(getIDOCheckInStatement(ast));
                constructorBlock.statements().add(getObjectReturnStatement(ast, "findByPrimaryKey", "pk"));
            }
        } else if (fullMethodName.startsWith(WizardConstants.EJB_HOME_START)) {
            constructorBlock.statements().add(
                    getDataCollectingStatement(ast, returnType, "theReturn", fullMethodName, parameterNames));
            constructorBlock.statements().add(getIDOCheckInStatement(ast));
            constructorBlock.statements().add(getPrimitiveReturnStatement(ast, "theReturn"));
        } else if (fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) {
            constructorBlock.statements()
                    .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames));

            ce = ast.newCastExpression();
            ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName())));
            ce.setExpression(ast.newSimpleName("entity"));

            ParenthesizedExpression pe = ast.newParenthesizedExpression();
            pe.setExpression(ce);
            MethodInvocation mi = ast.newMethodInvocation();
            mi.setExpression(pe);
            mi.setName(ast.newSimpleName("ejbPostCreate"));
            constructorBlock.statements().add(ast.newExpressionStatement(mi));

            constructorBlock.statements().add(getIDOCheckInStatement(ast));

            TryStatement tryStatement = ast.newTryStatement();
            constructorBlock.statements().add(tryStatement);
            Block tryBlock = ast.newBlock();
            tryStatement.setBody(tryBlock);

            mi = ast.newMethodInvocation();
            mi.setName(ast.newSimpleName("findByPrimaryKey"));
            mi.arguments().add(ast.newSimpleName("pk"));

            returnStatement = ast.newReturnStatement();
            returnStatement.setExpression(mi);
            tryBlock.statements().add(returnStatement);

            CatchClause catchClause = ast.newCatchClause();
            tryStatement.catchClauses().add(catchClause);
            variableDeclaration = ast.newSingleVariableDeclaration();
            variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
            variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException"))));
            variableDeclaration.setName(ast.newSimpleName("fe"));
            catchClause.setException(variableDeclaration);
            Block catchBlock = ast.newBlock();
            catchClause.setBody(catchBlock);

            ClassInstanceCreation cc = ast.newClassInstanceCreation();
            cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException")));
            addHomeImplImport("com.idega.data.IDOCreateException");
            cc.arguments().add(ast.newSimpleName(("fe")));

            ThrowStatement throwStatement = ast.newThrowStatement();
            throwStatement.setExpression(cc);
            catchBlock.statements().add(throwStatement);

            catchClause = ast.newCatchClause();
            tryStatement.catchClauses().add(catchClause);
            variableDeclaration = ast.newSingleVariableDeclaration();
            variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
            variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("Exception"))));
            variableDeclaration.setName(ast.newSimpleName("e"));
            catchClause.setException(variableDeclaration);
            catchBlock = ast.newBlock();
            catchClause.setBody(catchBlock);

            cc = ast.newClassInstanceCreation();
            cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException")));
            cc.arguments().add(ast.newSimpleName(("e")));

            throwStatement = ast.newThrowStatement();
            throwStatement.setExpression(cc);
            catchBlock.statements().add(throwStatement);
        }
    }

    writeImports(ast, unit, getHomeImplImports());
    commitChanges(iUnit, unit, document);
}

From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java

License:Open Source License

private Statement getDataCollectingStatement(AST ast, String returnType, String variableName, String methodName,
        String[] parameterNames) {
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName(variableName));
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(getType(ast, returnType));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName())));
    ce.setExpression(ast.newSimpleName("entity"));

    ParenthesizedExpression pe = ast.newParenthesizedExpression();
    pe.setExpression(ce);/*from  w w w  .j a v  a2 s. c o m*/
    MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(pe);
    mi.setName(ast.newSimpleName(methodName));
    vdf.setInitializer(mi);

    for (int i = 0; i < parameterNames.length; i++) {
        mi.arguments().add(ast.newSimpleName(parameterNames[i]));
    }

    return vds;
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateAndOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>.
 *///from   www  .  j  ava2 s  .co  m
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(numberExpression);

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.UseEqualsResolution.java

License:Open Source License

private Expression createLeftOperand(ASTRewrite rewrite, Expression leftOperand) {
    Expression leftExp = (Expression) rewrite.createMoveTarget(leftOperand);
    if (leftOperand instanceof Name || leftOperand instanceof ParenthesizedExpression) {
        return leftExp;
    }/*from  w  w w . j  av a 2 s . c  o  m*/
    final AST ast = rewrite.getAST();
    ParenthesizedExpression parExp = ast.newParenthesizedExpression();
    parExp.setExpression(leftExp);
    return parExp;
}

From source file:edu.umd.cs.findbugs.quickfix.resolution.CreateAndOddnessCheckResolution.java

License:Open Source License

/**
 * Creates the new <CODE>InfixExpression</CODE> <CODE>(x & 1) == 1</CODE>.
 *//*from   w ww.  ja  v  a2  s .c  om*/
@Override
protected InfixExpression createCorrectOddnessCheck(ASTRewrite rewrite, Expression numberExpression) {
    assert rewrite != null;
    assert numberExpression != null;

    final AST ast = rewrite.getAST();
    InfixExpression andOddnessCheck = ast.newInfixExpression();
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    InfixExpression andExpression = ast.newInfixExpression();

    andExpression.setLeftOperand((Expression) rewrite.createMoveTarget(numberExpression));
    andExpression.setOperator(AND);
    andExpression.setRightOperand(ast.newNumberLiteral("1"));
    parenthesizedExpression.setExpression(andExpression);
    andOddnessCheck.setLeftOperand(parenthesizedExpression);
    andOddnessCheck.setOperator(EQUALS);
    andOddnessCheck.setRightOperand(ast.newNumberLiteral("1"));

    return andOddnessCheck;

}

From source file:net.sf.eclipsecs.ui.quickfixes.coding.SimplifyBooleanReturnQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}//from w  ww .jav  a 2 s . c  o m
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @Override
        public boolean visit(final IfStatement node) {
            if (containsPosition(node, markerStartOffset)) {

                final Boolean isThenStatementTrue = isReturnStatementTrue(node.getThenStatement());

                if (isThenStatementTrue == null) {
                    // the AST structure of the if statement is not as expected
                    return true;
                }

                final Expression condition = removeNotFromCondition(node.getExpression());
                final boolean isNotCondition = condition != node.getExpression();

                final ReturnStatement replacement;
                if (isThenStatementTrue ^ isNotCondition) {
                    // create replacement: return condition;
                    replacement = node.getAST().newReturnStatement();
                    replacement.setExpression(copy(condition));

                } else {
                    // create replacement: return !(condition);
                    final AST ast = node.getAST();
                    replacement = ast.newReturnStatement();
                    final PrefixExpression not = ast.newPrefixExpression();
                    not.setOperator(Operator.NOT);
                    if (omitParantheses(condition)) {
                        not.setOperand(copy(condition));
                    } else {
                        final ParenthesizedExpression parentheses = ast.newParenthesizedExpression();
                        parentheses.setExpression(copy(condition));
                        not.setOperand(parentheses);
                    }
                    replacement.setExpression(not);
                }
                replace(node, replacement);

            }
            return true;
        }

        private Boolean isReturnStatementTrue(final Statement node) {
            if (node instanceof ReturnStatement) {
                final Expression expression = ((ReturnStatement) node).getExpression();
                if (expression instanceof BooleanLiteral) {
                    return ((BooleanLiteral) expression).booleanValue();
                }
            } else if (node instanceof Block) {
                // the return statement might be wrapped in a block statement
                @SuppressWarnings("unchecked")
                final List<Statement> statements = ((Block) node).statements();
                if (statements.size() > 0) {
                    return isReturnStatementTrue(statements.get(0));
                }
            }
            return null;
        }

        private Expression removeNotFromCondition(final Expression condition) {
            if (condition instanceof PrefixExpression) {
                final PrefixExpression prefix = (PrefixExpression) condition;
                if (PrefixExpression.Operator.NOT.equals(prefix.getOperator())) {
                    return prefix.getOperand();
                }
            }
            return condition;
        }

        private boolean omitParantheses(final Expression condition) {
            return OMIT_PARANETHESES_CLASSES.contains(condition.getClass());
        }

    };
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression,
        Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes,
        Collection proposals) {//from   www. j a  va  2 s.c  o m
    ITypeBinding castType = expression.getType().resolveBinding();
    if (castType == null) {
        return false;
    }
    if (paramTypes != null) {
        if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
            return false;
        }
    } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
        return false;
    }
    ITypeBinding bindingToCast = accessExpression.resolveTypeBinding();
    if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
        return false;
    }

    IMethodBinding res = Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
    if (res != null) {
        AST ast = expression.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        CastExpression newCast = ast.newCastExpression();
        newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
        newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
        ParenthesizedExpression parents = ast.newParenthesizedExpression();
        parents.setExpression(newCast);

        ASTNode node = rewrite.createCopyTarget(expression.getExpression());
        rewrite.replace(expression, node, null);
        rewrite.replace(accessExpression, parents, null);

        String label = CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, 8, image);
        proposals.add(proposal);
        return true;
    }
    return false;
}

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

License:Open Source License

@Override
public org.eclipse.jdt.core.dom.CastExpression materialize(AST ast) {
    if (isMaterialized()) {
        return cast;
    }/*from  ww  w.  j  a  v  a2 s  .  c  o  m*/
    cast = ast.newCastExpression();
    cast.setType(JDTHelper.getType(type, ast));
    if (expression != null) {
        org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression(expression, this, ast);
        ParenthesizedExpression paren = ast.newParenthesizedExpression();
        paren.setExpression(expr);
        cast.setExpression(paren);
    }
    return cast;
}

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

License:Open Source License

protected void wireArg(Argument<O, OperatorExpression<O, P>, ?> arg, AST ast) {
    Expression child = wireAndGetExpression(arg, this, ast);

    if (child.getNodeType() == ASTNode.INFIX_EXPRESSION) {
        if (!expr.getOperator().equals(((InfixExpression) child).getOperator())) {
            ParenthesizedExpression par = ast.newParenthesizedExpression();
            par.setExpression(child);/*from  w  ww.j a v  a2s.co m*/
            child = par;
        }
    }

    InfixExpression infix = expr;
    if ("MISSING".equals(infix.getLeftOperand().toString())) {
        infix.setLeftOperand(child);
    } else if ("MISSING".equals(infix.getRightOperand().toString())) {
        infix.setRightOperand(child);
    } else {
        org.eclipse.jdt.core.dom.Expression prev = infix.getRightOperand();
        InfixExpression more = ast.newInfixExpression();
        more.setOperator(infix.getOperator());
        infix.setRightOperand(more);
        prev.delete();
        more.setLeftOperand(prev);
        more.setRightOperand(child);
    }
}