Package spoon.support.compiler.jdt
Class JDTBasedSpoonCompiler
java.lang.Object
spoon.support.compiler.jdt.JDTBasedSpoonCompiler
- All Implemented Interfaces:
SpoonModelBuilder
- Direct Known Subclasses:
JDTSnippetCompiler
Main class of Spoon to build the model.
Highly depends on
JDTBatchCompiler for performing the job.-
Nested Class Summary
Nested classes/interfaces inherited from interface spoon.SpoonModelBuilder
SpoonModelBuilder.InputType -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<CompilationUnitFilter>protected org.eclipse.jdt.internal.compiler.env.INameEnvironmentprotected Factoryprotected intprotected List<org.eclipse.jdt.core.compiler.CategorizedProblem>protected TreeBuilderRequestorprotected SpoonFolderprotected String[]protected SpoonFolder -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAddsfilter.voidaddInputSource(File source)Adds a file/directory to be built.voidaddInputSource(SpoonResource source)Adds a file/directory (as aSpoonResource) to be built.voidaddInputSources(List<SpoonResource> resources)Adds a list of files/directories (as aSpoonResourceto be built.voidaddTemplateSource(File source)Adds a file/directory to be used to build templates.voidaddTemplateSource(SpoonResource source)Adds a file/directory (as aSpoonResource) to be used to build templates.voidaddTemplateSources(List<SpoonResource> resources)Adds a list of files/directories (as a CtResource) to be used to build templates.booleanbuild()Builds the program's model with this compiler's factory and stores the result into this factory.booleanbuild(JDTBuilder builder)Builds the program's model with this compiler's factory and stores the result into this factory.protected voidbuildModel(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] units, Factory aFactory)protected booleanbuildSources(JDTBuilder jdtBuilder)protected booleanbuildTemplates(JDTBuilder jdtBuilder)protected org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[]buildUnits(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix)Build the CompilationUnit found in the source folderprotected booleanbuildUnitsAndModel(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix)Get the units from the given source folder and build the Spoon Model.booleancompile(SpoonModelBuilder.InputType... types)Generates the bytecode associated to the classes stored in this compiler's factory.protected JDTBatchCompilerprotected JDTBatchCompilercreateBatchCompiler(SpoonModelBuilder.InputType... types)voidgenerateProcessedSourceFiles(OutputType outputType)Generates the source code associated to the classes stored in this compiler's factory.voidgenerateProcessedSourceFiles(OutputType outputType, Filter<CtType<?>> typeFilter)Generates the source code associated to the classes stored in this compiler's factory.protected voidprotected voidgenerateProcessedSourceFilesUsingTypes(Filter<CtType<?>> typeFilter)Gets the binary output directory of the compiler.Returns a copy of the internal list ofCompilationUnitFilters.protected InputStreamprotected EnvironmentReturns the working factoryGets all the files/directories given as input sources to this builder (seeSpoonModelBuilder.addInputSource(File)).List<org.eclipse.jdt.core.compiler.CategorizedProblem>returns the list of current problemsString[]Gets the classpath that is used to build/compile the input sources.Gets the output directory of this compiler.String[]Gets the classpath that is used to build the template sources.Gets all the files/directories given as template sources to this builder (seeSpoonModelBuilder.addTemplateSource(File)).voidinstantiateAndProcess(List<String> processors)Takes a list of fully qualified name processors and instantiates them to process the Java model.voidprocess(Collection<Processor<? extends CtElement>> processors)Processes the Java model with the given processors.voidRemovesfilter.protected voidreport(Environment environment, org.eclipse.jdt.core.compiler.CategorizedProblem problem)voidreportProblem(org.eclipse.jdt.core.compiler.CategorizedProblem pb)report a compilation problem (callback for JDT)voidreportProblems(Environment environment)Report problems as logs when building the AST model In in full classpath, a problem categorized as "Error" triggers an exceptionvoidreportProblemsWhenCompiling(Environment environment)Report problems as logs when compiling to binary.voidsetBinaryOutputDirectory(File binaryOutputDirectory)Sets the output directory for binary generated.voidsetEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment environment)voidsetSourceClasspath(String... classpath)Sets the classpath that is used to build/compile the input sources.voidsetTemplateClasspath(String... classpath)Sets the classpath that is used to build the template sources.protected List<org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration>sortCompilationUnits(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] units)protected voidtraverseUnitDeclaration(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.
-
Field Details
-
environment
protected org.eclipse.jdt.internal.compiler.env.INameEnvironment environment -
probs
-
requestor
-
factory
-
javaCompliance
protected int javaCompliance -
sources
-
templates
-
templateClasspath
-
compilationUnitFilters
-
-
Constructor Details
-
JDTBasedSpoonCompiler
Default constructor
-
-
Method Details
-
build
public boolean build()Description copied from interface:SpoonModelBuilderBuilds 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:
buildin interfaceSpoonModelBuilder- 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
Description copied from interface:SpoonModelBuilderBuilds 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:
buildin interfaceSpoonModelBuilder- 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
Description copied from interface:SpoonModelBuilderGenerates the bytecode associated to the classes stored in this compiler's factory. The bytecode is generated in the directory given bySpoonModelBuilder.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:
compilein interfaceSpoonModelBuilder- See Also:
SpoonModelBuilder.getSourceClasspath()
-
instantiateAndProcess
Description copied from interface:SpoonModelBuilderTakes a list of fully qualified name processors and instantiates them to process the Java model.- Specified by:
instantiateAndProcessin interfaceSpoonModelBuilder
-
process
Description copied from interface:SpoonModelBuilderProcesses the Java model with the given processors.- Specified by:
processin interfaceSpoonModelBuilder
-
generateProcessedSourceFiles
Description copied from interface:SpoonModelBuilderGenerates the source code associated to the classes stored in this compiler's factory. The source code is generated in the directory given bySpoonModelBuilder.getSourceOutputDirectory().- Specified by:
generateProcessedSourceFilesin interfaceSpoonModelBuilder- Parameters:
outputType- the output method
-
generateProcessedSourceFiles
Description copied from interface:SpoonModelBuilderGenerates the source code associated to the classes stored in this compiler's factory. The source code is generated in the directory given bySpoonModelBuilder.getSourceOutputDirectory().- Specified by:
generateProcessedSourceFilesin interfaceSpoonModelBuilder- Parameters:
outputType- the output methodtypeFilter- Filter on CtType to know which type Spoon must print.
-
addInputSource
Description copied from interface:SpoonModelBuilderAdds a file/directory to be built. By default, the files could be Java source files or Jar files. Directories are processed recursively.- Specified by:
addInputSourcein interfaceSpoonModelBuilder- Parameters:
source- file or directory to add
-
addInputSource
Description copied from interface:SpoonModelBuilderAdds a file/directory (as aSpoonResource) to be built. By default, the files could be Java source files or Jar files. Directories are processed recursively.- Specified by:
addInputSourcein interfaceSpoonModelBuilder- Parameters:
source- file or directory to add
-
addInputSources
Description copied from interface:SpoonModelBuilderAdds a list of files/directories (as aSpoonResourceto be built. By default, the files could be Java source files of Java files. Directories are processed recursively.- Specified by:
addInputSourcesin interfaceSpoonModelBuilder- Parameters:
resources- files or directories to add.
-
getInputSources
Description copied from interface:SpoonModelBuilderGets all the files/directories given as input sources to this builder (seeSpoonModelBuilder.addInputSource(File)).- Specified by:
getInputSourcesin interfaceSpoonModelBuilder
-
addTemplateSource
Description copied from interface:SpoonModelBuilderAdds a file/directory (as aSpoonResource) 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:
addTemplateSourcein interfaceSpoonModelBuilder- Parameters:
source- file or directory to add
-
addTemplateSource
Description copied from interface:SpoonModelBuilderAdds 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:
addTemplateSourcein interfaceSpoonModelBuilder- Parameters:
source- file or directory to add
-
addTemplateSources
Description copied from interface:SpoonModelBuilderAdds 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:
addTemplateSourcesin interfaceSpoonModelBuilder- Parameters:
resources- files or directories to add.
-
getTemplateSources
Description copied from interface:SpoonModelBuilderGets all the files/directories given as template sources to this builder (seeSpoonModelBuilder.addTemplateSource(File)).- Specified by:
getTemplateSourcesin interfaceSpoonModelBuilder
-
getSourceOutputDirectory
Description copied from interface:SpoonModelBuilderGets the output directory of this compiler.- Specified by:
getSourceOutputDirectoryin interfaceSpoonModelBuilder
-
setBinaryOutputDirectory
Description copied from interface:SpoonModelBuilderSets the output directory for binary generated.- Specified by:
setBinaryOutputDirectoryin interfaceSpoonModelBuilder- Parameters:
binaryOutputDirectory-Filefor binary output directory.
-
getBinaryOutputDirectory
Description copied from interface:SpoonModelBuilderGets the binary output directory of the compiler.- Specified by:
getBinaryOutputDirectoryin interfaceSpoonModelBuilder
-
getSourceClasspath
Description copied from interface:SpoonModelBuilderGets the classpath that is used to build/compile the input sources.- Specified by:
getSourceClasspathin interfaceSpoonModelBuilder
-
setSourceClasspath
Description copied from interface:SpoonModelBuilderSets 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:
setSourceClasspathin interfaceSpoonModelBuilder
-
getTemplateClasspath
Description copied from interface:SpoonModelBuilderGets the classpath that is used to build the template sources. SeeSpoonModelBuilder.setSourceClasspath(java.lang.String...)for the meaning of the returned string.- Specified by:
getTemplateClasspathin interfaceSpoonModelBuilder
-
setTemplateClasspath
Description copied from interface:SpoonModelBuilderSets the classpath that is used to build the template sources.- Specified by:
setTemplateClasspathin interfaceSpoonModelBuilder
-
getFactory
Description copied from interface:SpoonModelBuilderReturns the working factory- Specified by:
getFactoryin interfaceSpoonModelBuilder
-
buildSources
-
createBatchCompiler
-
createBatchCompiler
-
buildTemplates
-
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 argumentssourcesFolder- The source folderclasspath- The complete classpathdebugMessagePrefix- 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 argumentssourcesFolder- The source folderclasspath- The complete classpathdebugMessagePrefix- 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 invokeCompilationUnitDeclaration.traverse(ASTVisitor, CompilationUnitScope))} )} or this method before returning.- Parameters:
builder- the builder to use to traverse the unit.unitDeclaration- the unit declaration.
-
generateProcessedSourceFilesUsingTypes
-
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
Report problems as logs when compiling to binary. All "Error" problems trigger an exception -
reportProblems
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
returns the list of current problems -
getSource
-
getTemplates
-
getCompilationUnitInputStream
-
getEnvironment
-
addCompilationUnitFilter
Description copied from interface:SpoonModelBuilderAddsfilter.- Specified by:
addCompilationUnitFilterin interfaceSpoonModelBuilder- Parameters:
filter- TheCompilationUnitFilterto add.
-
removeCompilationUnitFilter
Description copied from interface:SpoonModelBuilderRemovesfilter. Does nothing, iffilterhas not been added beforehand.- Specified by:
removeCompilationUnitFilterin interfaceSpoonModelBuilder- Parameters:
filter- TheCompilationUnitFilterto remove.
-
getCompilationUnitFilter
Description copied from interface:SpoonModelBuilderReturns a copy of the internal list ofCompilationUnitFilters.- Specified by:
getCompilationUnitFilterin interfaceSpoonModelBuilder- Returns:
- A copy of the internal list of
CompilationUnitFilters.
-