use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor in project AutoRefactor by JnRouvignac.
the class BooleanCleanUp method noThenReturnStatement.
private boolean noThenReturnStatement(final IfStatement visited) {
Assignment thenAssignment = ASTNodes.asExpression(visited.getThenStatement(), Assignment.class);
if (ASTNodes.hasOperator(thenAssignment, Assignment.Operator.ASSIGN) && ASTNodes.asList(visited.getElseStatement()).isEmpty() && (thenAssignment.getLeftHandSide() instanceof Name || thenAssignment.getLeftHandSide() instanceof FieldAccess || thenAssignment.getLeftHandSide() instanceof SuperFieldAccess)) {
Statement previousSibling = ASTNodes.getPreviousSibling(visited);
if (previousSibling instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment fragment = getVariableDeclarationFragment(variableDeclarationStatement, thenAssignment.getLeftHandSide());
if (fragment != null) {
VarDefinitionsUsesVisitor variableUseVisitor;
try {
variableUseVisitor = new VarDefinitionsUsesVisitor(fragment.resolveBinding(), visited.getExpression(), true);
} catch (Exception e) {
return true;
}
if (variableUseVisitor.getReads().isEmpty()) {
ITypeBinding typeBinding = variableDeclarationStatement.getType().resolveBinding();
return maybeReplace(visited, thenAssignment, typeBinding, fragment.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
Assignment elseAssignment = ASTNodes.asExpression(previousSibling, Assignment.class);
if (ASTNodes.hasOperator(elseAssignment, Assignment.Operator.ASSIGN) && ASTNodes.isSameVariable(thenAssignment.getLeftHandSide(), elseAssignment.getLeftHandSide())) {
ITypeBinding typeBinding = elseAssignment.resolveTypeBinding();
return maybeReplace(visited, thenAssignment, typeBinding, elseAssignment.getRightHandSide());
}
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor in project AutoRefactor by JnRouvignac.
the class EntrySetRatherThanKeySetAndValueSearchCleanUp method replaceEntryIterationByKeyIteration.
private void replaceEntryIterationByKeyIteration(final EnhancedForStatement enhancedFor, final Expression mapExpression, final SingleVariableDeclaration parameter, final List<MethodInvocation> getValueMis) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.EntrySetRatherThanKeySetAndValueSearchCleanUp_description);
VarDefinitionsUsesVisitor keyUseVisitor = new VarDefinitionsUsesVisitor(parameter.resolveBinding(), enhancedFor.getBody(), true);
int keyUses = keyUseVisitor.getReads().size();
int insertionPoint = ASTNodes.asList(enhancedFor.getBody()).get(0).getStartPosition() - 1;
// $NON-NLS-1$ //$NON-NLS-2$
String entryVar = new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("entry", "mapEntry");
TypeNameDecider typeNameDecider = new TypeNameDecider(parameter);
MethodInvocation getValueMi0 = getValueMis.get(0);
ITypeBinding typeBinding = getValueMi0.getExpression().resolveTypeBinding();
MethodInvocation entrySetMethod = ast.newMethodInvocation();
entrySetMethod.setExpression(ASTNodes.createMoveTarget(rewrite, mapExpression));
// $NON-NLS-1$
entrySetMethod.setName(ast.newSimpleName("entrySet"));
rewrite.set(enhancedFor, EnhancedForStatement.EXPRESSION_PROPERTY, entrySetMethod, group);
MethodInvocation getKeyMethod = ast.newMethodInvocation();
getKeyMethod.setExpression(ast.newSimpleName(entryVar));
// $NON-NLS-1$
getKeyMethod.setName(ast.newSimpleName("getKey"));
if (typeBinding != null && typeBinding.isRawType()) {
// for (Object key : map.keySet()) => for (Object key : map.entrySet())
refactorRawMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, insertionPoint, entryVar, typeNameDecider, getKeyMethod);
} else {
// for (K key : map.keySet()) => for (K key : map.entrySet())
// for (K key : map.entrySet()) => for (Map.Entry<K, V> mapEntry :
// map.entrySet())
refactorGenericMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, entryVar, typeNameDecider, getValueMi0, getKeyMethod);
}
// Replace all occurrences of map.get(key) => mapEntry.getValue()
for (MethodInvocation getValueMi : getValueMis) {
MethodInvocation getValueMethod = ast.newMethodInvocation();
getValueMethod.setExpression(ast.newSimpleName(entryVar));
// $NON-NLS-1$
getValueMethod.setName(ast.newSimpleName("getValue"));
MethodInvocation newMethodInvocation = getValueMethod;
rewrite.replace(getValueMi, newMethodInvocation, group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor in project AutoRefactor by JnRouvignac.
the class OptimizeRegExCleanUp method isRegEx.
private boolean isRegEx(ITypeBinding typeBinding, final IVariableBinding variableBinding, final int extraDimensions, final Expression initializer) {
if (ASTNodes.hasType(typeBinding, String.class.getCanonicalName()) && extraDimensions == 0 && initializer != null) {
VarDefinitionsUsesVisitor varOccurrencesVisitor = new VarDefinitionsUsesVisitor(variableBinding, initializer.getRoot(), true);
List<SimpleName> reads = varOccurrencesVisitor.getReads();
List<SimpleName> writes = varOccurrencesVisitor.getWrites();
for (SimpleName write : writes) {
if (write.getParent() instanceof Assignment && write.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY && !ASTNodes.hasOperator((Assignment) write.getParent(), Assignment.Operator.ASSIGN)) {
return false;
}
}
if (!reads.isEmpty()) {
for (SimpleName read : reads) {
if (!isRegEx(read)) {
return false;
}
}
return true;
}
}
return false;
}
use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor in project AutoRefactor by JnRouvignac.
the class ObsoleteRemoveOverriddenAssignmentCleanUp method visit.
@Override
public boolean visit(final VariableDeclarationStatement visited) {
VariableDeclarationFragment fragment = ASTNodes.getUniqueFragment(visited);
if (fragment != null && fragment.getInitializer() != null && ASTNodes.isPassiveWithoutFallingThrough(fragment.getInitializer())) {
SimpleName varName = fragment.getName();
IVariableBinding variable = fragment.resolveBinding();
Statement stmtToInspect = ASTNodes.getNextSibling(visited);
boolean isOverridden = false;
while (stmtToInspect != null) {
if (!new VarDefinitionsUsesVisitor(variable, stmtToInspect, true).getReads().isEmpty()) {
return true;
}
Assignment assignment = ASTNodes.asExpression(stmtToInspect, Assignment.class);
if (assignment != null && ASTNodes.isSameVariable(varName, assignment.getLeftHandSide())) {
if (!ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN)) {
return true;
}
isOverridden = true;
break;
}
stmtToInspect = ASTNodes.getNextSibling(stmtToInspect);
}
if (isOverridden) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveOverriddenAssignmentCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(fragment.getInitializer(), group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor in project AutoRefactor by JnRouvignac.
the class DeclarationOutsideLoopRatherThanInsideCleanUp method isEffectivelyFinalRequired.
private boolean isEffectivelyFinalRequired(final VariableDeclarationStatement declaration, final VariableDeclarationFragment fragment) {
if (Modifier.isFinal(declaration.getModifiers())) {
return true;
}
VarDefinitionsUsesVisitor visitor = new VarDefinitionsUsesVisitor(fragment);
List<SimpleName> reads = visitor.getReads();
for (SimpleName read : reads) {
ASTNode ancestor = ASTNodes.getFirstAncestorOrNull(read, AnonymousClassDeclaration.class, LambdaExpression.class);
if (ancestor != null && !ASTNodes.isParent(fragment, ancestor)) {
return true;
}
}
return false;
}
Aggregations