Class VarDeclStmt

  • All Implemented Interfaces:
    ASTNode, Statement

    public final class VarDeclStmt
    extends Object
    implements Statement
    Represents a local variable declaration: Type name [= initializer] Also supports Java 10+ var keyword.
    • Constructor Detail

      • VarDeclStmt

        public VarDeclStmt​(SourceType type,
                           String name,
                           Expression initializer,
                           boolean useVarKeyword,
                           boolean isFinal,
                           SourceLocation location)
        Creates a declaration, adopting the initializer as a child.
        Parameters:
        type - declared type
        name - variable name
        initializer - initial value, or null
        useVarKeyword - whether to print "var" instead of the type
        isFinal - whether the declaration is final
        location - source position, null becoming UNKNOWN
        Throws:
        NullPointerException - if the type or name is null
      • VarDeclStmt

        public VarDeclStmt​(SourceType type,
                           String name,
                           Expression initializer)
        Creates a non-final declaration with an explicit type and an unknown location.
        Parameters:
        type - declared type
        name - variable name
        initializer - initial value, or null
        Throws:
        NullPointerException - if the type or name is null
      • VarDeclStmt

        public VarDeclStmt​(SourceType type,
                           String name)
        Creates a non-final declaration with no initializer and an unknown location.
        Parameters:
        type - declared type
        name - variable name
        Throws:
        NullPointerException - if the type or name is null
    • Method Detail

      • isSynthetic

        public boolean isSynthetic()
        Returns:
        whether synthetic
      • markSynthetic

        public void markSynthetic()
        Flags this declaration as a recovery-made carrier rather than a real local.
      • getType

        public SourceType getType()
        Returns:
        the type
      • setType

        public void setType​(SourceType type)
        Parameters:
        type - new declared type
      • getName

        public String getName()
        Returns:
        the name
      • setName

        public void setName​(String name)
        Parameters:
        name - new variable name
      • getInitializer

        public Expression getInitializer()
        Returns:
        the initializer
      • setInitializer

        public void setInitializer​(Expression initializer)
        Replaces the initializer through withInitializer, so parent links are maintained.
        Parameters:
        initializer - new initializer, or null to drop it
      • isUseVarKeyword

        public boolean isUseVarKeyword()
        Returns:
        whether use var keyword
      • setUseVarKeyword

        public void setUseVarKeyword​(boolean useVarKeyword)
        Parameters:
        useVarKeyword - whether to print "var" instead of the declared type
      • isFinal

        public boolean isFinal()
        Returns:
        whether final
      • setFinal

        public void setFinal​(boolean isFinal)
        Parameters:
        isFinal - whether the declaration carries the final modifier
      • 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 - enclosing AST node
      • withVar

        public static VarDeclStmt withVar​(SourceType inferredType,
                                          String name,
                                          Expression initializer)
        Creates a declaration printed with the var keyword.
        Parameters:
        inferredType - type the var stands for
        name - variable name
        initializer - initial value
        Returns:
        the new declaration
      • hasInitializer

        public boolean hasInitializer()
        Returns:
        true if an initializer is present
      • withInitializer

        public VarDeclStmt withInitializer​(Expression initializer)
        Replaces the initializer, reparenting the new one and releasing the old child link.
        Parameters:
        initializer - new initializer, 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