use of org.eclipse.jdt.core.dom.Assignment 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;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method getVariableDeclarationFragment.
private VariableDeclarationFragment getVariableDeclarationFragment(Expression exprToReplace, Name varName) {
if (exprToReplace instanceof Assignment) {
final Assignment a = (Assignment) exprToReplace;
if (a.getLeftHandSide() instanceof SimpleName) {
final SimpleName sn = (SimpleName) a.getLeftHandSide();
if (sn.getFullyQualifiedName().equals(varName.getFullyQualifiedName())) {
final ASTBuilder b = this.ctx.getASTBuilder();
final VariableDeclarationFragment vdf = b.getAST().newVariableDeclarationFragment();
vdf.setInitializer(b.copy(a.getRightHandSide()));
vdf.setName(b.copy(sn));
return vdf;
}
}
throw new NotImplementedException(a.getLeftHandSide());
}
throw new NotImplementedException(exprToReplace);
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method isInnerExprHardToRead.
/**
* Returns whether the supplied expression is complex enough to read.
*
* @param innerExpr
* the inner expression to test for ease of read
* @param parent
* the parent node to test for ease of read
* @return true if the expressions is hard to read, false otherwise
*/
private boolean isInnerExprHardToRead(final Expression innerExpr, final ASTNode parent) {
if (parent instanceof InfixExpression) {
if (innerExpr instanceof InfixExpression) {
final InfixExpression innerIe = (InfixExpression) innerExpr;
final Operator innerOp = innerIe.getOperator();
final Operator parentOp = ((InfixExpression) parent).getOperator();
if (Operator.EQUALS.equals(parentOp) || shouldHaveParentheses(innerOp, parentOp)) {
return true;
}
return is(innerIe.getLeftOperand(), Assignment.class) || is(innerIe.getRightOperand(), Assignment.class);
}
} else if (parent instanceof ConditionalExpression) {
return innerExpr instanceof ConditionalExpression || innerExpr instanceof Assignment || innerExpr instanceof InstanceofExpression || innerExpr instanceof InfixExpression;
}
return false;
}
Aggregations