Class CompilationUnit

  • All Implemented Interfaces:
    ASTNode

    public final class CompilationUnit
    extends Object
    implements ASTNode
    A Java source file: package declaration, imports, and top-level type declarations.
    • Constructor Detail

      • CompilationUnit

        public CompilationUnit​(SourceLocation location)
        Creates an empty compilation unit.
        Parameters:
        location - the source location, or null for unknown
      • CompilationUnit

        public CompilationUnit()
        Creates an empty compilation unit at an unknown location.
    • Method Detail

      • getPackageName

        public String getPackageName()
        Returns:
        the package name
      • setPackageName

        public void setPackageName​(String packageName)
        Sets the package name.
        Parameters:
        packageName - the package name, or null/empty for the default package
      • 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 parent node.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the new parent
      • withPackageName

        public CompilationUnit withPackageName​(String packageName)
        Sets the package name.
        Parameters:
        packageName - the package name, or null/empty for the default package
        Returns:
        this unit
      • addImport

        public CompilationUnit addImport​(ImportDecl importDecl)
        Adds an import declaration.
        Parameters:
        importDecl - the import to add
        Returns:
        this unit
      • addImport

        public CompilationUnit addImport​(String className)
        Adds a regular import of a class.
        Parameters:
        className - the imported class name
        Returns:
        this unit
      • addStaticImport

        public CompilationUnit addStaticImport​(String memberName)
        Adds a static import of a member.
        Parameters:
        memberName - the imported member name
        Returns:
        this unit
      • addType

        public CompilationUnit addType​(TypeDecl type)
        Adds a top-level type declaration.
        Parameters:
        type - the type to add
        Returns:
        this unit
      • hasPackage

        public boolean hasPackage()
        Returns:
        true if a non-empty package name is set
      • getClasses

        public List<ClassDecl> getClasses()
        Returns:
        the top-level class declarations
      • getInterfaces

        public List<InterfaceDecl> getInterfaces()
        Returns:
        the top-level interface declarations
      • getEnums

        public List<EnumDecl> getEnums()
        Returns:
        the top-level enum declarations
      • getType

        public TypeDecl getType​(String name)
        Finds a top-level type by simple name.
        Parameters:
        name - the simple type name
        Returns:
        the matching type, or null if none
      • getClass

        public ClassDecl getClass​(String name)
        Finds a top-level class by simple name.
        Parameters:
        name - the simple class name
        Returns:
        the matching class, or null if none
      • getInterface

        public InterfaceDecl getInterface​(String name)
        Finds a top-level interface by simple name.
        Parameters:
        name - the simple interface name
        Returns:
        the matching interface, or null if none
      • getEnum

        public EnumDecl getEnum​(String name)
        Finds a top-level enum by simple name.
        Parameters:
        name - the simple enum name
        Returns:
        the matching enum, or null if none
      • getPrimaryType

        public TypeDecl getPrimaryType()
        Picks the unit's primary type: the first public type, else the first type.
        Returns:
        the primary type, or null if the unit has no types
      • getFullyQualifiedName

        public String getFullyQualifiedName​(String simpleName)
        Qualifies a simple name with this unit's package.
        Parameters:
        simpleName - the simple type name
        Returns:
        the dotted qualified name, or the simple name in the default package
      • 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