Class JDTBasedSpoonCompiler

java.lang.Object
spoon.support.compiler.jdt.JDTBasedSpoonCompiler
All Implemented Interfaces:
SpoonModelBuilder
Direct Known Subclasses:
JDTSnippetCompiler

public class JDTBasedSpoonCompiler extends Object implements SpoonModelBuilder
Main class of Spoon to build the model. Highly depends on JDTBatchCompiler for performing the job.
  • Field Details

    • environment

      protected org.eclipse.jdt.internal.compiler.env.INameEnvironment environment
    • probs

      protected final List<org.eclipse.jdt.core.compiler.CategorizedProblem> probs
    • requestor

      protected final TreeBuilderRequestor requestor
    • factory

      protected Factory factory
    • javaCompliance

      protected int javaCompliance
    • sources

      protected SpoonFolder sources
    • templates

      protected SpoonFolder templates
    • templateClasspath

      protected String[] templateClasspath
    • compilationUnitFilters

      protected List<CompilationUnitFilter> compilationUnitFilters
  • Constructor Details

    • JDTBasedSpoonCompiler

      public JDTBasedSpoonCompiler(Factory factory)
      Default constructor
  • Method Details

    • build

      public boolean build()
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      build in interface SpoonModelBuilder
      Returns:
      true if the Java was successfully compiled with the core Java compiler, false if some errors were encountered while compiling
      See Also:
      SpoonModelBuilder.getSourceClasspath(), SpoonModelBuilder.getTemplateClasspath()
    • build

      public boolean build(JDTBuilder builder)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      build in interface SpoonModelBuilder
      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
      See Also:
      SpoonModelBuilder.getSourceClasspath(), SpoonModelBuilder.getTemplateClasspath()
    • compile

      public boolean compile(SpoonModelBuilder.InputType... types)
      Description copied from interface: SpoonModelBuilder
      Generates the bytecode associated to the classes stored in this compiler's factory. The bytecode is generated in the directory given by SpoonModelBuilder.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.
      Specified by:
      compile in interface SpoonModelBuilder
      See Also:
      SpoonModelBuilder.getSourceClasspath()
    • instantiateAndProcess

      public void instantiateAndProcess(List<String> processors)
      Description copied from interface: SpoonModelBuilder
      Takes a list of fully qualified name processors and instantiates them to process the Java model.
      Specified by:
      instantiateAndProcess in interface SpoonModelBuilder
    • process

      public void process(Collection<Processor<? extends CtElement>> processors)
      Description copied from interface: SpoonModelBuilder
      Processes the Java model with the given processors.
      Specified by:
      process in interface SpoonModelBuilder
    • generateProcessedSourceFiles

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

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

      public void addInputSource(File source)
      Description copied from interface: SpoonModelBuilder
      Adds a file/directory to be built. By default, the files could be Java source files or Jar files. Directories are processed recursively.
      Specified by:
      addInputSource in interface SpoonModelBuilder
      Parameters:
      source - file or directory to add
    • addInputSource

      public void addInputSource(SpoonResource source)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      addInputSource in interface SpoonModelBuilder
      Parameters:
      source - file or directory to add
    • addInputSources

      public void addInputSources(List<SpoonResource> resources)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      addInputSources in interface SpoonModelBuilder
      Parameters:
      resources - files or directories to add.
    • getInputSources

      public Set<File> getInputSources()
      Description copied from interface: SpoonModelBuilder
      Gets all the files/directories given as input sources to this builder (see SpoonModelBuilder.addInputSource(File)).
      Specified by:
      getInputSources in interface SpoonModelBuilder
    • addTemplateSource

      public void addTemplateSource(SpoonResource source)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      addTemplateSource in interface SpoonModelBuilder
      Parameters:
      source - file or directory to add
    • addTemplateSource

      public void addTemplateSource(File source)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      addTemplateSource in interface SpoonModelBuilder
      Parameters:
      source - file or directory to add
    • addTemplateSources

      public void addTemplateSources(List<SpoonResource> resources)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      addTemplateSources in interface SpoonModelBuilder
      Parameters:
      resources - files or directories to add.
    • getTemplateSources

      public Set<File> getTemplateSources()
      Description copied from interface: SpoonModelBuilder
      Gets all the files/directories given as template sources to this builder (see SpoonModelBuilder.addTemplateSource(File)).
      Specified by:
      getTemplateSources in interface SpoonModelBuilder
    • getSourceOutputDirectory

      public File getSourceOutputDirectory()
      Description copied from interface: SpoonModelBuilder
      Gets the output directory of this compiler.
      Specified by:
      getSourceOutputDirectory in interface SpoonModelBuilder
    • setBinaryOutputDirectory

      public void setBinaryOutputDirectory(File binaryOutputDirectory)
      Description copied from interface: SpoonModelBuilder
      Sets the output directory for binary generated.
      Specified by:
      setBinaryOutputDirectory in interface SpoonModelBuilder
      Parameters:
      binaryOutputDirectory - File for binary output directory.
    • getBinaryOutputDirectory

      public File getBinaryOutputDirectory()
      Description copied from interface: SpoonModelBuilder
      Gets the binary output directory of the compiler.
      Specified by:
      getBinaryOutputDirectory in interface SpoonModelBuilder
    • getSourceClasspath

      public String[] getSourceClasspath()
      Description copied from interface: SpoonModelBuilder
      Gets the classpath that is used to build/compile the input sources.
      Specified by:
      getSourceClasspath in interface SpoonModelBuilder
    • setSourceClasspath

      public void setSourceClasspath(String... classpath)
      Description copied from interface: SpoonModelBuilder
      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.
      Specified by:
      setSourceClasspath in interface SpoonModelBuilder
    • getTemplateClasspath

      public String[] getTemplateClasspath()
      Description copied from interface: SpoonModelBuilder
      Gets the classpath that is used to build the template sources. See SpoonModelBuilder.setSourceClasspath(java.lang.String...) for the meaning of the returned string.
      Specified by:
      getTemplateClasspath in interface SpoonModelBuilder
    • setTemplateClasspath

      public void setTemplateClasspath(String... classpath)
      Description copied from interface: SpoonModelBuilder
      Sets the classpath that is used to build the template sources.
      Specified by:
      setTemplateClasspath in interface SpoonModelBuilder
    • getFactory

      public Factory getFactory()
      Description copied from interface: SpoonModelBuilder
      Returns the working factory
      Specified by:
      getFactory in interface SpoonModelBuilder
    • buildSources

      protected boolean buildSources(JDTBuilder jdtBuilder)
    • createBatchCompiler

      protected JDTBatchCompiler createBatchCompiler()
    • createBatchCompiler

      protected JDTBatchCompiler createBatchCompiler(SpoonModelBuilder.InputType... types)
    • buildTemplates

      protected boolean buildTemplates(JDTBuilder jdtBuilder)
    • buildUnitsAndModel

      protected boolean buildUnitsAndModel(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix)
      Get the units from the given source folder and build the Spoon Model.
      Parameters:
      jdtBuilder - The instance of JDTBuilder to prepare the right JDT arguments
      sourcesFolder - The source folder
      classpath - The complete classpath
      debugMessagePrefix - Useful to help debugging
      Returns:
      true if the model has been built without errors
    • buildUnits

      protected org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix)
      Build the CompilationUnit found in the source folder
      Parameters:
      jdtBuilder - The instance of JDTBuilder to prepare the right JDT arguments
      sourcesFolder - The source folder
      classpath - The complete classpath
      debugMessagePrefix - Useful to help debugging
      Returns:
      All compilationUnitDeclaration from JDT found in source folder
    • sortCompilationUnits

      protected List<org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration> sortCompilationUnits(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] units)
    • buildModel

      protected void buildModel(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] units, Factory aFactory)
    • traverseUnitDeclaration

      protected void traverseUnitDeclaration(JDTTreeBuilder builder, org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration unitDeclaration)
      Invokes the traversal of the given compilation unit declaration using the given builder as a visitor. Overriders of this method must either invoke CompilationUnitDeclaration.traverse(ASTVisitor, CompilationUnitScope))} )} or this method before returning.
      Parameters:
      builder - the builder to use to traverse the unit.
      unitDeclaration - the unit declaration.
    • generateProcessedSourceFilesUsingTypes

      protected void generateProcessedSourceFilesUsingTypes(Filter<CtType<?>> typeFilter)
    • generateProcessedSourceFilesUsingCUs

      protected void generateProcessedSourceFilesUsingCUs()
    • setEnvironment

      public void setEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment environment)
    • reportProblem

      public void reportProblem(org.eclipse.jdt.core.compiler.CategorizedProblem pb)
      report a compilation problem (callback for JDT)
    • reportProblemsWhenCompiling

      public void reportProblemsWhenCompiling(Environment environment)
      Report problems as logs when compiling to binary. All "Error" problems trigger an exception
    • reportProblems

      public void reportProblems(Environment environment)
      Report problems as logs when building the AST model In in full classpath, a problem categorized as "Error" triggers an exception
    • report

      protected void report(Environment environment, org.eclipse.jdt.core.compiler.CategorizedProblem problem)
    • getProblems

      public List<org.eclipse.jdt.core.compiler.CategorizedProblem> getProblems()
      returns the list of current problems
    • getSource

      public SpoonFolder getSource()
    • getTemplates

      public SpoonFolder getTemplates()
    • getCompilationUnitInputStream

      protected InputStream getCompilationUnitInputStream(CompilationUnit cu)
    • getEnvironment

      protected Environment getEnvironment()
    • addCompilationUnitFilter

      public void addCompilationUnitFilter(CompilationUnitFilter filter)
      Description copied from interface: SpoonModelBuilder
      Adds filter.
      Specified by:
      addCompilationUnitFilter in interface SpoonModelBuilder
      Parameters:
      filter - The CompilationUnitFilter to add.
    • removeCompilationUnitFilter

      public void removeCompilationUnitFilter(CompilationUnitFilter filter)
      Description copied from interface: SpoonModelBuilder
      Removes filter. Does nothing, if filter has not been added beforehand.
      Specified by:
      removeCompilationUnitFilter in interface SpoonModelBuilder
      Parameters:
      filter - The CompilationUnitFilter to remove.
    • getCompilationUnitFilter

      public List<CompilationUnitFilter> getCompilationUnitFilter()
      Description copied from interface: SpoonModelBuilder
      Returns a copy of the internal list of CompilationUnitFilters.
      Specified by:
      getCompilationUnitFilter in interface SpoonModelBuilder
      Returns:
      A copy of the internal list of CompilationUnitFilters.