use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class MergeConditionalBlocksRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Expression firstCondition = node.getExpression();
final List<Statement> ifCode = asList(node.getThenStatement());
final List<Statement> elseCode = asList(node.getElseStatement());
if (elseCode != null && elseCode.size() == 1 && elseCode.get(0) instanceof IfStatement) {
final IfStatement subNode = (IfStatement) elseCode.get(0);
final Expression secondCondition = subNode.getExpression();
return maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getThenStatement(), subNode.getElseStatement(), true) && maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getElseStatement(), subNode.getThenStatement(), false);
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method getSwitchStructure.
private List<SwitchCaseSection> getSwitchStructure(final SwitchStatement node) {
final List<SwitchCaseSection> switchStructure = new ArrayList<SwitchCaseSection>();
SwitchCaseSection currentCase = new SwitchCaseSection();
for (final Statement stmt : statements(node)) {
if (stmt instanceof SwitchCase) {
final SwitchCase swithCase = (SwitchCase) stmt;
if (!currentCase.stmts.isEmpty()) {
switchStructure.add(currentCase);
currentCase = new SwitchCaseSection();
}
currentCase.existingCases.add(swithCase);
} else {
currentCase.stmts.add(stmt);
}
}
if (!currentCase.existingCases.isEmpty()) {
switchStructure.add(currentCase);
}
return switchStructure;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class HotSpotIntrinsicedAPIsRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final SystemArrayCopyParams params = new SystemArrayCopyParams();
collectUniqueIndex(node, params);
final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
final List<Statement> stmts = asList(node.getBody());
if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
collectLength(node.getExpression(), incrementedIdx, params);
final Assignment as = asExpression(stmts.get(0), Assignment.class);
if (hasOperator(as, ASSIGN)) {
final Expression lhs = as.getLeftHandSide();
final Expression rhs = as.getRightHandSide();
if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
final ArrayAccess aaLHS = (ArrayAccess) lhs;
final ArrayAccess aaRHS = (ArrayAccess) rhs;
params.destArrayExpr = aaLHS.getArray();
params.srcArrayExpr = aaRHS.getArray();
if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
params.destPos = calcIndex(aaLHS.getIndex(), params);
params.srcPos = calcIndex(aaRHS.getIndex(), params);
return replaceWithSystemArrayCopyCloneAll(node, params);
}
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class IfRatherThanWhileAndFallsThroughRefactoring method isEndingWithExit.
/**
* Return true if the statement falls through.
*
* @param stmt the statement
* @return true if the statement falls through.
*/
private boolean isEndingWithExit(final Statement stmt) {
final List<Statement> stmts = asList(stmt);
if (stmts.isEmpty()) {
return false;
}
final Statement lastStmt = stmts.get(stmts.size() - 1);
switch(lastStmt.getNodeType()) {
case RETURN_STATEMENT:
case THROW_STATEMENT:
return true;
case BREAK_STATEMENT:
final BreakStatement breakStmt = (BreakStatement) lastStmt;
return breakStmt.getLabel() == null;
case IF_STATEMENT:
final IfStatement ifStmt = (IfStatement) lastStmt;
final Statement thenStmt = ifStmt.getThenStatement();
final Statement elseStmt = ifStmt.getElseStatement();
return isEndingWithExit(thenStmt) && isEndingWithExit(elseStmt);
default:
return false;
}
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method replace.
private void replace(VariableAccess varDecl, VariableAccess varAccess) {
final ASTBuilder b = this.ctx.getASTBuilder();
final AST ast = b.getAST();
final ASTNode scope = varAccess.getScope();
final Name varName = varAccess.getVariableName();
final Type varType = getType(varDecl.getVariableName().getParent());
if (scope instanceof Block) {
final List<Statement> stmts = statements((Block) scope);
for (int i = 0; i < stmts.size(); i++) {
final Statement stmt = stmts.get(i);
// FIXME i=0
final Expression parentExpr = getAncestor(varName, Expression.class);
// FIXME i=0
final Statement parentStmt = getAncestor(parentExpr, Statement.class);
if (stmt.equals(parentStmt)) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
vds.setType(varType);
this.ctx.getRefactorings().replace(stmt, vds);
break;
}
}
} else if (scope instanceof EnhancedForStatement) {
final EnhancedForStatement efs = (EnhancedForStatement) scope;
final EnhancedForStatement newEfs = b.copy(efs);
newEfs.setParameter(b.copy(efs.getParameter()));
newEfs.setExpression(b.copy(efs.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(efs.getBody(), parentStmt)) {
newEfs.setBody(copy(efs.getBody(), varName));
}
this.ctx.getRefactorings().replace(efs, newEfs);
} else if (scope instanceof ForStatement) {
final ForStatement fs = (ForStatement) scope;
final ForStatement newFs = b.copy(fs);
final List<Expression> initializers = initializers(newFs);
if (initializers.size() == 1) {
final Expression init = initializers.remove(0);
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
vde.setType(varType);
initializers.add(vde);
this.ctx.getRefactorings().replace(fs, newFs);
// TODO JNR
// if (equalNotNull(fs.getBody(), parentStmt)) {
// newFs.setBody(copy(fs.getBody()));
// }
} else {
throw new NotImplementedException(scope, "for more than one initializer in for loop.");
}
} else if (scope instanceof WhileStatement) {
final WhileStatement ws = (WhileStatement) scope;
final WhileStatement newWs = ast.newWhileStatement();
newWs.setExpression(b.copy(ws.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(ws.getBody(), parentStmt)) {
newWs.setBody(copy(ws.getBody(), varName));
}
this.ctx.getRefactorings().replace(ws, newWs);
} else if (scope instanceof IfStatement) {
final IfStatement is = (IfStatement) scope;
final IfStatement newIs = ast.newIfStatement();
newIs.setExpression(b.copy(is.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(is.getThenStatement(), parentStmt)) {
newIs.setThenStatement(copy(is.getThenStatement(), varName));
if (is.getElseStatement() != null) {
newIs.setElseStatement(b.copy(is.getElseStatement()));
}
this.ctx.getRefactorings().replace(is, newIs);
} else if (equalNotNull(is.getElseStatement(), parentStmt)) {
if (is.getThenStatement() != null) {
newIs.setThenStatement(b.copy(is.getThenStatement()));
}
newIs.setElseStatement(copy(is.getElseStatement(), varName));
this.ctx.getRefactorings().replace(is, newIs);
} else {
throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is);
}
} else {
throw new NotImplementedException(scope);
}
}
Aggregations