Java Code Examples for org.eclipse.jdt.core.dom.InfixExpression#getRightOperand()

The following examples show how to use org.eclipse.jdt.core.dom.InfixExpression#getRightOperand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static int isOperatorSelected(InfixExpression infixExpression, int offset, int length) {
	ASTNode left = infixExpression.getLeftOperand();
	ASTNode right = infixExpression.getRightOperand();

	if (isSelectingOperator(left, right, offset, length)) {
		return ASTNodes.getExclusiveEnd(left);
	}
	List<Expression> extended = infixExpression.extendedOperands();
	for (int i = 0; i < extended.size(); i++) {
		left = right;
		right = extended.get(i);
		if (isSelectingOperator(left, right, offset, length)) {
			return ASTNodes.getExclusiveEnd(left);
		}
	}
	return -1;
}
 
Example 2
Source File: ASTFlattenerUtils.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private Iterable<StringLiteral> collectCompatibleNodes(final InfixExpression node) {
  final ArrayList<StringLiteral> strings = CollectionLiterals.<StringLiteral>newArrayList();
  InfixExpression.Operator _operator = node.getOperator();
  boolean _notEquals = (!Objects.equal(_operator, InfixExpression.Operator.PLUS));
  if (_notEquals) {
    return strings;
  }
  final Expression left = node.getLeftOperand();
  if ((left instanceof StringLiteral)) {
    strings.add(((StringLiteral)left));
  } else {
    if ((left instanceof InfixExpression)) {
      Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression)left)));
    }
  }
  final Expression right = node.getRightOperand();
  if ((right instanceof StringLiteral)) {
    strings.add(((StringLiteral)right));
  } else {
    if ((right instanceof InfixExpression)) {
      Iterables.<StringLiteral>addAll(strings, this.collectCompatibleNodes(((InfixExpression)right)));
    }
  }
  Iterables.<StringLiteral>addAll(strings, Iterables.<StringLiteral>filter(node.extendedOperands(), StringLiteral.class));
  return strings;
}
 
Example 3
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static int isOperatorSelected(InfixExpression infixExpression, int offset, int length) {
	ASTNode left= infixExpression.getLeftOperand();
	ASTNode right= infixExpression.getRightOperand();

	if (isSelectingOperator(left, right, offset, length)) {
		return ASTNodes.getExclusiveEnd(left);
	}
	List<Expression> extended= infixExpression.extendedOperands();
	for (int i= 0; i < extended.size(); i++) {
		left= right;
		right= extended.get(i);
		if (isSelectingOperator(left, right, offset, length)) {
			return ASTNodes.getExclusiveEnd(left);
		}
	}
	return -1;
}
 
Example 4
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean visit(InfixExpression expr) {
	/*
	 * InfixExpression: Expression InfixOperator Expression { InfixOperator Expression }
	 */
	activateDiffStyle(expr);
	StyledStringStyler styler = determineDiffStyle(expr, new StyledStringStyler(ordinaryStyle));
	
	Expression leftExpression = expr.getLeftOperand();
	handleExpression(leftExpression);
	appendSpace();
	styledString.append(expr.getOperator().toString(), styler);
	appendSpace();
	Expression rightExpression = expr.getRightOperand();
	handleExpression(rightExpression);
	for (int i = 0; i < expr.extendedOperands().size(); i++){
		appendSpace();
		styledString.append(expr.getOperator().toString(), styler);
		appendSpace();
		handleExpression((Expression) expr.extendedOperands().get(i));
	}
	
	deactivateDiffStyle(expr);
	return false;
}
 
Example 5
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression getNextSiblingExpression(Expression expression) {
	InfixExpression parentInfixExpression= (InfixExpression) expression.getParent();
	Expression sibiling;
	if (expression.equals(parentInfixExpression.getLeftOperand())) {
		sibiling= parentInfixExpression.getRightOperand();
	} else if (expression.equals(parentInfixExpression.getRightOperand())) {
		if (parentInfixExpression.getParent() instanceof InfixExpression)
			sibiling= getNextSiblingExpression(parentInfixExpression);
		else
			sibiling= null;
	} else {
		sibiling= null;
	}
	return sibiling;
}
 
Example 6
Source File: ConvertForLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean validateExpression(ForStatement statement) {
	Expression expression= statement.getExpression();
	if (!(expression instanceof InfixExpression))
		return false;

	InfixExpression infix= (InfixExpression)expression;

	Expression left= infix.getLeftOperand();
	Expression right= infix.getRightOperand();
	if (left instanceof SimpleName && right instanceof SimpleName) {
		IVariableBinding lengthBinding= fLengthBinding;
		if (lengthBinding == null)
			return false;

		IBinding leftBinding= ((SimpleName)left).resolveBinding();
		IBinding righBinding= ((SimpleName)right).resolveBinding();

		if (fIndexBinding.equals(leftBinding)) {
			return lengthBinding.equals(righBinding);
		} else if (fIndexBinding.equals(righBinding)) {
			return lengthBinding.equals(leftBinding);
		}

		return false;
	} else if (left instanceof SimpleName) {
		if (!fIndexBinding.equals(((SimpleName)left).resolveBinding()))
			return false;

		if (!Operator.LESS.equals(infix.getOperator()))
			return false;

		return validateLengthQuery(right);
	} else if (right instanceof SimpleName) {
		if (!fIndexBinding.equals(((SimpleName)right).resolveBinding()))
			return false;

		if (!Operator.GREATER.equals(infix.getOperator()))
			return false;

		return validateLengthQuery(left);
	}

	return false;
}
 
Example 7
Source File: ControlVariable.java    From JDeodorant with MIT License 4 votes vote down vote up
private VariableValue getEndValue(SimpleName variableNode, ASTNode conditionContainingVariable)
{
	VariableValue variableValue = new VariableValue();
	if (conditionContainingVariable instanceof InfixExpression)
	{
		InfixExpression infixExpression = (InfixExpression) conditionContainingVariable;
		Expression leftOperand          = infixExpression.getLeftOperand();
		Expression rightOperand         = infixExpression.getRightOperand();
		// get the operand opposite to the variable
		Boolean isVariableLeftOperand   = AbstractLoopUtilities.isVariableLeftOperand(variableNode, infixExpression);
		if (isVariableLeftOperand == null)
		{
			return null;
		}
		Expression nonVariableOperand = (isVariableLeftOperand) ? rightOperand : leftOperand;
		// evaluate the value of the opposing operand
		if (AbstractLoopUtilities.isLengthFieldAccess(nonVariableOperand) || AbstractLoopUtilities.isCollectionSizeInvocation(nonVariableOperand) ||
				AbstractLoopUtilities.isDataStructureSizeInvocation(nonVariableOperand))
		{
			variableValue = new VariableValue(VariableValue.ValueType.DATA_STRUCTURE_SIZE);
			if (nonVariableOperand instanceof QualifiedName)
			{
				this.dataStructureExpression = ((QualifiedName)nonVariableOperand).getQualifier();
			}
			else if (nonVariableOperand instanceof MethodInvocation)
			{
				this.dataStructureExpression = ((MethodInvocation)nonVariableOperand).getExpression();
			}
			else if (nonVariableOperand instanceof FieldAccess)
			{
				this.dataStructureExpression = ((FieldAccess)nonVariableOperand).getExpression();
			}
		}
		else
		{
			Integer value = AbstractLoopUtilities.getIntegerValue(nonVariableOperand);
			if (value != null)
			{
				variableValue = new VariableValue(value);
			}
		}
	}
	else if (conditionContainingVariable instanceof MethodInvocation)
	{
		// get the variableValue of that MethodBinding from ConditionalLoopBindingInformation
		MethodInvocation methodInvocation = (MethodInvocation) conditionContainingVariable;
		String methodBindingKey = methodInvocation.resolveMethodBinding().getMethodDeclaration().getKey();		// use .getMethodDeclaration() so we get the abstract method and not the method of the specific collection
		AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
		if (bindingInformation.conditionalMethodBindingEndValuesContains(methodBindingKey))
		{
			variableValue = bindingInformation.getConditionalMethodBindingEndValue(methodBindingKey);
			if (variableValue.getType() == VariableValue.ValueType.DATA_STRUCTURE_SIZE)
			{
				this.dataStructureExpression = getIteratorDataStructureExpression(variableNode);
			}
		}
	}
	return variableValue;
}
 
Example 8
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 4 votes vote down vote up
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;
}
 
Example 9
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 4 votes vote down vote up
private static boolean isVariableRightOperand(SimpleName variable, InfixExpression infixExpression)
{
	boolean rightOperandIsVariable     = false;
	//Expression leftOperand            = infixExpression.getLeftOperand();
	Expression rightOperand           = infixExpression.getRightOperand();
	IBinding infixVariableBinding     = variable.resolveBinding();
	if (rightOperand instanceof SimpleName)
	{
		SimpleName rightOperandSimpleName = (SimpleName) rightOperand;
		IBinding rightOperandBinding      = rightOperandSimpleName.resolveBinding();
		if (rightOperandBinding.isEqualTo(infixVariableBinding))
		{
			rightOperandIsVariable = true;
		}
	}
	else if (rightOperand instanceof InfixExpression)
	{
		InfixExpression infixRightOperand = (InfixExpression) rightOperand;
		boolean left = isVariableLeftOperand(variable, infixRightOperand);
		boolean right = isVariableRightOperand(variable, infixRightOperand);
		List<Expression> extendedOperands = infixRightOperand.extendedOperands();
		boolean variableFoundInExtendedOperands = false;
		for (Expression expression : extendedOperands)
		{
			if (expression instanceof SimpleName)
			{
				SimpleName simpleName = (SimpleName) expression;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				if (simpleNameBinding.isEqualTo(infixVariableBinding))
				{
					variableFoundInExtendedOperands = true;
					break;
				}
			}
		}
		if (left || right || variableFoundInExtendedOperands)
		{
			rightOperandIsVariable = true;
		}
	}
	return rightOperandIsVariable;
}