Interface ASTExpression
-
- All Superinterfaces:
ASTMemberValue
,ASTSwitchArrowRHS
,GenericNode<JavaNode>
,JavaNode
,JjtreeNode<JavaNode>
,Node
,Reportable
,TextAvailableNode
,TypeNode
- All Known Subinterfaces:
ASTAssignableExpr
,ASTAssignableExpr.ASTNamedReferenceExpr
,ASTLiteral
,ASTPrimaryExpression
,FunctionalExpression
,QualifiableExpression
- All Known Implementing Classes:
ASTAmbiguousName
,ASTArrayAccess
,ASTArrayAllocation
,ASTArrayInitializer
,ASTAssignmentExpression
,ASTBooleanLiteral
,ASTCastExpression
,ASTCharLiteral
,ASTClassLiteral
,ASTConditionalExpression
,ASTConstructorCall
,ASTFieldAccess
,ASTInfixExpression
,ASTLambdaExpression
,ASTMethodCall
,ASTMethodReference
,ASTNullLiteral
,ASTNumericLiteral
,ASTPatternExpression
,ASTStringLiteral
,ASTSuperExpression
,ASTSwitchExpression
,ASTTemplateExpression
,ASTThisExpression
,ASTTypeExpression
,ASTUnaryExpression
,ASTVariableAccess
public interface ASTExpression extends JavaNode, TypeNode, ASTMemberValue, ASTSwitchArrowRHS
Represents an expression, in the most general sense. This corresponds to the Expression of the JLS.From 7.0.0 on, this is an interface which all expression nodes implement.
Expressions are required to be constant in some parts of the grammar (in
SwitchLabel
,Annotation
,DefaultValue
). A constant expression is represented as a normal expression subtree, which does not feature anyMethodReference
,LambdaExpression
orAssignmentExpression
.(: In increasing precedence order :) Expression ::=
AssignmentExpression
|ConditionalExpression
|LambdaExpression
|InfixExpression
|PrefixExpression
|CastExpression
|PostfixExpression
|SwitchExpression
|PrimaryExpression
-
-
Field Summary
-
Fields inherited from interface net.sourceforge.pmd.lang.ast.Node
COORDS_COMPARATOR
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default @Nullable Object
getConstValue()
Returns the constant value of this node, if this is a constant expression.default @NonNull ExprContext
getConversionContext()
Returns the type expected by the context.int
getParenthesisDepth()
Returns the number of parenthesis levels around this expression.default boolean
isCompileTimeConstant()
Returns true if this expression is a compile-time constant, and is inlined.default boolean
isExpression()
Always returns true.default boolean
isParenthesized()
Returns true if this expression has at least one level of parentheses.-
Methods inherited from interface net.sourceforge.pmd.lang.ast.impl.GenericNode
ancestors, ancestorsOrSelf, asStream, children, descendants, descendantsOrSelf, getChild, getFirstChild, getLastChild, getNextSibling, getParent, getPreviousSibling
-
Methods inherited from interface net.sourceforge.pmd.lang.java.ast.JavaNode
getEnclosingType, getRoot, getSymbolTable, getTypeSystem
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode
getFirstToken, getLastToken, tokens
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.Node
acceptVisitor, ancestors, children, compareLocation, descendants, firstChild, getAstInfo, getBeginColumn, getBeginLine, getEndColumn, getEndLine, getImage, getIndexInParent, getLanguageVersion, getNumChildren, getReportLocation, getTextDocument, getUserMap, getXPathAttributesIterator, getXPathNodeName, hasImageEqualTo, isFindBoundary
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.TextAvailableNode
getOriginalText, getText, getTextRegion
-
Methods inherited from interface net.sourceforge.pmd.lang.java.ast.TypeNode
getTypeMirror, getTypeMirror
-
-
-
-
Method Detail
-
isExpression
default boolean isExpression()
Always returns true. This is to allow XPath queries to query like/*[@Expression=true()]
to match any expression, but is useless in Java code.
-
getParenthesisDepth
int getParenthesisDepth()
Returns the number of parenthesis levels around this expression. If this method returns 0, then no parentheses are present.E.g. the expression
(a + b)
is parsed as an AdditiveExpression whose parenthesisDepth is 1, and in((a + b))
it's 2.This is to avoid the parentheses interfering with analysis. Parentheses already influence parsing by breaking the natural precedence of operators. It would mostly hide false positives to make a ParenthesizedExpr node, because it would make semantically equivalent nodes have a very different representation.
On the other hand, when a rule explicitly cares about parentheses, then this attribute may be used to find out whether parentheses were mentioned, so no information is lost.
-
isParenthesized
default boolean isParenthesized()
Returns true if this expression has at least one level of parentheses. The specific depth can be fetched withgetParenthesisDepth()
.
-
getConstValue
default @Nullable Object getConstValue()
Description copied from interface:ASTMemberValue
Returns the constant value of this node, if this is a constant expression. Otherwise, or if some references couldn't be resolved, returns null. Note thatnull
is not a constant value, so this method's returning null is not a problem. Note that annotations are not given a constant value by this implementation.- Specified by:
getConstValue
in interfaceASTMemberValue
-
isCompileTimeConstant
default boolean isCompileTimeConstant()
Returns true if this expression is a compile-time constant, and is inlined.
-
getConversionContext
default @NonNull ExprContext getConversionContext()
Returns the type expected by the context. This type may determine an implicit conversion of this value to that type (eg a boxing conversion, widening numeric conversion, or widening reference conversion).There are many different cases. For example, in
arr['c']
,TypeNode.getTypeMirror()
would returnchar
for the char literal, but the context type isint
since it's used as an array index. Hence, a widening conversion occurs. Similarly, the context type of an expression in a return statement is the return type of the method, etc.If the context is undefined, then the returned object will answer true to
ExprContext.isMissing()
. This is completely normal and needs to be accounted for by rules. For instance, it occurs if this expression is used as a statement.Note that conversions are a language-level construct only. Converting from a type to another may not actually require any concrete operation at runtime. For instance, converting a
char
to anint
is a noop at runtime, because chars are anyway treated as ints by the JVM (within stack frames). A boxing conversion will however in general translate to a call to e.g.Integer.valueOf(int)
.Not all contexts allow all kinds of conversions. See
ExprContext
.
-
-