Class TryCatchStmt

  • All Implemented Interfaces:
    ASTNode, Statement

    public final class TryCatchStmt
    extends Object
    implements Statement
    Represents a try-catch statement with optional finally block and resources (try-with-resources).
    • Constructor Detail

      • TryCatchStmt

        public TryCatchStmt​(Statement tryBlock,
                            List<CatchClause> catches,
                            Statement finallyBlock,
                            List<Expression> resources,
                            SourceLocation location)
        Creates a try statement, adopting the try block, catch bodies, finally block and resources as children.
        Parameters:
        tryBlock - the guarded block
        catches - the catch clauses, may be null for none
        finallyBlock - the finally block, or null
        resources - the try-with-resources expressions, may be null for none
        location - the source location, or null for unknown
        Throws:
        NullPointerException - if the try block is null
      • TryCatchStmt

        public TryCatchStmt​(Statement tryBlock,
                            List<CatchClause> catches,
                            Statement finallyBlock)
        Creates a try statement with no resources and an unknown location.
        Parameters:
        tryBlock - the guarded block
        catches - the catch clauses, may be null for none
        finallyBlock - the finally block, or null
        Throws:
        NullPointerException - if the try block is null
      • TryCatchStmt

        public TryCatchStmt​(Statement tryBlock,
                            List<CatchClause> catches)
        Creates a try statement with no finally block, no resources and an unknown location.
        Parameters:
        tryBlock - the guarded block
        catches - the catch clauses, may be null for none
        Throws:
        NullPointerException - if the try block is null
    • Method Detail

      • getTryBlock

        public Statement getTryBlock()
        Returns:
        the try block
      • setTryBlock

        public void setTryBlock​(Statement tryBlock)
        Replaces the guarded block, reparenting it and releasing the previous one.
        Parameters:
        tryBlock - the new try block
      • getFinallyBlock

        public Statement getFinallyBlock()
        Returns:
        the finally block
      • setFinallyBlock

        public void setFinallyBlock​(Statement finallyBlock)
        Replaces the finally block, reparenting it and releasing the previous one.
        Parameters:
        finallyBlock - the new finally block, or null to drop it
      • getResources

        public NodeList<Expression> getResources()
        Returns:
        the try-with-resources declarations, empty when there are none
      • 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)
        Sets the node this statement hangs from.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the enclosing node
      • addCatch

        public void addCatch​(CatchClause clause)
        Appends a catch clause and adopts its body.
        Parameters:
        clause - the clause to add
      • addResource

        public void addResource​(Expression resource)
        Appends a try-with-resources resource.
        Parameters:
        resource - the resource expression
      • hasResources

        public boolean hasResources()
        Returns:
        whether this is a try-with-resources statement
      • hasFinally

        public boolean hasFinally()
        Returns:
        whether a finally block is present
      • hasCatch

        public boolean hasCatch()
        Returns:
        whether any catch clause is present
      • withTryBlock

        public TryCatchStmt withTryBlock​(Statement tryBlock)
        Replaces the guarded block in place, reparenting it and releasing the previous one.
        Parameters:
        tryBlock - the new try block, may be null
        Returns:
        this statement
      • withFinallyBlock

        public TryCatchStmt withFinallyBlock​(Statement finallyBlock)
        Replaces the finally block in place, reparenting it and releasing the previous one.
        Parameters:
        finallyBlock - the new finally block, or null to drop it
        Returns:
        this statement
      • 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