Interface CodeGenerator

  • All Implemented Interfaces:

    
    public interface CodeGenerator
    
                        

    CodeGenerator creates and manages files.

    Files created by CodeGenerator are considered in incremental processing. Kotlin and Java files will be compiled together with other source files in the module. Files created without using this API will not participate in incremental processing nor subsequent compilations.

    • Constructor Detail

    • Method Detail

      • createNewFile

         abstract OutputStream createNewFile(Dependencies dependencies, String packageName, String fileName, String extensionName)

        Creates a file which is managed by CodeGenerator

        Sources of corresponding KSNodes which are obtained directly from Resolver need to be specified. Namely, the containing files of those KSNodes who are obtained from:

        Instead of requiring processors to specify all source files which are relevant in generating the given output, KSP traces dependencies automatically and only needs to know those sources that only processors know what they are for. If a KSFile is indirectly obtained through other KSNodes, it hasn't to be specified for the given output, even if its contents contribute to the generation of the output.

        For example, a processor generates an output O after reading class A in A.kt and class B in B.kt, where A extends B. The processor got A by Resolver.getSymbolsWithAnnotation and then got B by KSClassDeclaration.superTypes from A. Because the inclusion of B is due to A, B.kt needn't to be specified in dependencies for O. Note that specifying B.kt in this case doesn't hurt, it is only unnecessary.

        Parameters:
        dependencies - are KSFiles from which this output is built.
        packageName - corresponds to the relative path of the generated file; using either '.'or '/' as separator.
        fileName - file name
        extensionName - If "kt" or "java", this file will participate in subsequent compilation.
        Returns:

        OutputStream for writing into files.

      • createNewFileByPath

         abstract OutputStream createNewFileByPath(Dependencies dependencies, String path, String extensionName)

        Creates a file which is managed by CodeGenerator

        Sources of corresponding KSNodes which are obtained directly from Resolver need to be specified. Namely, the containing files of those KSNodes who are obtained from:

        Instead of requiring processors to specify all source files which are relevant in generating the given output, KSP traces dependencies automatically and only needs to know those sources that only processors know what they are for. If a KSFile is indirectly obtained through other KSNodes, it hasn't to be specified for the given output, even if its contents contribute to the generation of the output.

        For example, a processor generates an output O after reading class A in A.kt and class B in B.kt, where A extends B. The processor got A by Resolver.getSymbolsWithAnnotation and then got B by KSClassDeclaration.superTypes from A. Because the inclusion of B is due to A, B.kt needn't to be specified in dependencies for O. Note that specifying B.kt in this case doesn't hurt, it is only unnecessary.

        Parameters:
        dependencies - are KSFiles from which this output is built.
        path - corresponds to the relative path of the generated file; includes the full file name
        Returns:

        OutputStream for writing into files.

      • associate

         abstract Unit associate(List<KSFile> sources, String packageName, String fileName, String extensionName)

        Associate sources to an output file.

        Parameters:
        sources - are KSFiles from which this output is built.
        packageName - corresponds to the relative path of the generated file; using either '.'or '/' as separator.
        fileName - file name
        extensionName - If "kt" or "java", this file will participate in subsequent compilation.
      • associateByPath

         abstract Unit associateByPath(List<KSFile> sources, String path, String extensionName)

        Associate sources to an output file.

        Parameters:
        sources - are KSFiles from which this output is built.
        path - corresponds to the relative path of the generated file; includes the full file name
      • associateWithClasses

         abstract Unit associateWithClasses(List<KSClassDeclaration> classes, String packageName, String fileName, String extensionName)

        Associate classes to an output file.

        Parameters:
        classes - are KSClassDeclarations from which this output is built.
        packageName - corresponds to the relative path of the generated file; using either '.'or '/' as separator.
        fileName - file name
        extensionName - If "kt" or "java", this file will participate in subsequent compilation.