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
Modifier and TypeFieldDescriptionprotected List<CompilationUnitFilter>
protected org.eclipse.jdt.internal.compiler.env.INameEnvironment
protected Factory
protected int
protected List<org.eclipse.jdt.core.compiler.CategorizedProblem>
protected TreeBuilderRequestor
protected SpoonFolder
protected String[]
protected SpoonFolder
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Addsfilter
.void
addInputSource(File source)
Adds a file/directory to be built.void
addInputSource(SpoonResource source)
Adds a file/directory (as aSpoonResource
) to be built.void
addInputSources(List<SpoonResource> resources)
Adds a list of files/directories (as aSpoonResource
to be built.void
addTemplateSource(File source)
Adds a file/directory to be used to build templates.void
addTemplateSource(SpoonResource source)
Adds a file/directory (as aSpoonResource
) to be used to build templates.void
addTemplateSources(List<SpoonResource> resources)
Adds a list of files/directories (as a CtResource) to be used to build templates.boolean
build()
Builds the program's model with this compiler's factory and stores the result into this factory.boolean
build(JDTBuilder builder)
Builds the program's model with this compiler's factory and stores the result into this factory.protected void
buildModel(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration[] units, Factory aFactory)
protected boolean
buildSources(JDTBuilder jdtBuilder)
protected boolean
buildTemplates(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 boolean
buildUnitsAndModel(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix)
Get the units from the given source folder and build the Spoon Model.boolean
compile(SpoonModelBuilder.InputType... types)
Generates the bytecode associated to the classes stored in this compiler's factory.protected JDTBatchCompiler
protected JDTBatchCompiler
createBatchCompiler(SpoonModelBuilder.InputType... types)
void
generateProcessedSourceFiles(OutputType outputType)
Generates the source code associated to the classes stored in this compiler's factory.void
generateProcessedSourceFiles(OutputType outputType, Filter<CtType<?>> typeFilter)
Generates the source code associated to the classes stored in this compiler's factory.protected void
protected void
generateProcessedSourceFilesUsingTypes(Filter<CtType<?>> typeFilter)
Gets the binary output directory of the compiler.Returns a copy of the internal list ofCompilationUnitFilter
s.protected InputStream
protected Environment
Returns 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)
).void
instantiateAndProcess(List<String> processors)
Takes a list of fully qualified name processors and instantiates them to process the Java model.void
process(Collection<Processor<? extends CtElement>> processors)
Processes the Java model with the given processors.void
Removesfilter
.protected void
report(Environment environment, org.eclipse.jdt.core.compiler.CategorizedProblem problem)
void
reportProblem(org.eclipse.jdt.core.compiler.CategorizedProblem pb)
report a compilation problem (callback for JDT)void
reportProblems(Environment environment)
Report problems as logs when building the AST model In in full classpath, a problem categorized as "Error" triggers an exceptionvoid
reportProblemsWhenCompiling(Environment environment)
Report problems as logs when compiling to binary.void
setBinaryOutputDirectory(File binaryOutputDirectory)
Sets the output directory for binary generated.void
setEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment environment)
void
setSourceClasspath(String... classpath)
Sets the classpath that is used to build/compile the input sources.void
setTemplateClasspath(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 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.
-
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: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 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: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 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:SpoonModelBuilder
Generates 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:
compile
in interfaceSpoonModelBuilder
- See Also:
SpoonModelBuilder.getSourceClasspath()
-
instantiateAndProcess
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 interfaceSpoonModelBuilder
-
process
Description copied from interface:SpoonModelBuilder
Processes the Java model with the given processors.- Specified by:
process
in interfaceSpoonModelBuilder
-
generateProcessedSourceFiles
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 bySpoonModelBuilder.getSourceOutputDirectory()
.- Specified by:
generateProcessedSourceFiles
in interfaceSpoonModelBuilder
- Parameters:
outputType
- the output method
-
generateProcessedSourceFiles
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 bySpoonModelBuilder.getSourceOutputDirectory()
.- Specified by:
generateProcessedSourceFiles
in interfaceSpoonModelBuilder
- Parameters:
outputType
- the output methodtypeFilter
- Filter on CtType to know which type Spoon must print.
-
addInputSource
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 interfaceSpoonModelBuilder
- Parameters:
source
- file or directory to add
-
addInputSource
Description copied from interface:SpoonModelBuilder
Adds 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:
addInputSource
in interfaceSpoonModelBuilder
- Parameters:
source
- file or directory to add
-
addInputSources
Description copied from interface:SpoonModelBuilder
Adds a list of files/directories (as aSpoonResource
to be built. By default, the files could be Java source files of Java files. Directories are processed recursively.- Specified by:
addInputSources
in interfaceSpoonModelBuilder
- Parameters:
resources
- files or directories to add.
-
getInputSources
Description copied from interface:SpoonModelBuilder
Gets all the files/directories given as input sources to this builder (seeSpoonModelBuilder.addInputSource(File)
).- Specified by:
getInputSources
in interfaceSpoonModelBuilder
-
addTemplateSource
Description copied from interface:SpoonModelBuilder
Adds 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:
addTemplateSource
in interfaceSpoonModelBuilder
- Parameters:
source
- file or directory to add
-
addTemplateSource
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 interfaceSpoonModelBuilder
- Parameters:
source
- file or directory to add
-
addTemplateSources
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 interfaceSpoonModelBuilder
- Parameters:
resources
- files or directories to add.
-
getTemplateSources
Description copied from interface:SpoonModelBuilder
Gets all the files/directories given as template sources to this builder (seeSpoonModelBuilder.addTemplateSource(File)
).- Specified by:
getTemplateSources
in interfaceSpoonModelBuilder
-
getSourceOutputDirectory
Description copied from interface:SpoonModelBuilder
Gets the output directory of this compiler.- Specified by:
getSourceOutputDirectory
in interfaceSpoonModelBuilder
-
setBinaryOutputDirectory
Description copied from interface:SpoonModelBuilder
Sets the output directory for binary generated.- Specified by:
setBinaryOutputDirectory
in interfaceSpoonModelBuilder
- Parameters:
binaryOutputDirectory
-File
for binary output directory.
-
getBinaryOutputDirectory
Description copied from interface:SpoonModelBuilder
Gets the binary output directory of the compiler.- Specified by:
getBinaryOutputDirectory
in interfaceSpoonModelBuilder
-
getSourceClasspath
Description copied from interface:SpoonModelBuilder
Gets the classpath that is used to build/compile the input sources.- Specified by:
getSourceClasspath
in interfaceSpoonModelBuilder
-
setSourceClasspath
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 interfaceSpoonModelBuilder
-
getTemplateClasspath
Description copied from interface:SpoonModelBuilder
Gets the classpath that is used to build the template sources. SeeSpoonModelBuilder.setSourceClasspath(java.lang.String...)
for the meaning of the returned string.- Specified by:
getTemplateClasspath
in interfaceSpoonModelBuilder
-
setTemplateClasspath
Description copied from interface:SpoonModelBuilder
Sets the classpath that is used to build the template sources.- Specified by:
setTemplateClasspath
in interfaceSpoonModelBuilder
-
getFactory
Description copied from interface:SpoonModelBuilder
Returns the working factory- Specified by:
getFactory
in 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:SpoonModelBuilder
Addsfilter
.- Specified by:
addCompilationUnitFilter
in interfaceSpoonModelBuilder
- Parameters:
filter
- TheCompilationUnitFilter
to add.
-
removeCompilationUnitFilter
Description copied from interface:SpoonModelBuilder
Removesfilter
. Does nothing, iffilter
has not been added beforehand.- Specified by:
removeCompilationUnitFilter
in interfaceSpoonModelBuilder
- Parameters:
filter
- TheCompilationUnitFilter
to remove.
-
getCompilationUnitFilter
Description copied from interface:SpoonModelBuilder
Returns a copy of the internal list ofCompilationUnitFilter
s.- Specified by:
getCompilationUnitFilter
in interfaceSpoonModelBuilder
- Returns:
- A copy of the internal list of
CompilationUnitFilter
s.
-