Class InstanceOfExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class InstanceOfExpr
    extends Object
    implements Expression
    An instanceof expression: expression instanceof Type, with an optional Java 16+ pattern variable.
    • Constructor Detail

      • InstanceOfExpr

        public InstanceOfExpr​(Expression expression,
                              SourceType checkType,
                              String patternVariable,
                              SourceLocation location)
        Creates an instanceof test and reparents the tested expression.
        Parameters:
        expression - the expression being tested
        checkType - the type tested against
        patternVariable - the pattern variable name, or null for classic instanceof
        location - the source location, or null for unknown
        Throws:
        NullPointerException - if expression or checkType is null
      • InstanceOfExpr

        public InstanceOfExpr​(Expression expression,
                              SourceType checkType,
                              String patternVariable)
        Creates an instanceof test with an unknown source location.
        Parameters:
        expression - the expression being tested
        checkType - the type tested against
        patternVariable - the pattern variable name, or null for classic instanceof
        Throws:
        NullPointerException - if expression or checkType is null
      • InstanceOfExpr

        public InstanceOfExpr​(Expression expression,
                              SourceType checkType)
        Creates a classic instanceof test without a pattern variable.
        Parameters:
        expression - the expression being tested
        checkType - the type tested against
        Throws:
        NullPointerException - if expression or checkType is null
    • Method Detail

      • getExpression

        public Expression getExpression()
        Returns:
        the expression
      • setExpression

        public void setExpression​(Expression expression)
        Replaces the tested expression, reparenting the new child.
        Parameters:
        expression - the new tested expression
      • getCheckType

        public SourceType getCheckType()
        Returns:
        the check type
      • getPatternVariable

        public String getPatternVariable()
        Returns:
        the pattern variable
      • setPatternVariable

        public void setPatternVariable​(String patternVariable)
        Parameters:
        patternVariable - the pattern variable name, or null for classic instanceof
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Description copied from interface: ASTNode
        Sets the parent node in the AST tree.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the enclosing AST node
      • withExpression

        public InstanceOfExpr withExpression​(Expression expression)
        Replaces the tested expression, reparenting the new child and releasing the former one.
        Parameters:
        expression - the new tested expression
        Returns:
        this expression
      • withPatternVariable

        public InstanceOfExpr withPatternVariable​(String patternVariable)
        Replaces the pattern variable name.
        Parameters:
        patternVariable - the pattern variable name, or null for classic instanceof
        Returns:
        this expression
      • hasPatternVariable

        public boolean hasPatternVariable()
        Returns:
        true if this is a pattern matching instanceof (Java 16+)
      • getType

        public SourceType getType()
        Description copied from interface: Expression
        Gets the inferred type of this expression.
        Specified by:
        getType in interface Expression
        Returns:
        the type of this expression
      • getChildren

        public List<ASTNode> getChildren()
        Description copied from interface: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor