Package spoon.reflect.visitor
Class OperatorHelper
java.lang.Object
spoon.reflect.visitor.OperatorHelper
Computes source code representation of the operator
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionGet 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.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.static int
Get the precedence of a binary operator as defined by https://introcs.cs.princeton.edu/java/11precedence/static int
Get the precedence of a unary operator as defined by https://introcs.cs.princeton.edu/java/11precedence/static String
Gets the representation of the operator in the source code.static String
Gets the representation of the operator in the source code.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.static Optional<CtTypeReference<?>>
getPromotedType(UnaryOperatorKind operator, CtExpression<?> operand)
Gets the promoted type of the unary operator, as defined by the Java Language Specification.static boolean
Checks if the operator is a prefix operator.static boolean
Checks if the operator is a suffix operator.
-
Method Details
-
isPrefixOperator
Checks if the operator is a prefix operator.- Parameters:
o
- the operator- Returns:
- true if it is a prefix operator, false otherwise
-
isSufixOperator
Checks if the operator is a suffix operator.- Parameters:
o
- the operator- Returns:
- true if it is a suffix operator, false otherwise
-
getOperatorText
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
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
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
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
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
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 operatorleft
- the left operand,FactoryAccessor.getFactory()
must not returnnull
.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 example1 << 1.0
is invalid. In this case,Optional.empty()
is returned. - Throws:
UnsupportedOperationException
- if the operator isBinaryOperatorKind.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 operatoroperand
- the operand,FactoryAccessor.getFactory()
must not returnnull
.- 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
-