Interface CtCompilationUnit

All Superinterfaces:
Cloneable, CtElement, CtQueryable, CtVisitable, FactoryAccessor, Serializable, SourcePositionHolder
All Known Subinterfaces:
CompilationUnit
All Known Implementing Classes:
CompilationUnitImpl, CtCompilationUnitImpl, NoSourcePosition.NullCompilationUnit

public interface CtCompilationUnit extends CtElement
Defines a compilation unit. In Java, a compilation unit can contain only one public type declaration and other secondary types declarations (not public).
  • Method Details

    • 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

      CtCompilationUnit setFile(File file)
      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

      CtCompilationUnit setLineSeparatorPositions(int[] lineSeparatorPositions)
      Parameters:
      lineSeparatorPositions - array of offsets in the origin source file, where occurs line separator
    • getBinaryFiles

      List<File> getBinaryFiles()
      Gets all binary (.class) files that corresponds to this compilation unit and have been created by calling SpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...).
    • getDeclaredTypes

      List<CtType<?>> 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

      CtCompilationUnit setDeclaredTypeReferences(List<CtTypeReference<?>> types)
      Sets the references to types declared in this compilation unit.
    • setDeclaredTypes

      CtCompilationUnit setDeclaredTypes(List<CtType<?>> types)
      Sets the types declared in this compilation unit. It is here for backward compatibility. It calls internally setDeclaredTypeReferences(List) so the CtCompilationUnit contains type reference only. It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model. Note that CtCompilationUnit represents a secondary model related to mapping of java modules, packages and types to file system.
    • addDeclaredType

      CtCompilationUnit addDeclaredType(CtType<?> type)
      Add a type to the list of declared types. It is here for backward compatibility. It calls internally addDeclaredTypeReference(CtTypeReference) so the CtCompilationUnit contains type reference only. It doesn't contain whole type, which belongs to it's CtPackage in primary `java concept` model. Note that CtCompilationUnit represents a secondary model related to mapping of java modules, packages and types to file system.
    • addDeclaredTypeReference

      CtCompilationUnit addDeclaredTypeReference(CtTypeReference<?> type)
      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

      CtCompilationUnit setDeclaredModule(CtModule module)
      Sets the declared module if the compilationUnit is "module-info.java" It is here for backward compatibility. It internally calls setDeclaredModuleReference(CtModuleReference) It doesn't contain whole CtModule, which belongs to CtModel in primary `java concept` model. Note that CtCompilationUnit represents a secondary model related to mapping of java modules, packages and types to file system.
    • setDeclaredModuleReference

      CtCompilationUnit setDeclaredModuleReference(CtModuleReference module)
      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

      CtCompilationUnit setDeclaredPackage(CtPackage ctPackage)
      Sets the package declaration using the instance of CtPackage. It is here for backward compatibility. It calls internally setPackageDeclaration(CtPackageDeclaration) It doesn't contain whole CtPackage, which belongs to it's parent package or to CtModule in primary `java concept` model. Note that CtCompilationUnit represents a secondary model related to mapping of java modules, packages and types to file system.
    • setPackageDeclaration

      CtCompilationUnit setPackageDeclaration(CtPackageDeclaration packageDeclaration)
      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

      ModelList<CtImport> 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

      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 methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtElement
    • setImports

      CtCompilationUnit 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.
      Parameters:
      imports - All the imports of the original source code
    • getParent

      CtElement getParent()
      Description copied from interface: CtElement
      Gets the parent of current reference.
      Specified by:
      getParent in interface CtElement
    • setParent

      <E extends CtElement> E setParent(CtElement parent)
      Description copied from interface: CtElement
      Manually sets the parent element of the current element.
      Specified by:
      setParent in interface CtElement
      Type Parameters:
      E - this element's type
      Parameters:
      parent - parent reference.
      Returns:
      this element
    • setPosition

      <E extends CtElement> E setPosition(SourcePosition position)
      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 interface CtElement
      Parameters:
      position - of this element in the input source files