Package spoon

Interface SpoonModelBuilder

All Known Implementing Classes:
JDTBasedSpoonCompiler, JDTSnippetCompiler

public interface SpoonModelBuilder
Responsible for building a spoon model from Java source code.

The Spoon model (see Factory is built from input sources given as files. Use build() to create the Spoon model. Once the model is built and stored in the factory, it can be processed by using a instantiateAndProcess(List).

Create an instance of the default implementation of the Spoon compiler by using Launcher.createCompiler(). For example:

  • Method Details

    • build

      boolean build()
      Builds the program's model with this compiler's factory and stores the result into this factory. Note that this method should only be used once on a given factory.
      Returns:
      true if the Java was successfully compiled with the core Java compiler, false if some errors were encountered while compiling
      Throws:
      SpoonException - when a building problem occurs
      See Also:
      getSourceClasspath(), getTemplateClasspath()
    • build

      boolean build(JDTBuilder builder)
      Builds the program's model with this compiler's factory and stores the result into this factory. Note that this method should only be used once on a given factory.
      Parameters:
      builder - Parameters given at JDT compiler.
      Returns:
      true if the Java was successfully compiled with the core Java compiler, false if some errors were encountered while compiling
      Throws:
      SpoonException - when a building problem occurs
      See Also:
      getSourceClasspath(), getTemplateClasspath()
    • compile

      boolean compile(SpoonModelBuilder.InputType... types)
      Generates the bytecode associated to the classes stored in this compiler's factory. The bytecode is generated in the directory given by getBinaryOutputDirectory(). The array of types must be of size 0 or 1. If it's empty, the types of the factory are compiled. If it's InputType.FILES, the files given as input are compiled. Note that the varargs ... enables this version to be backward compatible for callers.
      See Also:
      getSourceClasspath()
    • instantiateAndProcess

      void instantiateAndProcess(List<String> processors)
      Takes a list of fully qualified name processors and instantiates them to process the Java model.
    • process

      void process(Collection<Processor<? extends CtElement>> processors)
      Processes the Java model with the given processors.
    • generateProcessedSourceFiles

      void generateProcessedSourceFiles(OutputType outputType)
      Generates the source code associated to the classes stored in this compiler's factory. The source code is generated in the directory given by getSourceOutputDirectory().
      Parameters:
      outputType - the output method
    • generateProcessedSourceFiles

      void generateProcessedSourceFiles(OutputType outputType, Filter<CtType<?>> typeFilter)
      Generates the source code associated to the classes stored in this compiler's factory. The source code is generated in the directory given by getSourceOutputDirectory().
      Parameters:
      outputType - the output method
      typeFilter - Filter on CtType to know which type Spoon must print.
    • addInputSource

      void addInputSource(File source)
      Adds a file/directory to be built. By default, the files could be Java source files or Jar files. Directories are processed recursively.
      Parameters:
      source - file or directory to add
    • addInputSource

      void addInputSource(SpoonResource source)
      Adds a file/directory (as a SpoonResource) to be built. By default, the files could be Java source files or Jar files. Directories are processed recursively.
      Parameters:
      source - file or directory to add
    • addInputSources

      void addInputSources(List<SpoonResource> resources)
      Adds a list of files/directories (as a SpoonResource to be built. By default, the files could be Java source files of Java files. Directories are processed recursively.
      Parameters:
      resources - files or directories to add.
    • getInputSources

      Set<File> getInputSources()
      Gets all the files/directories given as input sources to this builder (see addInputSource(File)).
    • addTemplateSource

      void addTemplateSource(File source)
      Adds a file/directory to be used to build templates. By default, the files should be Java source files or Jar files containing the sources. Directories are processed recursively. Templates are set apart from the program to be processed for logical reasons. However, if a template was needed to be processed, it could be added as an input source.
      Parameters:
      source - file or directory to add
    • addTemplateSource

      void addTemplateSource(SpoonResource source)
      Adds a file/directory (as a SpoonResource) to be used to build templates. By default, the files should be Java source files or Jar files containing the sources. Directories are processed recursively. Templates are set apart from the program to be processed for logical reasons. However, if a template was needed to be processed, it could be added as an input source.
      Parameters:
      source - file or directory to add
    • addTemplateSources

      void addTemplateSources(List<SpoonResource> resources)
      Adds a list of files/directories (as a CtResource) to be used to build templates. By default, the files should be Java source files or Jar files containing the sources. Directories are processed recursively. Templates are set apart from the program to be processed for logical reasons. However, if a template was needed to be processed, it could be added as an input source.
      Parameters:
      resources - files or directories to add.
    • getTemplateSources

      Set<File> getTemplateSources()
      Gets all the files/directories given as template sources to this builder (see addTemplateSource(File)).
    • getSourceOutputDirectory

      File getSourceOutputDirectory()
      Gets the output directory of this compiler.
    • setBinaryOutputDirectory

      void setBinaryOutputDirectory(File binaryOutputDirectory)
      Sets the output directory for binary generated.
      Parameters:
      binaryOutputDirectory - File for binary output directory.
    • getBinaryOutputDirectory

      File getBinaryOutputDirectory()
      Gets the binary output directory of the compiler.
    • getSourceClasspath

      String[] getSourceClasspath()
      Gets the classpath that is used to build/compile the input sources.
    • setSourceClasspath

      void setSourceClasspath(String... classpath)
      Sets the classpath that is used to build/compile the input sources. Each element of the array is either a jar file or a folder containing bytecode files.
    • getTemplateClasspath

      String[] getTemplateClasspath()
      Gets the classpath that is used to build the template sources. See setSourceClasspath(java.lang.String...) for the meaning of the returned string.
    • setTemplateClasspath

      void setTemplateClasspath(String... classpath)
      Sets the classpath that is used to build the template sources.
    • getFactory

      Factory getFactory()
      Returns the working factory
    • addCompilationUnitFilter

      void addCompilationUnitFilter(CompilationUnitFilter filter)
      Adds filter.
      Parameters:
      filter - The CompilationUnitFilter to add.
    • removeCompilationUnitFilter

      void removeCompilationUnitFilter(CompilationUnitFilter filter)
      Removes filter. Does nothing, if filter has not been added beforehand.
      Parameters:
      filter - The CompilationUnitFilter to remove.
    • getCompilationUnitFilter

      List<CompilationUnitFilter> getCompilationUnitFilter()
      Returns a copy of the internal list of CompilationUnitFilters.
      Returns:
      A copy of the internal list of CompilationUnitFilters.