下面列出了org.eclipse.jdt.core.dom.Assignment#getOperator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void endVisit(Assignment node) {
if (skipNode(node)) {
return;
}
FlowInfo lhs = getFlowInfo(node.getLeftHandSide());
FlowInfo rhs = getFlowInfo(node.getRightHandSide());
if (lhs instanceof LocalFlowInfo) {
LocalFlowInfo llhs = (LocalFlowInfo) lhs;
llhs.setWriteAccess(fFlowContext);
if (node.getOperator() != Assignment.Operator.ASSIGN) {
GenericSequentialFlowInfo tmp = createSequential();
tmp.merge(new LocalFlowInfo(llhs, FlowInfo.READ, fFlowContext), fFlowContext);
tmp.merge(rhs, fFlowContext);
rhs = tmp;
}
}
GenericSequentialFlowInfo info = createSequential(node);
// first process right and side and then left hand side.
info.merge(rhs, fFlowContext);
info.merge(lhs, fFlowContext);
}
@Override
public void endVisit(Assignment node) {
Expression lhs= node.getLeftHandSide();
Expression rhs= node.getRightHandSide();
ConstraintVariable2 left= getConstraintVariable(lhs);
ConstraintVariable2 right= getConstraintVariable(rhs);
if (node.resolveBoxing()) {
ImmutableTypeVariable2 boxed= fTCModel.makeImmutableTypeVariable(node.resolveTypeBinding(), node);
setConstraintVariable(node, boxed);
} else {
setConstraintVariable(node, left); // type of assignement is type of 'left'
}
if (left == null || right == null)
return;
Assignment.Operator op= node.getOperator();
if (op == Assignment.Operator.PLUS_ASSIGN && (lhs.resolveTypeBinding() == node.getAST().resolveWellKnownType("java.lang.String"))) { //$NON-NLS-1$
//Special handling for automatic String conversion: do nothing; the RHS can be anything.
} else {
fTCModel.createElementEqualsConstraints(left, right);
fTCModel.createSubtypeConstraint(right, left); // left= right; --> [right] <= [left]
}
//TODO: other implicit conversions: numeric promotion, autoboxing?
}
@Override
public void endVisit(Assignment node) {
if (skipNode(node))
return;
FlowInfo lhs= getFlowInfo(node.getLeftHandSide());
FlowInfo rhs= getFlowInfo(node.getRightHandSide());
if (lhs instanceof LocalFlowInfo) {
LocalFlowInfo llhs= (LocalFlowInfo)lhs;
llhs.setWriteAccess(fFlowContext);
if (node.getOperator() != Assignment.Operator.ASSIGN) {
GenericSequentialFlowInfo tmp= createSequential();
tmp.merge(new LocalFlowInfo(llhs, FlowInfo.READ, fFlowContext), fFlowContext);
tmp.merge(rhs, fFlowContext);
rhs= tmp;
}
}
GenericSequentialFlowInfo info= createSequential(node);
// first process right and side and then left hand side.
info.merge(rhs, fFlowContext);
info.merge(lhs, fFlowContext);
}
@Override
public void write(Assignment assignment) {
Assignment.Operator operator = assignment.getOperator();
writeNode(assignment.getLeftHandSide());
copySpaceAndComments();
if (operator == Assignment.Operator.ASSIGN)
matchAndWrite("=");
else if (operator == Assignment.Operator.PLUS_ASSIGN)
matchAndWrite("+=");
else if (operator == Assignment.Operator.MINUS_ASSIGN)
matchAndWrite("-=");
else if (operator == Assignment.Operator.TIMES_ASSIGN)
matchAndWrite("*=");
else if (operator == Assignment.Operator.DIVIDE_ASSIGN)
matchAndWrite("/=");
else if (operator == Assignment.Operator.BIT_AND_ASSIGN)
matchAndWrite("&=");
else if (operator == Assignment.Operator.BIT_OR_ASSIGN)
matchAndWrite("|=");
else if (operator == Assignment.Operator.BIT_XOR_ASSIGN)
matchAndWrite("^=");
else if (operator == Assignment.Operator.REMAINDER_ASSIGN)
matchAndWrite("%=");
else if (operator == Assignment.Operator.LEFT_SHIFT_ASSIGN)
matchAndWrite("<<=");
else if (operator == Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN)
matchAndWrite(">>=");
else if (operator == Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN)
throw sourceNotSupported(">>>= operator not supported; change Java of the form 'a >>>= b' to 'a = a >>> b' instead, which is supported");
copySpaceAndComments();
writeNode(assignment.getRightHandSide());
}
@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) {
// Write access.
AST ast = node.getAST();
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
fReferencingSetter = true;
Expression receiver = getReceiver(leftHandSide);
if (receiver != null) {
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
}
List<Expression> arguments = invocation.arguments();
if (node.getOperator() == Assignment.Operator.ASSIGN) {
arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
} else {
// This is the compound assignment case: field+= 10;
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);
}
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
}
rightHandSide.accept(this);
return false;
}
@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) {
// Write access.
AST ast= node.getAST();
MethodInvocation invocation= ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
fReferencingSetter= true;
Expression receiver= getReceiver(leftHandSide);
if (receiver != null)
invocation.setExpression((Expression)fRewriter.createCopyTarget(receiver));
List<Expression> arguments= invocation.arguments();
if (node.getOperator() == Assignment.Operator.ASSIGN) {
arguments.add((Expression)fRewriter.createCopyTarget(rightHandSide));
} else {
// This is the compound assignment case: field+= 10;
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);
}
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
}
rightHandSide.accept(this);
return false;
}
/**
* Converts an assignment, postfix expression or prefix expression into an assignable equivalent expression using the getter.
*
* @param node the assignment/prefix/postfix node
* @param astRewrite the astRewrite to use
* @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist
* @param variableType the type of the variable that the result will be assigned to
* @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used
* @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter
*/
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
InfixExpression.Operator op= null;
AST ast= astRewrite.getAST();
if (isNotInBlock(node))
return null;
if (node.getNodeType() == ASTNode.ASSIGNMENT) {
Assignment assignment= ((Assignment) node);
Expression rightHandSide= assignment.getRightHandSide();
Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide);
if (assignment.getOperator() == Operator.ASSIGN) {
ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding();
copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
return copiedRightOp;
}
if (getterExpression != null) {
InfixExpression infix= ast.newInfixExpression();
infix.setLeftOperand(getterExpression);
infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
ITypeBinding infixType= infix.resolveTypeBinding();
if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
ParenthesizedExpression p= ast.newParenthesizedExpression();
p.setExpression(copiedRightOp);
copiedRightOp= p;
}
infix.setRightOperand(copiedRightOp);
return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
}
} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
PostfixExpression po= (PostfixExpression) node;
if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
op= InfixExpression.Operator.PLUS;
if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
op= InfixExpression.Operator.MINUS;
} else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
PrefixExpression pe= (PrefixExpression) node;
if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
op= InfixExpression.Operator.PLUS;
if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
op= InfixExpression.Operator.MINUS;
}
if (op != null && getterExpression != null) {
return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
}
return null;
}
private static List<ASTNode> getValueContributingModifiers(SimpleName variable)
{
List<ASTNode> allVariableModifiers = getAllVariableModifiersInParentMethod(variable);
List<ASTNode> contributingModifiers = new ArrayList<ASTNode>();
boolean noModifierInLowerScope = true;
MethodDeclaration parentMethod = AbstractLoopUtilities.findParentMethodDeclaration(variable);
// create a list of all parents of the specified variable until the root method
List<ASTNode> variableParents = new ArrayList<ASTNode>();
ASTNode currentVariableParent = variable.getParent();
while (currentVariableParent != null && currentVariableParent != parentMethod)
{
variableParents.add(currentVariableParent);
currentVariableParent = currentVariableParent.getParent();
}
variableParents.add(parentMethod);
// we traverse allVariableModifiers and build a list of nodes that will influence the final value
Iterator<ASTNode> it = allVariableModifiers.iterator();
while (it.hasNext())
{
ASTNode currentNode = it.next();
boolean currentNodeAdded = false;
// if the current node is the declaration or an assignment, the list restarts the modifiers. if it is a plus, minus, times, or divide equals, then it adds to the modifiers
if (currentNode instanceof VariableDeclaration)
{
contributingModifiers = new ArrayList<ASTNode>();
contributingModifiers.add(currentNode);
currentNodeAdded = true;
noModifierInLowerScope = true;
}
else if (currentNode instanceof Assignment)
{
Assignment assignment = (Assignment) currentNode;
Assignment.Operator operator = assignment.getOperator();
if (operator == Assignment.Operator.ASSIGN)
{
contributingModifiers = new ArrayList<ASTNode>();
contributingModifiers.add(currentNode);
currentNodeAdded = true;
noModifierInLowerScope = true;
}
else if (operator == Assignment.Operator.PLUS_ASSIGN ||
operator == Assignment.Operator.MINUS_ASSIGN ||
operator == Assignment.Operator.TIMES_ASSIGN ||
operator == Assignment.Operator.DIVIDE_ASSIGN)
{
contributingModifiers.add(currentNode);
currentNodeAdded = true;
}
}
else if (currentNode instanceof PrefixExpression || currentNode instanceof PostfixExpression)
{
contributingModifiers.add(currentNode);
currentNodeAdded = true;
}
else if (currentNode instanceof MethodInvocation)
{
MethodInvocation currentMethodInvocation = (MethodInvocation) currentNode;
AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
String currentMethodBindingKey = currentMethodInvocation.resolveMethodBinding().getMethodDeclaration().getKey();
if (bindingInformation.updateMethodValuesContains(currentMethodBindingKey))
{
contributingModifiers.add(currentNode);
currentNodeAdded = true;
}
}
// if currentNode was added, move up through it's parents until the first block or conditional parent and check if it is in the variableParents list, if not, it is in a lower scope
if (currentNodeAdded)
{
ASTNode currentNodeParent = currentNode.getParent();
while (currentNodeParent != null)
{
if ((currentNodeParent instanceof MethodDeclaration || currentNodeParent instanceof IfStatement || currentNodeParent instanceof ForStatement ||
currentNodeParent instanceof WhileStatement || currentNodeParent instanceof DoStatement || currentNodeParent instanceof EnhancedForStatement ||
currentNodeParent instanceof SwitchStatement || currentNodeParent instanceof TryStatement))
{
if (!variableParents.contains(currentNodeParent))
{
noModifierInLowerScope = false;
}
break;
}
currentNodeParent = currentNodeParent.getParent();
}
}
}
// return constructed list if all modifiers are in same or higher scope
if (noModifierInLowerScope)
{
return contributingModifiers;
}
return new ArrayList<ASTNode>();
}
private static Integer assignmentUpdateValue(Assignment assignment)
{
Integer updateValue = null;
Expression leftHandSide = assignment.getLeftHandSide();
Assignment.Operator operator = assignment.getOperator();
Expression rightHandSide = assignment.getRightHandSide();
if (operator == Assignment.Operator.PLUS_ASSIGN)
{
updateValue = AbstractLoopUtilities.getIntegerValue(rightHandSide);
}
else if (operator == Assignment.Operator.MINUS_ASSIGN)
{
Integer rightHandSideIntegerValue = AbstractLoopUtilities.getIntegerValue(rightHandSide);
if (rightHandSideIntegerValue != null)
{
updateValue = (-1) * rightHandSideIntegerValue;
}
}
else if (leftHandSide instanceof SimpleName && operator == Assignment.Operator.ASSIGN && rightHandSide instanceof InfixExpression)
{
SimpleName leftHandSideSimpleName = (SimpleName) leftHandSide;
IBinding leftHandSideBinding = leftHandSideSimpleName.resolveBinding();
InfixExpression infixExpression = (InfixExpression) rightHandSide;
InfixExpression.Operator infixOperator = infixExpression.getOperator();
Expression rightOperand = infixExpression.getRightOperand();
Expression leftOperand = infixExpression.getLeftOperand();
if (infixOperator.toString().equals("+") || infixOperator.toString().equals("-"))
{
if (leftOperand instanceof SimpleName)
{
SimpleName leftOperandSimpleName = (SimpleName) leftOperand;
IBinding leftOperandBinding = leftOperandSimpleName.resolveBinding();
if (leftOperandBinding.isEqualTo(leftHandSideBinding))
{
Integer rightOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(rightOperand);
if (infixOperator.toString().equals("+") && rightOperandIntegerValue != null)
{
updateValue = rightOperandIntegerValue;
}
else if (infixOperator.toString().equals("-") && rightOperandIntegerValue != null)
{
updateValue = (-1) * rightOperandIntegerValue;
}
}
}
else if (rightOperand instanceof SimpleName)
{
SimpleName rightOperandSimpleName = (SimpleName) rightOperand;
IBinding rightOperandBinding = rightOperandSimpleName.resolveBinding();
if (rightOperandBinding.isEqualTo(leftHandSideBinding))
{
Integer leftOperandIntegerValue = AbstractLoopUtilities.getIntegerValue(leftOperand);
if (infixOperator.toString().equals("+") && leftOperandIntegerValue != null)
{
updateValue = leftOperandIntegerValue;
}
}
}
}
}
return updateValue;
}
@Override
public void write(Assignment assignment) {
Assignment.Operator operator = assignment.getOperator();
if (operator == Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN) {
writeNode(assignment.getLeftHandSide());
copySpaceAndComments();
matchAndWrite(">>>=", "=");
copySpaceAndComments();
write("rightShiftUnsigned(");
writeNodeAtDifferentPosition(assignment.getLeftHandSide());
write(", ");
writeNode(assignment.getRightHandSide());
write(")");
} else {
writeNode(assignment.getLeftHandSide());
copySpaceAndComments();
if (operator == Assignment.Operator.ASSIGN)
matchAndWrite("=");
else if (operator == Assignment.Operator.PLUS_ASSIGN)
matchAndWrite("+=");
else if (operator == Assignment.Operator.MINUS_ASSIGN)
matchAndWrite("-=");
else if (operator == Assignment.Operator.TIMES_ASSIGN)
matchAndWrite("*=");
else if (operator == Assignment.Operator.DIVIDE_ASSIGN)
matchAndWrite("/=");
else if (operator == Assignment.Operator.BIT_AND_ASSIGN)
matchAndWrite("&=");
else if (operator == Assignment.Operator.BIT_OR_ASSIGN)
matchAndWrite("|=");
else if (operator == Assignment.Operator.BIT_XOR_ASSIGN)
matchAndWrite("^=");
else if (operator == Assignment.Operator.REMAINDER_ASSIGN)
matchAndWrite("%=");
else if (operator == Assignment.Operator.LEFT_SHIFT_ASSIGN)
matchAndWrite("<<=");
else if (operator == Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN)
matchAndWrite(">>=");
else if (operator == Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN)
matchAndWrite(">>>=");
copySpaceAndComments();
writeNode(assignment.getRightHandSide());
}
}