下面列出了org.eclipse.jdt.core.dom.Assignment#Operator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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?
}
public static BinaryOperator getBinaryOperator(Assignment.Operator operator) {
switch (operator.toString()) {
case "=":
return BinaryOperator.ASSIGN;
case "+=":
return BinaryOperator.PLUS_ASSIGN;
case "-=":
return BinaryOperator.MINUS_ASSIGN;
case "*=":
return BinaryOperator.TIMES_ASSIGN;
case "/=":
return BinaryOperator.DIVIDE_ASSIGN;
case "&=":
return BinaryOperator.BIT_AND_ASSIGN;
case "|=":
return BinaryOperator.BIT_OR_ASSIGN;
case "^=":
return BinaryOperator.BIT_XOR_ASSIGN;
case "%=":
return BinaryOperator.REMAINDER_ASSIGN;
case "<<=":
return BinaryOperator.LEFT_SHIFT_ASSIGN;
case ">>=":
return BinaryOperator.RIGHT_SHIFT_SIGNED_ASSIGN;
case ">>>=":
return BinaryOperator.RIGHT_SHIFT_UNSIGNED_ASSIGN;
default:
return null;
}
}
public static InfixExpression.Operator convertToInfixOperator(Assignment.Operator operator) {
if (operator.equals(Assignment.Operator.PLUS_ASSIGN))
return InfixExpression.Operator.PLUS;
if (operator.equals(Assignment.Operator.MINUS_ASSIGN))
return InfixExpression.Operator.MINUS;
if (operator.equals(Assignment.Operator.TIMES_ASSIGN))
return InfixExpression.Operator.TIMES;
if (operator.equals(Assignment.Operator.DIVIDE_ASSIGN))
return InfixExpression.Operator.DIVIDE;
if (operator.equals(Assignment.Operator.BIT_AND_ASSIGN))
return InfixExpression.Operator.AND;
if (operator.equals(Assignment.Operator.BIT_OR_ASSIGN))
return InfixExpression.Operator.OR;
if (operator.equals(Assignment.Operator.BIT_XOR_ASSIGN))
return InfixExpression.Operator.XOR;
if (operator.equals(Assignment.Operator.REMAINDER_ASSIGN))
return InfixExpression.Operator.REMAINDER;
if (operator.equals(Assignment.Operator.LEFT_SHIFT_ASSIGN))
return InfixExpression.Operator.LEFT_SHIFT;
if (operator.equals(Assignment.Operator.RIGHT_SHIFT_SIGNED_ASSIGN))
return InfixExpression.Operator.RIGHT_SHIFT_SIGNED;
if (operator.equals(Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN))
return InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED;
Assert.isTrue(false, "Cannot convert assignment operator"); //$NON-NLS-1$
return null;
}
private static Statement createAssignmentStatement(ASTRewrite rewrite, Assignment.Operator assignmentOperator, Expression origAssignee, Expression origAssigned) {
AST ast= rewrite.getAST();
Assignment elseAssignment= ast.newAssignment();
elseAssignment.setOperator(assignmentOperator);
elseAssignment.setLeftHandSide((Expression) rewrite.createCopyTarget(origAssignee));
elseAssignment.setRightHandSide((Expression) rewrite.createCopyTarget(origAssigned));
ExpressionStatement statement= ast.newExpressionStatement(elseAssignment);
return statement;
}
@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());
}
public void setOperator(Assignment.Operator operator){
_operator = operator;
}
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());
}
}