Interface ASTExpression

All Superinterfaces:
ASTMemberValue, ASTSwitchArrowRHS, net.sourceforge.pmd.lang.ast.impl.GenericNode<JavaNode>, JavaNode, net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode<JavaNode>, net.sourceforge.pmd.lang.ast.Node, net.sourceforge.pmd.reporting.Reportable, net.sourceforge.pmd.lang.ast.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, ASTThisExpression, ASTTypeExpression, ASTUnaryExpression, ASTVariableAccess

public interface ASTExpression extends 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 any MethodReference, LambdaExpression or AssignmentExpression.


 (: In increasing precedence order :)
 Expression ::= AssignmentExpression
              | ConditionalExpression
              | LambdaExpression
              | InfixExpression
              | PrefixExpression | CastExpression
              | PostfixExpression
              | SwitchExpression
              | PrimaryExpression

 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Result of constant folding an expression.
  • Field Summary

    Fields inherited from interface net.sourceforge.pmd.lang.ast.Node

    COORDS_COMPARATOR
  • Method Summary

    Modifier and Type
    Method
    Description
    default @NonNull ASTExpression.ConstResult
    Returns the result of constant folding on this expression.
    default @Nullable Object
    Returns the constant value of this node, if this is a constant expression.
    default @NonNull ExprContext
    Returns the type expected by the context.
    int
    Returns the number of parenthesis levels around this expression.
    default boolean
    Returns true if this expression is a compile-time constant, and is inlined.
    default boolean
    Always returns true.
    default boolean
    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 Details

    • 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 with getParenthesisDepth().
    • getConstValue

      default @Nullable Object getConstValue()
      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 that null 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.

      Note that even if this is null, the constant value may be known anyway but technically not be a CT constant, because it uses non-static (final) fields for instance. See getConstFoldingResult() and ASTExpression.ConstResult to get a constant value in this case.

      Specified by:
      getConstValue in interface ASTMemberValue
      See Also:
    • getConstFoldingResult

      default @NonNull ASTExpression.ConstResult getConstFoldingResult()
      Returns the result of constant folding on this expression. This may find a constant value for more than strict compile-time constants. See ASTExpression.ConstResult.
      Since:
      7.12.0
    • isCompileTimeConstant

      default boolean isCompileTimeConstant()
      Returns true if this expression is a compile-time constant, and is inlined.
      See Also:
    • 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 return char for the char literal, but the context type is int 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 an int 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.