Search in sources :

Example 26 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression 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)

Example 27 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project xtext-xtend by eclipse.

the class ASTFlattenerUtils method findDeclaredType.

private Type findDeclaredType(final ASTNode scope, final SimpleName simpleName) {
    final ArrayList<Type> matchesFound = CollectionLiterals.<Type>newArrayList();
    scope.accept(new ASTVisitor() {

        @Override
        public boolean visit(final VariableDeclarationFragment node) {
            boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
            if (_equals) {
                final ASTNode parentNode = node.getParent();
                boolean _matched = false;
                if (parentNode instanceof VariableDeclarationStatement) {
                    _matched = true;
                    matchesFound.add(((VariableDeclarationStatement) parentNode).getType());
                }
                if (!_matched) {
                    if (parentNode instanceof FieldDeclaration) {
                        _matched = true;
                        matchesFound.add(((FieldDeclaration) parentNode).getType());
                    }
                }
                if (!_matched) {
                    if (parentNode instanceof VariableDeclarationExpression) {
                        _matched = true;
                        matchesFound.add(((VariableDeclarationExpression) parentNode).getType());
                    }
                }
            }
            return false;
        }

        @Override
        public boolean preVisit2(final ASTNode node) {
            return matchesFound.isEmpty();
        }

        @Override
        public boolean visit(final SingleVariableDeclaration node) {
            boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
            if (_equals) {
                matchesFound.add(node.getType());
            }
            return false;
        }
    });
    return IterableExtensions.<Type>head(matchesFound);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 28 with VariableDeclarationExpression

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

the class HotSpotIntrinsicedAPIsRefactoring method collectUniqueIndex.

private void collectUniqueIndex(ForStatement node, SystemArrayCopyParams params) {
    if (initializers(node).size() != 1) {
        return;
    }
    final Expression initializer0 = initializers(node).get(0);
    if (initializer0 instanceof VariableDeclarationExpression) {
        final VariableDeclarationExpression vde = (VariableDeclarationExpression) initializer0;
        if (isPrimitive(vde, "int") && fragments(vde).size() == 1) {
            // this must be the array index
            VariableDeclarationFragment vdf = fragments(vde).get(0);
            if (vdf.getExtraDimensions() == 0) {
                params.indexStartPos = vdf.getInitializer();
                params.indexVarBinding = vdf.resolveBinding();
                return;
            }
        }
    } else if (initializer0 instanceof Assignment) {
        final Assignment as = (Assignment) initializer0;
        if (hasOperator(as, ASSIGN) && isPrimitive(as.resolveTypeBinding(), "int")) {
            // this must be the array index
            params.indexStartPos = as.getRightHandSide();
            final Expression lhs = as.getLeftHandSide();
            if (lhs instanceof SimpleName) {
                final IBinding binding = ((SimpleName) lhs).resolveBinding();
                if (binding instanceof IVariableBinding) {
                    params.indexVarBinding = (IVariableBinding) binding;
                    return;
                }
            }
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 29 with VariableDeclarationExpression

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

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the provided node.
 *
 * @param node the node for which to build a CFG.
 * @param state the blocks liveness state before current node
 * @param throwers the thrower blocks information
 * @return the blocks liveness state after current node
 */
public LivenessState buildCFG(ForStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock initBlock = getCFGBasicBlock(initializers(node), state);
    final LivenessState initLiveBlock = LivenessState.of(new CFGEdgeBuilder(initBlock));
    final CFGBasicBlock exprBlock = getCFGBasicBlock(node.getExpression(), initLiveBlock, true);
    final CFGBasicBlock updatersBlock = getCFGBasicBlock(updaters(node), new LivenessState());
    buildEdge(updatersBlock, exprBlock);
    for (Expression expression : initializers(node)) {
        if (expression instanceof VariableDeclarationExpression) {
            addDeclarations(initBlock, (VariableDeclarationExpression) expression, throwers);
        }
    }
    addVariableAccess(exprBlock, node.getExpression(), READ, throwers);
    addVariableAccesses(updatersBlock, updaters(node), WRITE, throwers);
    CFGEdgeBuilder liveBlock = new CFGEdgeBuilder(node.getExpression(), true, exprBlock);
    final LivenessState liveAfterBody = buildCFG(node.getBody(), LivenessState.of(liveBlock), throwers);
    buildEdges(liveAfterBody, updatersBlock);
    final LivenessState liveAfterStmt = LivenessState.of(new CFGEdgeBuilder(node.getExpression(), false, exprBlock));
    buildEdgesAfterBranchableStmt(node, liveAfterStmt, updatersBlock);
    return liveAfterStmt.nextStmtWillCreateNewBlock();
}
Also used : InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Aggregations

VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)29 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)22 Expression (org.eclipse.jdt.core.dom.Expression)15 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)13 AST (org.eclipse.jdt.core.dom.AST)12 Assignment (org.eclipse.jdt.core.dom.Assignment)9 Block (org.eclipse.jdt.core.dom.Block)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 Type (org.eclipse.jdt.core.dom.Type)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)6 ForStatement (org.eclipse.jdt.core.dom.ForStatement)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)6 Statement (org.eclipse.jdt.core.dom.Statement)6