use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method replaceClass.
private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor) {
final ASTBuilder b = ctx.getASTBuilder();
final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
if (substituteType != null) {
ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
originalInstanceCreation.setType(substituteType);
}
for (final MethodInvocation methodCall : methodCallsToRefactor) {
final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
refactorMethod(b, methodCall, copyOfMethodCall);
ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
}
for (final VariableDeclaration variableDecl : variableDecls) {
final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
if (substituteVarType != null) {
ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
}
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project AutoRefactor by JnRouvignac.
the class ASTBuilder method declareStmt.
/**
* Builds a new {@link VariableDeclarationStatement} instance.
*
* @param type
* the type of the variable being declared
* @param varName
* the name of the variable being declared
* @param initializer
* the variable initializer, can be null
* @return a new variable declaration statement
*/
public VariableDeclarationStatement declareStmt(Type type, SimpleName varName, Expression initializer) {
final VariableDeclarationFragment fragment = declareFragment(varName, initializer);
final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(fragment);
vds.setType(type);
return vds;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project AutoRefactor by JnRouvignac.
the class NoSettingRatherThanUselessSettingRefactoring method visit.
@Override
public boolean visit(VariableDeclarationStatement node) {
if (node.fragments() != null && node.fragments().size() == 1) {
final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.fragments().get(0);
if (fragment.getInitializer() != null && fragment.getInitializer().resolveConstantExpressionValue() != null) {
final IVariableBinding variable = fragment.resolveBinding();
Statement stmtToInspect = getNextSibling(node);
boolean isOverridden = false;
boolean isRead = false;
while (stmtToInspect != null && !isOverridden && !isRead) {
if (stmtToInspect instanceof ExpressionStatement) {
final Assignment assignment = asExpression(stmtToInspect, Assignment.class);
isOverridden = hasOperator(assignment, ASSIGN) && isSameVariable(fragment.getName(), assignment.getLeftHandSide());
}
isRead = !new VariableDefinitionsUsesVisitor(variable, stmtToInspect).find().getUses().isEmpty();
stmtToInspect = getNextSibling(stmtToInspect);
}
if (isOverridden && !isRead) {
ctx.getRefactorings().remove(fragment.getInitializer());
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
Aggregations