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;
}
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);
}
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;
}
}
}
}
}
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();
}
Aggregations