Search in sources :

Example 21 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 22 with ExpressionStatement

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

the class TryWithResourceRefactoring method visit.

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
        final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
        if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
            return collapseTryStatements(node, innerTryStmt);
        }
    }
    final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
    if (previousDeclStmt == null) {
        return VISIT_SUBTREE;
    }
    final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
    final List<Statement> finallyStmts = asList(node.getFinally());
    if (previousDeclFragment != null && finallyStmts.size() >= 1) {
        final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
        nodesToRemove.add(previousDeclStmt);
        final Statement finallyStmt = finallyStmts.get(0);
        nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
        final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
        final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
        if (finallyEs != null) {
            final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        } else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
            final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
            final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
            final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Aggregations

ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)22 Expression (org.eclipse.jdt.core.dom.Expression)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)14 AST (org.eclipse.jdt.core.dom.AST)11 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)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 Statement (org.eclipse.jdt.core.dom.Statement)9 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)7 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)7 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6