use of org.eclipse.jdt.core.dom.VariableDeclarationFragment 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);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method copy.
private Block copy(Statement stmtToCopy, Name varName) {
if (stmtToCopy != null && !(stmtToCopy instanceof Block)) {
final Block b = this.ctx.getAST().newBlock();
final Assignment a = asExpression(stmtToCopy, Assignment.class);
if (a != null) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(a, varName);
statements(b).add(this.ctx.getAST().newVariableDeclarationStatement(vdf));
} else {
throw new NotImplementedException(stmtToCopy);
}
return b;
}
// We should never come here if we had a Block statement, see the replace() method
throw new NotImplementedException(stmtToCopy);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class AbstractEnumCollectionReplacementRefactoring method handleVarDeclarationStatement.
private boolean handleVarDeclarationStatement(final VariableDeclarationStatement node) {
Type type = node.getType();
if (type.isParameterizedType() && isTargetType(type)) {
ParameterizedType ptype = (ParameterizedType) type;
List<Type> typeArguments = typeArguments(ptype);
if (!typeArguments.isEmpty() && typeArguments.get(0).resolveBinding().isEnum()) {
List<VariableDeclarationFragment> fragments = fragments(node);
for (VariableDeclarationFragment vdf : fragments) {
Expression initExpr = vdf.getInitializer();
if (initExpr != null) {
initExpr = removeParentheses(initExpr);
if (creates(initExpr, getImplType())) {
return replace((ClassInstanceCreation) initExpr, typeArguments.toArray(new Type[typeArguments.size()]));
}
}
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method noThenReturnStmt.
private boolean noThenReturnStmt(final IfStatement node) {
final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
final Statement previousSibling = getPreviousSibling(node);
if (previousSibling instanceof VariableDeclarationStatement) {
final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
if (vdf != null) {
final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
if (variableUseVisitor.getUses().isEmpty()) {
final ITypeBinding typeBinding = vds.getType().resolveBinding();
return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
final Assignment elseA = asExpression(previousSibling, Assignment.class);
if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
final ITypeBinding typeBinding = elseA.resolveTypeBinding();
return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method decomposeInitializer.
/**
* Decomposes an initializer into a {@link Pair} with the name of the initialized variable
* and the initializing expression.
*
* @param init
* the initializer to decompose
* @return a {@link Pair} with the name of the initialized variable and the initializing
* expression, or {@code null} if the initializer could not be decomposed
*/
public static Pair<Name, Expression> decomposeInitializer(Expression init) {
if (init instanceof VariableDeclarationExpression) {
final VariableDeclarationExpression vde = (VariableDeclarationExpression) init;
final List<VariableDeclarationFragment> fragments = fragments(vde);
if (fragments.size() == 1) {
final VariableDeclarationFragment fragment = fragments.get(0);
return Pair.of((Name) fragment.getName(), fragment.getInitializer());
}
} else if (init instanceof Assignment) {
final Assignment as = (Assignment) init;
if (hasOperator(as, ASSIGN) && as.getLeftHandSide() instanceof Name) {
return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
}
}
return Pair.empty();
}
Aggregations