Java Code Examples for org.eclipse.jdt.core.dom.InfixExpression#Operator

The following examples show how to use org.eclipse.jdt.core.dom.InfixExpression#Operator . 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: PolymorphismRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
protected Expression constructExpression(AST ast, DefaultMutableTreeNode node) {
	Object object = node.getUserObject();
	if(object instanceof InfixExpression.Operator) {
		InfixExpression.Operator operator = (InfixExpression.Operator)object;
		InfixExpression infixExpression = ast.newInfixExpression();
		infixExpression.setOperator(operator);
		DefaultMutableTreeNode leftChild = (DefaultMutableTreeNode)node.getChildAt(0);
		DefaultMutableTreeNode rightChild = (DefaultMutableTreeNode)node.getChildAt(1);
		infixExpression.setLeftOperand(constructExpression(ast, leftChild));
		infixExpression.setRightOperand(constructExpression(ast, rightChild));
		return infixExpression;
	}
	else if(object instanceof Expression) {
		Expression expression = (Expression)object;
		return expression;
	}
	return null;
}
 
Example 2
Source File: ExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean needsParentesis(ASTNode node) {
	if (!(node.getParent() instanceof InfixExpression))
		return false;

	if (node instanceof InstanceofExpression)
		return true;

	if (node instanceof InfixExpression) {
		InfixExpression expression = (InfixExpression) node;
		InfixExpression.Operator operator = expression.getOperator();

		InfixExpression parentExpression = (InfixExpression) node.getParent();
		InfixExpression.Operator parentOperator = parentExpression.getOperator();

		if (parentOperator == operator)
			return false;

		return true;
	}

	return false;
}
 
Example 3
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 4
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type of infix expression based on its operands and operator.
 * 
 * @param operator the operator of infix expression
 * @param leftOperandType the type of left operand of infix expression
 * @param rightOperandType the type of right operand of infix expression
 * @return the type of infix expression if the type of both the operands is same or if the type
 *         of either operand of a + operator is String, <code>null</code> otherwise.
 * 
 * @since 3.9
 */
private static ITypeBinding getInfixExpressionType(InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
	if (leftOperandType == rightOperandType) {
		return leftOperandType;
	}
	if (operator == InfixExpression.Operator.PLUS) {
		if (isStringType(leftOperandType)) {
			return leftOperandType;
		} else if (isStringType(rightOperandType)) {
			return rightOperandType;
		}
	}
	// If the left and right operand types are different, we assume that parentheses are needed.
	// This is to avoid complications of numeric promotions and for readability of complicated code.
	return null;
}
 
Example 5
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isAssociative(InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) {
	if (operator == InfixExpression.Operator.PLUS)
		return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType;

	if (operator == InfixExpression.Operator.TIMES)
		return isIntegerType(infixExprType) && isAllOperandsHaveSameType;

	if (operator == InfixExpression.Operator.CONDITIONAL_AND
			|| operator == InfixExpression.Operator.CONDITIONAL_OR
			|| operator == InfixExpression.Operator.AND
			|| operator == InfixExpression.Operator.OR
			|| operator == InfixExpression.Operator.XOR)
		return true;

	return false;
}
 
Example 6
Source File: IfStatementExpressionAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean allParentNodesAreConditionalOrOperators() {
	Enumeration<DefaultMutableTreeNode> enumeration = root.breadthFirstEnumeration();
	while(enumeration.hasMoreElements()) {
		DefaultMutableTreeNode node = enumeration.nextElement();
		if(!node.isLeaf()) {
			InfixExpression.Operator operator = (InfixExpression.Operator)node.getUserObject();
			if(!operator.equals(InfixExpression.Operator.CONDITIONAL_OR))
				return false;
		}
	}
	return true;
}
 
Example 7
Source File: InfixExpressionWriter.java    From juniversal with MIT License 5 votes vote down vote up
@Override
public void write(InfixExpression infixExpression) {
    InfixExpression.Operator operator = infixExpression.getOperator();

    if (operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) {
        writeRightShiftUnsigned(infixExpression);
    } else {
        writeNode(infixExpression.getLeftOperand());

        copySpaceAndComments();
        String operatorToken = this.equivalentOperators.get(operator);
        matchAndWrite(operatorToken);

        copySpaceAndComments();
        writeNode(infixExpression.getRightOperand());

        if (infixExpression.hasExtendedOperands()) {
            forEach(infixExpression.extendedOperands(), (Expression extendedOperand) -> {
                copySpaceAndComments();
                matchAndWrite(operatorToken);

                copySpaceAndComments();
                writeNode(extendedOperand);
            });
        }
    }
}
 
Example 8
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(InfixExpression e) {
	InfixExpression.Operator op= e.getOperator();
	if (isBitOperation(op)) {
		return true;
	} else if (op == InfixExpression.Operator.EQUALS || op == InfixExpression.Operator.NOT_EQUALS) {
		fCompareExpression= e;
		return false;
	}
	return false;
}
 
Example 9
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	InfixExpression infix= ast.newInfixExpression();
	infix.setLeftOperand(getterExpression);
	infix.setOperator(operator);
	NumberLiteral number= ast.newNumberLiteral();
	number.setToken("1"); //$NON-NLS-1$
	infix.setRightOperand(number);
	ITypeBinding infixType= infix.resolveTypeBinding();
	return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
}
 
Example 10
Source File: IfStatementExpressionAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
public List<InfixExpression> getInfixExpressionsWithEqualsOperator() {
	List<InfixExpression> expressionList = new ArrayList<InfixExpression>();
	DefaultMutableTreeNode leaf = root.getFirstLeaf();
	while(leaf != null) {
		Expression expression = (Expression)leaf.getUserObject();
		if(expression instanceof InfixExpression) {
			InfixExpression infixExpression = (InfixExpression)expression;
			InfixExpression.Operator operator = infixExpression.getOperator();
			if(operator.equals(InfixExpression.Operator.EQUALS))
				expressionList.add(infixExpression);
		}
		leaf = leaf.getNextLeaf();
	}
	return expressionList;
}
 
Example 11
Source File: InfixExpressionWriter.java    From juniversal with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void write(InfixExpression infixExpression) {
	InfixExpression.Operator operator = infixExpression.getOperator();

	if (operator == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) {
		write("rightShiftUnsigned(");
           writeNode(infixExpression.getLeftOperand());

		// Skip spaces before the >>> but if there's a newline (or comments) there, copy them
		skipSpacesAndTabs();
		copySpaceAndComments();
		matchAndWrite(">>>", ",");

		copySpaceAndComments();
           writeNode(infixExpression.getRightOperand());
		write(")");
	}
	else {
           writeNode(infixExpression.getLeftOperand());

		copySpaceAndComments();
		String operatorToken = this.equivalentOperators.get(infixExpression.getOperator());
		matchAndWrite(operatorToken);

		copySpaceAndComments();
           writeNode(infixExpression.getRightOperand());

		if (infixExpression.hasExtendedOperands()) {
			for (Expression extendedOperand : (List<Expression>) infixExpression.extendedOperands()) {
				
				copySpaceAndComments();
				matchAndWrite(operatorToken);

				copySpaceAndComments();
                   writeNode(extendedOperand);
			}
		}
	}
}
 
Example 12
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: AssociativeInfixExpressionFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isOperatorAssociative(InfixExpression.Operator operator) {
	return    operator == InfixExpression.Operator.PLUS
	        || operator == InfixExpression.Operator.TIMES
	        || operator == InfixExpression.Operator.XOR
	        || operator == InfixExpression.Operator.OR
	        || operator == InfixExpression.Operator.AND
	        || operator == InfixExpression.Operator.CONDITIONAL_OR
	        || operator == InfixExpression.Operator.CONDITIONAL_AND;
}
 
Example 14
Source File: IfStatementExpressionAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
private void processExpression(DefaultMutableTreeNode parent, Expression expression) {
	if(expression instanceof InfixExpression) {
		InfixExpression infixExpression = (InfixExpression)expression;
		InfixExpression.Operator operator = infixExpression.getOperator();
		if(operator.equals(InfixExpression.Operator.CONDITIONAL_AND) || operator.equals(InfixExpression.Operator.CONDITIONAL_OR)) {
			parent.setUserObject(operator);
			DefaultMutableTreeNode leftOperandNode = new DefaultMutableTreeNode();
			DefaultMutableTreeNode rightOperandNode = new DefaultMutableTreeNode();
			parent.add(leftOperandNode);
			parent.add(rightOperandNode);
			processExpression(leftOperandNode, infixExpression.getLeftOperand());
			processExpression(rightOperandNode, infixExpression.getRightOperand());
			if(infixExpression.hasExtendedOperands()) {
				DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent();
				int parentIndex = -1;
				if(grandParent != null)
					parentIndex = grandParent.getIndex(parent);
				DefaultMutableTreeNode newParent = processExtendedOperands(parent, infixExpression.extendedOperands());
				if(grandParent != null)
					grandParent.insert(newParent, parentIndex);
				else
					root = newParent;
			}
		}
		else {
			parent.setUserObject(infixExpression);
		}
	}
	else {
		parent.setUserObject(expression);
	}
}
 
Example 15
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getInversedInfixExpression(ASTRewrite rewrite, InfixExpression expression, InfixExpression.Operator newOperator, SimpleNameRenameProvider provider) {
	InfixExpression newExpression = rewrite.getAST().newInfixExpression();
	newExpression.setOperator(newOperator);
	newExpression.setLeftOperand(getRenamedNameCopy(provider, rewrite, expression.getLeftOperand()));
	newExpression.setRightOperand(getRenamedNameCopy(provider, rewrite, expression.getRightOperand()));
	return newExpression;
}
 
Example 16
Source File: AssociativeInfixExpressionFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public InfixExpression.Operator getOperator() {
	return fGroupRoot.getOperator();
}
 
Example 17
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isBitOperation(InfixExpression.Operator op) {
	return op == InfixExpression.Operator.AND || op == InfixExpression.Operator.OR || op == InfixExpression.Operator.XOR;
}
 
Example 18
Source File: AssociativeInfixExpressionFragment.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isOperatorAssociative(InfixExpression.Operator operator) {
	return operator == InfixExpression.Operator.PLUS || operator == InfixExpression.Operator.TIMES || operator == InfixExpression.Operator.XOR || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.AND
			|| operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.CONDITIONAL_AND;
}
 
Example 19
Source File: InfixExpr.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
public void setOperator(InfixExpression.Operator operator){
	_operator = operator;
}
 
Example 20
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static BinaryOperator getBinaryOperator(InfixExpression.Operator operator) {
  switch (operator.toString()) {
    case "*":
      return BinaryOperator.TIMES;
    case "/":
      return BinaryOperator.DIVIDE;
    case "%":
      return BinaryOperator.REMAINDER;
    case "+":
      return BinaryOperator.PLUS;
    case "-":
      return BinaryOperator.MINUS;
    case "<<":
      return BinaryOperator.LEFT_SHIFT;
    case ">>":
      return BinaryOperator.RIGHT_SHIFT_SIGNED;
    case ">>>":
      return BinaryOperator.RIGHT_SHIFT_UNSIGNED;
    case "<":
      return BinaryOperator.LESS;
    case ">":
      return BinaryOperator.GREATER;
    case "<=":
      return BinaryOperator.LESS_EQUALS;
    case ">=":
      return BinaryOperator.GREATER_EQUALS;
    case "==":
      return BinaryOperator.EQUALS;
    case "!=":
      return BinaryOperator.NOT_EQUALS;
    case "^":
      return BinaryOperator.BIT_XOR;
    case "&":
      return BinaryOperator.BIT_AND;
    case "|":
      return BinaryOperator.BIT_OR;
    case "&&":
      return BinaryOperator.CONDITIONAL_AND;
    case "||":
      return BinaryOperator.CONDITIONAL_OR;
    default:
      return null;
  }
}