Package spoon.reflect.declaration
Interface CtCompilationUnit
- All Superinterfaces:
Cloneable
,CtElement
,CtQueryable
,CtVisitable
,FactoryAccessor
,Serializable
,SourcePositionHolder
- All Known Subinterfaces:
CompilationUnit
- All Known Implementing Classes:
CompilationUnitImpl
,CtCompilationUnitImpl
,NoSourcePosition.NullCompilationUnit
Defines a compilation unit. In Java, a compilation unit can contain only one
public type declaration and other secondary types declarations (not public).
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddDeclaredType(CtType<?> type)
Add a type to the list of declared types.addDeclaredTypeReference(CtTypeReference<?> type)
Add a type reference to the list of declared typesclone()
Clone the element which calls this method in a new object.Gets all binary (.class) files that corresponds to this compilation unit and have been created by callingSpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...)
.Gets the declared module if the compilationUnit is "module-info.java"Gets the declared module reference if the compilationUnit is "module-info.java"Gets the declared packageList<CtTypeReference<?>>
Gets references to all the types declared in this compilation unit.Gets all the types declared in this compilation unit.getFile()
Gets the file that corresponds to this compilation unit if any (contains the source code).Get the imports computed for this CU.int[]
CtType<?>
Searches and returns the main type (the type which has the same name as the file).Gets the original source code as a string.Gets the parent of current reference.Returns the declaration type of the compilation unit.setDeclaredModule(CtModule module)
Sets the declared module if the compilationUnit is "module-info.java" It is here for backward compatibility.Sets the declared module reference if the compilationUnit is "module-info.java"setDeclaredPackage(CtPackage ctPackage)
Sets the package declaration using the instance of CtPackage.setDeclaredTypeReferences(List<CtTypeReference<?>> types)
Sets the references to types declared in this compilation unit.setDeclaredTypes(List<CtType<?>> types)
Sets the types declared in this compilation unit.Sets the file that corresponds to this compilation unit.setImports(Collection<CtImport> imports)
Set the imports of this CU WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.setLineSeparatorPositions(int[] lineSeparatorPositions)
setPackageDeclaration(CtPackageDeclaration packageDeclaration)
Sets the package declaration<E extends CtElement>
EManually sets the parent element of the current element.<E extends CtElement>
EsetPosition(SourcePosition position)
Sets the position in the Java source file.Methods inherited from interface spoon.reflect.declaration.CtElement
addAnnotation, addComment, asIterable, delete, descendantIterator, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getMetadata, getMetadataKeys, getOriginalSourceFragment, getParent, getParent, getPath, getPosition, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hasParent, isImplicit, isParentInitialized, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setComments, setDocComment, setImplicit, setPositions, setValueByRole, toString, toStringDebug, updateAllParentsBelow
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
Methods inherited from interface spoon.reflect.visitor.CtVisitable
accept
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Method Details
-
getUnitType
CtCompilationUnit.UNIT_TYPE getUnitType()Returns the declaration type of the compilation unit. -
getFile
File getFile()Gets the file that corresponds to this compilation unit if any (contains the source code). -
setFile
Sets the file that corresponds to this compilation unit. -
getLineSeparatorPositions
int[] getLineSeparatorPositions()- Returns:
- array of offsets in the origin source file, where occurs line separator
-
setLineSeparatorPositions
- Parameters:
lineSeparatorPositions
- array of offsets in the origin source file, where occurs line separator
-
getBinaryFiles
Gets all binary (.class) files that corresponds to this compilation unit and have been created by callingSpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...)
. -
getDeclaredTypes
Gets all the types declared in this compilation unit. -
getDeclaredTypeReferences
List<CtTypeReference<?>> getDeclaredTypeReferences()Gets references to all the types declared in this compilation unit. -
setDeclaredTypeReferences
Sets the references to types declared in this compilation unit. -
setDeclaredTypes
Sets the types declared in this compilation unit. It is here for backward compatibility. It calls internallysetDeclaredTypeReferences(List)
so theCtCompilationUnit
contains type reference only. It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model. Note thatCtCompilationUnit
represents a secondary model related to mapping of java modules, packages and types to file system. -
addDeclaredType
Add a type to the list of declared types. It is here for backward compatibility. It calls internallyaddDeclaredTypeReference(CtTypeReference)
so theCtCompilationUnit
contains type reference only. It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model. Note thatCtCompilationUnit
represents a secondary model related to mapping of java modules, packages and types to file system. -
addDeclaredTypeReference
Add a type reference to the list of declared types -
getDeclaredModule
CtModule getDeclaredModule()Gets the declared module if the compilationUnit is "module-info.java" -
getDeclaredModuleReference
CtModuleReference getDeclaredModuleReference()Gets the declared module reference if the compilationUnit is "module-info.java" -
setDeclaredModule
Sets the declared module if the compilationUnit is "module-info.java" It is here for backward compatibility. It internally callssetDeclaredModuleReference(CtModuleReference)
It doesn't contain whole CtModule, which belongs to CtModel in primary `java concept` model. Note thatCtCompilationUnit
represents a secondary model related to mapping of java modules, packages and types to file system. -
setDeclaredModuleReference
Sets the declared module reference if the compilationUnit is "module-info.java" -
getDeclaredPackage
CtPackage getDeclaredPackage()Gets the declared package -
getPackageDeclaration
CtPackageDeclaration getPackageDeclaration()- Returns:
- the package declaration
-
setDeclaredPackage
Sets the package declaration using the instance of CtPackage. It is here for backward compatibility. It calls internallysetPackageDeclaration(CtPackageDeclaration)
It doesn't contain whole CtPackage, which belongs to it's parent package or to CtModule in primary `java concept` model. Note thatCtCompilationUnit
represents a secondary model related to mapping of java modules, packages and types to file system. -
setPackageDeclaration
Sets the package declaration -
getMainType
CtType<?> getMainType()Searches and returns the main type (the type which has the same name as the file). -
getOriginalSourceCode
String getOriginalSourceCode()Gets the original source code as a string. -
getImports
Get the imports computed for this CU. WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.- Returns:
- All the imports from the original source code
-
clone
CtCompilationUnit clone()Description copied from interface:CtElement
Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methodsRefactoring.copyType(CtType)
andRefactoring.copyMethod(CtMethod)
instead which does additional work beyond cloning. -
setImports
Set the imports of this CU WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.- Parameters:
imports
- All the imports of the original source code
-
getParent
CtElement getParent()Description copied from interface:CtElement
Gets the parent of current reference. -
setParent
Description copied from interface:CtElement
Manually sets the parent element of the current element. -
setPosition
Description copied from interface:CtElement
Sets the position in the Java source file. Note that this information is used to feed the line numbers in the generated bytecode if any (which is useful for debugging).- Specified by:
setPosition
in interfaceCtElement
- Parameters:
position
- of this element in the input source files
-