Class CompilationUnit
- java.lang.Object
-
- com.tonic.analysis.source.ast.decl.CompilationUnit
-
-
Constructor Summary
Constructors Constructor Description CompilationUnit()Creates an empty compilation unit at an unknown location.CompilationUnit(SourceLocation location)Creates an empty compilation unit.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Taccept(SourceVisitor<T> visitor)Accepts a visitor for this node.CompilationUnitaddImport(ImportDecl importDecl)Adds an import declaration.CompilationUnitaddImport(String className)Adds a regular import of a class.CompilationUnitaddStaticImport(String memberName)Adds a static import of a member.CompilationUnitaddType(TypeDecl type)Adds a top-level type declaration.List<ASTNode>getChildren()Lists the direct children a node holds, in source order.ClassDeclgetClass(String name)Finds a top-level class by simple name.List<ClassDecl>getClasses()EnumDeclgetEnum(String name)Finds a top-level enum by simple name.List<EnumDecl>getEnums()StringgetFullyQualifiedName(String simpleName)Qualifies a simple name with this unit's package.NodeList<ImportDecl>getImports()InterfaceDeclgetInterface(String name)Finds a top-level interface by simple name.List<InterfaceDecl>getInterfaces()SourceLocationgetLocation()Gets the source location of this node.StringgetPackageName()ASTNodegetParent()Gets the parent node in the AST tree.TypeDeclgetPrimaryType()Picks the unit's primary type: the first public type, else the first type.TypeDeclgetType(String name)Finds a top-level type by simple name.NodeList<TypeDecl>getTypes()booleanhasPackage()voidsetPackageName(String packageName)Sets the package name.voidsetParent(ASTNode parent)Sets the parent node.StringtoString()CompilationUnitwithPackageName(String packageName)Sets the package name.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.tonic.analysis.source.ast.ASTNode
deepClone, findAll, findAll, findAncestor, findFirst, findFirst, getRoot, isDescendantOf, remove, replaceWith, stream, stream, walk
-
-
-
-
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
-
getImports
public NodeList<ImportDecl> getImports()
- Returns:
- the imports
-
getLocation
public SourceLocation getLocation()
Description copied from interface:ASTNodeGets the source location of this node.- Specified by:
getLocationin interfaceASTNode- Returns:
- the location
-
getParent
public ASTNode getParent()
Description copied from interface:ASTNodeGets the parent node in the AST tree.
-
setParent
public void setParent(ASTNode parent)
Sets the parent node.
-
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
-
getInterfaces
public List<InterfaceDecl> getInterfaces()
- Returns:
- the top-level interface 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:ASTNodeLists the direct children a node holds, in source order.- Specified by:
getChildrenin interfaceASTNode- Returns:
- the direct children, empty for a leaf
-
accept
public <T> T accept(SourceVisitor<T> visitor)
Description copied from interface:ASTNodeAccepts a visitor for this node.
-
-