Search in sources :

Example 11 with ExpressionStatement

use of org.eclipse.jdt.core.dom.ExpressionStatement in project flux by eclipse.

the class QuickAssistProcessor method getChangeLambdaBodyToBlockProposal.

private static boolean getChangeLambdaBodyToBlockProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    LambdaExpression lambda;
    if (covering instanceof LambdaExpression) {
        lambda = (LambdaExpression) covering;
    } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
        lambda = (LambdaExpression) covering.getParent();
    } else {
        return false;
    }
    if (!(lambda.getBody() instanceof Expression))
        return false;
    if (lambda.resolveMethodBinding() == null)
        return false;
    if (resultingCollections == null)
        return true;
    AST ast = lambda.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    Statement statementInBlockBody;
    Expression bodyExpr = (Expression) rewrite.createMoveTarget(lambda.getBody());
    if (ast.resolveWellKnownType("void").isEqualTo(lambda.resolveMethodBinding().getReturnType())) {
        //$NON-NLS-1$
        ExpressionStatement expressionStatement = ast.newExpressionStatement(bodyExpr);
        statementInBlockBody = expressionStatement;
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(bodyExpr);
        statementInBlockBody = returnStatement;
    }
    Block blockBody = ast.newBlock();
    blockBody.statements().add(statementInBlockBody);
    rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, blockBody, null);
    // add proposal
    String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_block;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_BLOCK);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 12 with ExpressionStatement

use of org.eclipse.jdt.core.dom.ExpressionStatement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getReplaceConditionalWithIfElseProposals.

private static boolean getReplaceConditionalWithIfElseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = covering;
    while (!(node instanceof ConditionalExpression) && node instanceof Expression) {
        node = node.getParent();
    }
    if (!(node instanceof ConditionalExpression)) {
        node = covering;
        while (node != null && !(node instanceof Statement)) {
            node = node.getParent();
        }
        if (node instanceof VariableDeclarationStatement) {
            node = (ASTNode) (((VariableDeclarationStatement) node).fragments().get(0));
            node = ((VariableDeclarationFragment) node).getInitializer();
        }
        if (node instanceof ExpressionStatement) {
            node = ((ExpressionStatement) node).getExpression();
            if (node instanceof Assignment) {
                node = ((Assignment) node).getRightHandSide();
            }
        }
        if (node instanceof ReturnStatement) {
            node = ((ReturnStatement) node).getExpression();
        }
    }
    if (!(node instanceof ConditionalExpression)) {
        return false;
    }
    covering = node;
    StructuralPropertyDescriptor locationInParent = covering.getLocationInParent();
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        if (covering.getParent().getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
            return false;
        }
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        ASTNode statement = covering.getParent().getParent();
        if (!(statement instanceof VariableDeclarationStatement) || statement.getLocationInParent() != Block.STATEMENTS_PROPERTY) {
            return false;
        }
    } else if (locationInParent != ReturnStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ConditionalExpression conditional = (ConditionalExpression) covering;
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new 'if' statement
    Expression expression = conditional.getExpression();
    while (expression instanceof ParenthesizedExpression) {
        expression = ((ParenthesizedExpression) expression).getExpression();
    }
    IfStatement ifStatement = ast.newIfStatement();
    ifStatement.setExpression((Expression) rewrite.createCopyTarget(expression));
    if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
        Assignment assignment = (Assignment) covering.getParent();
        Expression assignee = assignment.getLeftHandSide();
        Assignment.Operator op = assignment.getOperator();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // replace return conditional expression with if/then/else/return
        rewrite.replace(covering.getParent().getParent(), ifStatement, null);
    } else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
        ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression()));
        ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression()));
        //
        // replace return conditional expression with if/then/else/return
        rewrite.replace(conditional.getParent(), ifStatement, null);
    } else if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) covering.getParent();
        Assignment.Operator op = Assignment.Operator.ASSIGN;
        Expression assignee = frag.getName();
        ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression()));
        ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
        // clear initializer
        rewrite.set(frag, VariableDeclarationFragment.INITIALIZER_PROPERTY, null, null);
        ASTNode statement = frag.getParent();
        rewrite.getListRewrite(statement.getParent(), Block.STATEMENTS_PROPERTY).insertAfter(ifStatement, statement, null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceConditionalWithIf;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_CONDITIONAL_WITH_IF_ELSE);
    resultingCollections.add(proposal);
    return true;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 13 with ExpressionStatement

use of org.eclipse.jdt.core.dom.ExpressionStatement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getReplaceIfElseWithConditionalProposals.

private static boolean getReplaceIfElseWithConditionalProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) node;
    Statement thenStatement = getSingleStatement(ifStatement.getThenStatement());
    Statement elseStatement = getSingleStatement(ifStatement.getElseStatement());
    if (thenStatement == null || elseStatement == null) {
        return false;
    }
    Expression assigned = null;
    Expression thenExpression = null;
    Expression elseExpression = null;
    ITypeBinding exprBinding = null;
    if (thenStatement instanceof ReturnStatement && elseStatement instanceof ReturnStatement) {
        thenExpression = ((ReturnStatement) thenStatement).getExpression();
        elseExpression = ((ReturnStatement) elseStatement).getExpression();
        MethodDeclaration declaration = ASTResolving.findParentMethodDeclaration(node);
        if (declaration == null || declaration.isConstructor()) {
            return false;
        }
        exprBinding = declaration.getReturnType2().resolveBinding();
    } else if (thenStatement instanceof ExpressionStatement && elseStatement instanceof ExpressionStatement) {
        Expression inner1 = ((ExpressionStatement) thenStatement).getExpression();
        Expression inner2 = ((ExpressionStatement) elseStatement).getExpression();
        if (inner1 instanceof Assignment && inner2 instanceof Assignment) {
            Assignment assign1 = (Assignment) inner1;
            Assignment assign2 = (Assignment) inner2;
            Expression left1 = assign1.getLeftHandSide();
            Expression left2 = assign2.getLeftHandSide();
            if (left1 instanceof Name && left2 instanceof Name && assign1.getOperator() == assign2.getOperator()) {
                IBinding bind1 = ((Name) left1).resolveBinding();
                IBinding bind2 = ((Name) left2).resolveBinding();
                if (bind1 == bind2 && bind1 instanceof IVariableBinding) {
                    assigned = left1;
                    exprBinding = ((IVariableBinding) bind1).getType();
                    thenExpression = assign1.getRightHandSide();
                    elseExpression = assign2.getRightHandSide();
                }
            }
        }
    }
    if (thenExpression == null || elseExpression == null) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(ifStatement);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceIfWithConditional;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_IF_ELSE_WITH_CONDITIONAL);
    // prepare conditional expression
    ConditionalExpression conditionalExpression = ast.newConditionalExpression();
    Expression conditionCopy = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
    conditionalExpression.setExpression(conditionCopy);
    Expression thenCopy = (Expression) rewrite.createCopyTarget(thenExpression);
    Expression elseCopy = (Expression) rewrite.createCopyTarget(elseExpression);
    IJavaProject project = context.getCompilationUnit().getJavaProject();
    if (!JavaModelUtil.is50OrHigher(project)) {
        ITypeBinding thenBinding = thenExpression.resolveTypeBinding();
        ITypeBinding elseBinding = elseExpression.resolveTypeBinding();
        if (thenBinding != null && elseBinding != null && exprBinding != null && !elseBinding.isAssignmentCompatible(thenBinding)) {
            CastExpression castException = ast.newCastExpression();
            ImportRewrite importRewrite = proposal.createImportRewrite(context.getASTRoot());
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, importRewrite);
            castException.setType(importRewrite.addImport(exprBinding, ast, importRewriteContext));
            castException.setExpression(elseCopy);
            elseCopy = castException;
        }
    } else if (JavaModelUtil.is17OrHigher(project)) {
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression);
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression);
    }
    conditionalExpression.setThenExpression(thenCopy);
    conditionalExpression.setElseExpression(elseCopy);
    // replace 'if' statement with conditional expression
    if (assigned == null) {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(conditionalExpression);
        rewrite.replace(ifStatement, returnStatement, null);
    } else {
        Assignment assignment = ast.newAssignment();
        assignment.setLeftHandSide((Expression) rewrite.createCopyTarget(assigned));
        assignment.setRightHandSide(conditionalExpression);
        assignment.setOperator(((Assignment) assigned.getParent()).getOperator());
        ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
        rewrite.replace(ifStatement, expressionStatement, null);
    }
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 14 with ExpressionStatement

use of org.eclipse.jdt.core.dom.ExpressionStatement in project flux by eclipse.

the class QuickAssistProcessor method getJoinVariableProposals.

private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode parent = node.getParent();
    VariableDeclarationFragment fragment = null;
    boolean onFirstAccess = false;
    if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
        onFirstAccess = true;
        SimpleName name = (SimpleName) node;
        IBinding binding = name.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
        if (declaring instanceof VariableDeclarationFragment) {
            fragment = (VariableDeclarationFragment) declaring;
        } else {
            return false;
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) parent;
    } else {
        return false;
    }
    IVariableBinding binding = fragment.resolveBinding();
    Expression initializer = fragment.getInitializer();
    if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
        return false;
    }
    if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
        return false;
    }
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
    SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
    if (names.length <= 1 || names[0] != fragment.getName()) {
        return false;
    }
    SimpleName firstAccess = names[1];
    if (onFirstAccess) {
        if (firstAccess != node) {
            return false;
        }
    } else {
        if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return false;
        }
    }
    Assignment assignment = (Assignment) firstAccess.getParent();
    if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(assignParent);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
    proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
    if (onFirstAccess) {
        // replace assignment with variable declaration
        rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
    } else {
        // different scopes -> remove assignments, set variable initializer
        if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
            Block block = ast.newBlock();
            rewrite.replace(assignParent, block, null);
        } else {
            rewrite.remove(assignParent, null);
        }
    }
    proposal.setEndPosition(rewrite.track(fragment.getName()));
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 15 with ExpressionStatement

use of org.eclipse.jdt.core.dom.ExpressionStatement in project che by eclipse.

the class ExtractTempRefactoring method canReplace.

private static boolean canReplace(IASTFragment fragment) {
    ASTNode node = fragment.getAssociatedNode();
    ASTNode parent = node.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
        if (node.equals(vdf.getName()))
            return false;
    }
    if (isMethodParameter(node))
        return false;
    if (isThrowableInCatchBlock(node))
        return false;
    if (parent instanceof ExpressionStatement)
        return false;
    if (isLeftValue(node))
        return false;
    if (isReferringToLocalVariableFromFor((Expression) node))
        return false;
    if (isUsedInForInitializerOrUpdater((Expression) node))
        return false;
    if (parent instanceof SwitchCase)
        return false;
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)24 Expression (org.eclipse.jdt.core.dom.Expression)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)14 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 AST (org.eclipse.jdt.core.dom.AST)11 Statement (org.eclipse.jdt.core.dom.Statement)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)11 Assignment (org.eclipse.jdt.core.dom.Assignment)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 Block (org.eclipse.jdt.core.dom.Block)9 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)7 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)7