Class CtCompilationUnitImpl
java.lang.Object
spoon.support.reflect.declaration.CtElementImpl
spoon.support.reflect.declaration.CtCompilationUnitImpl
- All Implemented Interfaces:
Serializable
,Cloneable
,FactoryAccessor
,SourcePositionHolder
,CtCompilationUnit
,CtElement
,CtQueryable
,CtVisitable
- Direct Known Subclasses:
CompilationUnitImpl
Implements a compilation unit. In Java, a compilation unit can contain only one
public type declaration and other secondary types declarations (not public).
- See Also:
- Serialized Form
-
Nested Class Summary
Nested classes/interfaces inherited from interface spoon.reflect.declaration.CtCompilationUnit
CtCompilationUnit.UNIT_TYPE
-
Field Summary
Fields inherited from class spoon.support.reflect.declaration.CtElementImpl
ERROR_MESSAGE_TO_STRING, factory, LOGGER, parent
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Accepts a visitoraddDeclaredType(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.Returns the original source code (maybe different from toString() if a transformation has been applied).Gets the parent of current reference.Gets the position of this element in input source filesReturns 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.toString()
Methods inherited from class spoon.support.reflect.declaration.CtElementImpl
addAnnotation, addComment, asIterable, comment, delete, descendantIterator, emptyList, emptySet, equals, filterChildren, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getFactory, getMetadata, getMetadataKeys, getParent, getParent, getPath, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hashCode, hasParent, isImplicit, isParentInitialized, map, map, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setComments, setDocComment, setFactory, setImplicit, setPositions, setValueByRole, toStringDebug, unmodifiableList, updateAllParentsBelow
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface spoon.reflect.declaration.CtElement
addAnnotation, addComment, asIterable, delete, descendantIterator, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getMetadata, getMetadataKeys, getParent, getParent, getPath, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hasParent, isImplicit, isParentInitialized, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setComments, setDocComment, setImplicit, setPositions, setValueByRole, toStringDebug, updateAllParentsBelow
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Constructor Details
-
CtCompilationUnitImpl
public CtCompilationUnitImpl()
-
-
Method Details
-
getUnitType
Description copied from interface:CtCompilationUnit
Returns the declaration type of the compilation unit.- Specified by:
getUnitType
in interfaceCtCompilationUnit
-
getFile
Description copied from interface:CtCompilationUnit
Gets the file that corresponds to this compilation unit if any (contains the source code).- Specified by:
getFile
in interfaceCtCompilationUnit
-
getMainType
Description copied from interface:CtCompilationUnit
Searches and returns the main type (the type which has the same name as the file).- Specified by:
getMainType
in interfaceCtCompilationUnit
-
getDeclaredTypes
Description copied from interface:CtCompilationUnit
Gets all the types declared in this compilation unit.- Specified by:
getDeclaredTypes
in interfaceCtCompilationUnit
-
getDeclaredTypeReferences
Description copied from interface:CtCompilationUnit
Gets references to all the types declared in this compilation unit.- Specified by:
getDeclaredTypeReferences
in interfaceCtCompilationUnit
-
setDeclaredTypeReferences
Description copied from interface:CtCompilationUnit
Sets the references to types declared in this compilation unit.- Specified by:
setDeclaredTypeReferences
in interfaceCtCompilationUnit
-
setDeclaredTypes
Description copied from interface:CtCompilationUnit
Sets the types declared in this compilation unit. It is here for backward compatibility. It calls internallyCtCompilationUnit.setDeclaredTypeReferences(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.- Specified by:
setDeclaredTypes
in interfaceCtCompilationUnit
-
addDeclaredType
Description copied from interface:CtCompilationUnit
Add a type to the list of declared types. It is here for backward compatibility. It calls internallyCtCompilationUnit.addDeclaredTypeReference(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.- Specified by:
addDeclaredType
in interfaceCtCompilationUnit
-
addDeclaredTypeReference
Description copied from interface:CtCompilationUnit
Add a type reference to the list of declared types- Specified by:
addDeclaredTypeReference
in interfaceCtCompilationUnit
-
getDeclaredModule
Description copied from interface:CtCompilationUnit
Gets the declared module if the compilationUnit is "module-info.java"- Specified by:
getDeclaredModule
in interfaceCtCompilationUnit
-
getDeclaredModuleReference
Description copied from interface:CtCompilationUnit
Gets the declared module reference if the compilationUnit is "module-info.java"- Specified by:
getDeclaredModuleReference
in interfaceCtCompilationUnit
-
setDeclaredModule
Description copied from interface:CtCompilationUnit
Sets the declared module if the compilationUnit is "module-info.java" It is here for backward compatibility. It internally callsCtCompilationUnit.setDeclaredModuleReference(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.- Specified by:
setDeclaredModule
in interfaceCtCompilationUnit
-
setDeclaredModuleReference
Description copied from interface:CtCompilationUnit
Sets the declared module reference if the compilationUnit is "module-info.java"- Specified by:
setDeclaredModuleReference
in interfaceCtCompilationUnit
-
getDeclaredPackage
Description copied from interface:CtCompilationUnit
Gets the declared package- Specified by:
getDeclaredPackage
in interfaceCtCompilationUnit
-
getPackageDeclaration
- Specified by:
getPackageDeclaration
in interfaceCtCompilationUnit
- Returns:
- the package declaration
-
setDeclaredPackage
Description copied from interface:CtCompilationUnit
Sets the package declaration using the instance of CtPackage. It is here for backward compatibility. It calls internallyCtCompilationUnit.setPackageDeclaration(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.- Specified by:
setDeclaredPackage
in interfaceCtCompilationUnit
-
setPackageDeclaration
Description copied from interface:CtCompilationUnit
Sets the package declaration- Specified by:
setPackageDeclaration
in interfaceCtCompilationUnit
-
setFile
Description copied from interface:CtCompilationUnit
Sets the file that corresponds to this compilation unit.- Specified by:
setFile
in interfaceCtCompilationUnit
-
getBinaryFiles
Description copied from interface:CtCompilationUnit
Gets all binary (.class) files that corresponds to this compilation unit and have been created by callingSpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...)
.- Specified by:
getBinaryFiles
in interfaceCtCompilationUnit
-
getOriginalSourceCode
Description copied from interface:CtCompilationUnit
Gets the original source code as a string.- Specified by:
getOriginalSourceCode
in interfaceCtCompilationUnit
-
getImports
Description copied from interface:CtCompilationUnit
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.- Specified by:
getImports
in interfaceCtCompilationUnit
- Returns:
- All the imports from the original source code
-
setImports
Description copied from interface:CtCompilationUnit
Set the imports of this CU WARNING: This method is tagged as experimental, as its signature and/or usage might change in future release.- Specified by:
setImports
in interfaceCtCompilationUnit
- Parameters:
imports
- All the imports of the original source code
-
getOriginalSourceFragment
Description copied from interface:SourcePositionHolder
Returns the original source code (maybe different from toString() if a transformation has been applied). Warning: this is a advanced method which cannot be considered as part of the stable API- Specified by:
getOriginalSourceFragment
in interfaceCtElement
- Specified by:
getOriginalSourceFragment
in interfaceSourcePositionHolder
- Overrides:
getOriginalSourceFragment
in classCtElementImpl
-
getLineSeparatorPositions
public int[] getLineSeparatorPositions()- Specified by:
getLineSeparatorPositions
in interfaceCtCompilationUnit
- Returns:
- array of offsets in the origin source file, where occurs line separator
-
setLineSeparatorPositions
- Specified by:
setLineSeparatorPositions
in interfaceCtCompilationUnit
- Parameters:
lineSeparatorPositions
- array of offsets in the origin source file, where occurs line separator
-
getPosition
Description copied from interface:CtElement
Gets the position of this element in input source files- Specified by:
getPosition
in interfaceCtElement
- Specified by:
getPosition
in interfaceSourcePositionHolder
- Overrides:
getPosition
in classCtElementImpl
- Returns:
- Source file and line number of this element.
It never returns null. Use
SourcePosition.isValidPosition()
to detect whether return instance contains start/end indexes.
-
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 interfaceCtCompilationUnit
- Specified by:
setPosition
in interfaceCtElement
- Overrides:
setPosition
in classCtElementImpl
- Parameters:
position
- of this element in the input source files
-
getOrCreatePartialSourcePosition
- Returns:
- a
SourcePosition
which points to thisCompilationUnit
. It always returns same value to safe memory.
-
accept
Description copied from interface:CtVisitable
Accepts a visitor- Specified by:
accept
in interfaceCtVisitable
-
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.- Specified by:
clone
in interfaceCtCompilationUnit
- Specified by:
clone
in interfaceCtElement
- Overrides:
clone
in classCtElementImpl
-
getParent
Description copied from interface:CtElement
Gets the parent of current reference.- Specified by:
getParent
in interfaceCtCompilationUnit
- Specified by:
getParent
in interfaceCtElement
- Overrides:
getParent
in classCtElementImpl
- Throws:
ParentNotInitializedException
- when the parent of this element is not initialized
-
setParent
Description copied from interface:CtElement
Manually sets the parent element of the current element.- Specified by:
setParent
in interfaceCtCompilationUnit
- Specified by:
setParent
in interfaceCtElement
- Overrides:
setParent
in classCtElementImpl
- Type Parameters:
E
- this element's type- Parameters:
parent
- parent reference.- Returns:
- this element
-
toString
- Specified by:
toString
in interfaceCtElement
- Overrides:
toString
in classCtElementImpl
- Returns:
- the source code of this element according to the setting of
Environment.getPrettyPrintingMode()
.
-