Class OperatorHelper

java.lang.Object
spoon.reflect.visitor.OperatorHelper

public final class OperatorHelper extends Object
Computes source code representation of the operator
  • Method Details

    • isPrefixOperator

      public static boolean isPrefixOperator(UnaryOperatorKind o)
      Checks if the operator is a prefix operator.
      Parameters:
      o - the operator
      Returns:
      true if it is a prefix operator, false otherwise
    • isSufixOperator

      public static boolean isSufixOperator(UnaryOperatorKind o)
      Checks if the operator is a suffix operator.
      Parameters:
      o - the operator
      Returns:
      true if it is a suffix operator, false otherwise
    • getOperatorText

      public static String getOperatorText(UnaryOperatorKind o)
      Gets the representation of the operator in the source code. For example, POS will return "+".
      Parameters:
      o - the operator
      Returns:
      java source code representation of a pre or post unary operator.
    • getOperatorText

      public static String getOperatorText(BinaryOperatorKind o)
      Gets the representation of the operator in the source code. For example, OR will return "||".
      Parameters:
      o - the operator
      Returns:
      java source code representation of a binary operator.
    • getOperatorPrecedence

      public static int getOperatorPrecedence(BinaryOperatorKind o)
      Get the precedence of a binary operator as defined by https://introcs.cs.princeton.edu/java/11precedence/
      Parameters:
      o - A binary operator kind.
      Returns:
      The precedence of the given operator.
    • getOperatorPrecedence

      public static int getOperatorPrecedence(UnaryOperatorKind o)
      Get the precedence of a unary operator as defined by https://introcs.cs.princeton.edu/java/11precedence/
      Parameters:
      o - A unary operator kind.
      Returns:
      The precedence of the given operator.
    • getOperatorAssociativity

      public static OperatorHelper.OperatorAssociativity getOperatorAssociativity(BinaryOperatorKind o)
      Get the associativity of a binary operator as defined by https://introcs.cs.princeton.edu/java/11precedence/ All binary operators are left-associative in Java, except for the relational operators that have no associativity (i.e. you can't chain them). There's an exception: the ternary operator ?: is right-associative, but that's not an operator kind in Spoon so we don't deal with it.
      Parameters:
      o - A binary operator kind.
      Returns:
      The associativity of the operator.
    • getOperatorAssociativity

      public static OperatorHelper.OperatorAssociativity getOperatorAssociativity(UnaryOperatorKind o)
      Get the associativity of a unary operator, as defined by https://introcs.cs.princeton.edu/java/11precedence/ All unary operators are right-associative, except for post increment and decrement, which are not associative.
      Parameters:
      o - A unary operator kind.
      Returns:
      The associativity of the operator.
    • getPromotedType

      public static Optional<CtTypeReference<?>> getPromotedType(BinaryOperatorKind operator, CtExpression<?> left, CtExpression<?> right)
      Get the promoted type of the binary operator, as defined by the Java Language Specification.

      Before an operator is applied, the type of the operands might be changed. This is called promotion. For example 1 + 1.0 has an int and a double as operands. The left operand is promoted to a double, so that the left and right operand have the same type.

      Parameters:
      operator - the operator
      left - the left operand, FactoryAccessor.getFactory() must not return null.
      right - the right operand
      Returns:
      the promoted type or Optional.empty() if promotion does not apply or the operation is invalid. Not every operator is defined for every combination of operands. For example 1 << 1.0 is invalid. In this case, Optional.empty() is returned.
      Throws:
      UnsupportedOperationException - if the operator is BinaryOperatorKind.INSTANCEOF or an unknown operator.
      See Also:
      JLS 5.6.2
    • getPromotedType

      public static Optional<CtTypeReference<?>> getPromotedType(UnaryOperatorKind operator, CtExpression<?> operand)
      Gets the promoted type of the unary operator, as defined by the Java Language Specification.

      Before an operator is applied, the type of the operand might be changed. This is called promotion. For example -((short) 1) has an operand of type short. The operand is promoted to an int, before the operator is applied.

      Parameters:
      operator - the operator
      operand - the operand, FactoryAccessor.getFactory() must not return null.
      Returns:
      the promoted type or Optional.empty() if promotion does not apply or the operation is invalid. Not every operator is defined for every combination of operands. For example !1 is invalid. In this case, Optional.empty() is returned.
      Throws:
      UnsupportedOperationException - if the operator is an unknown operator.
      See Also:
      JLS 5.6.1