use of org.eclipse.jdt.core.dom.Assignment.Operator in project Main by SpartanRefactoring.
the class LocalInitializedUpdateAssignment method go.
@Override
protected ASTRewrite go(final ASTRewrite $, final VariableDeclarationFragment f, final SimpleName n, final Expression initializer, final Statement nextStatement, final TextEditGroup g) {
if (initializer == null)
return null;
final Assignment a = extract.assignment(nextStatement);
if (a == null || !wizard.eq(n, to(a)) || $FragmentAndStatement.doesUseForbiddenSiblings(f, from(a)))
return null;
final Operator o = a.getOperator();
if (o == ASSIGN)
return null;
final InfixExpression newInitializer = subject.pair(to(a), from(a)).to(op.assign2infix(o));
final InlinerWithValue i = new Inliner(n, $, g).byValue(initializer);
if (!i.canInlineinto(newInitializer) || i.replacedSize(newInitializer) - metrics.size(nextStatement, initializer) > 0)
return null;
$.replace(initializer, newInitializer, g);
i.inlineInto(newInitializer);
$.remove(nextStatement, g);
return $;
}
use of org.eclipse.jdt.core.dom.Assignment.Operator in project eclipse.jdt.ui by eclipse-jdt.
the class AccessAnalyzer method visit.
@Override
public boolean visit(Assignment node) {
Expression leftHandSide = node.getLeftHandSide();
if (!considerBinding(resolveBinding(leftHandSide), leftHandSide))
return true;
checkParent(node);
Expression rightHandSide = node.getRightHandSide();
if (!fIsFieldFinal) {
List<Expression> arguments = new ArrayList<>();
AST ast = node.getAST();
Expression receiver = getReceiver(leftHandSide);
Operator operator = node.getOperator();
if (operator != Operator.ASSIGN) {
// setter only selected. Ex. f += 10; --> setF(f + 10);
if (!fSetter.isEmpty() && fGetter.isEmpty()) {
InfixExpression argument = ast.newInfixExpression();
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
if (receiver != null)
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
invocation.arguments().add(argument);
argument.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
argument.setLeftOperand(ast.newSimpleName(leftHandSide.toString()));
Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
argument.setRightOperand(rhs);
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
fReferencingSetter = true;
return false;
}
// getter only selected. Ex. f += 10; --> f = getF() + 10;
if (fSetter.isEmpty() && !fGetter.isEmpty()) {
Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(ast.newSimpleName(leftHandSide.toString()));
InfixExpression argument = ast.newInfixExpression();
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fGetter));
if (receiver != null)
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
argument.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
argument.setLeftOperand(invocation);
Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
argument.setRightOperand(rhs);
assignment.setRightHandSide(argument);
fRewriter.replace(node, assignment, createGroupDescription(READ_ACCESS));
fReferencingGetter = true;
return false;
}
} else if (fSetter.isEmpty() && !fGetter.isEmpty()) {
// assignment operator with getter only. Ex f = 10;
// the getter only will not affect the assignment expression
rightHandSide.accept(this);
return false;
}
if (!fSetter.isEmpty()) {
// Write access.
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
fReferencingSetter = true;
if (receiver != null) {
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
}
arguments = invocation.arguments();
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
}
if (node.getOperator() == Assignment.Operator.ASSIGN) {
arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
} else if (!fGetter.isEmpty()) {
InfixExpression exp = ast.newInfixExpression();
exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
MethodInvocation getter = ast.newMethodInvocation();
getter.setName(ast.newSimpleName(fGetter));
fReferencingGetter = true;
if (receiver != null) {
getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
}
exp.setLeftOperand(getter);
Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) {
ParenthesizedExpression p = ast.newParenthesizedExpression();
p.setExpression(rhs);
rhs = p;
}
exp.setRightOperand(rhs);
arguments.add(exp);
}
}
rightHandSide.accept(this);
return false;
}
Aggregations