Package spoon
Class Launcher
- java.lang.Object
-
- spoon.Launcher
-
- All Implemented Interfaces:
SpoonAPI
- Direct Known Subclasses:
IncrementalLauncher
,MavenLauncher
public class Launcher extends Object implements SpoonAPI
This class implements an integrated command-line launcher for processing programs at compile-time using the JDT-based builder (Eclipse). It takes arguments that allow building, processing, printing, and compiling Java programs. Launch with no arguments (seemain(String[])
) for detailed usage.
-
-
Field Summary
Fields Modifier and Type Field Description static org.apache.commons.io.filefilter.IOFileFilter
ALL_DIR_FILTER
protected Factory
factory
protected com.martiansoftware.jsap.JSAPResult
jsapActualArgs
static org.slf4j.Logger
LOGGER
A default logger to be used by Spoon.static String
OUTPUTDIR
static org.apache.commons.io.filefilter.IOFileFilter
RESOURCES_FILE_FILTER
static String
SPOONED_CLASSES
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addInputResource(String path)
Adds an input resource to be processed by Spoon (either a file or a folder).void
addInputResource(SpoonResource resource)
adds a resource to be parsed to build the spoon modelvoid
addProcessor(String name)
Adds a processor (fully qualified name).<T extends CtElement>
voidaddProcessor(Processor<T> processor)
Adds an instance of a processor.void
addTemplateResource(SpoonResource resource)
CtModel
buildModel()
Builds the modelSpoonModelBuilder
createCompiler()
Creates a new Spoon compiler (for building the model)SpoonModelBuilder
createCompiler(List<SpoonResource> inputSources)
Creates a new Spoon Java compiler with a default factory and a list of input sources.SpoonModelBuilder
createCompiler(Factory factory)
Creates a new Spoon Java compiler in order to process and compile Java source code.SpoonModelBuilder
createCompiler(Factory factory, List<SpoonResource> inputSources)
SpoonModelBuilder
createCompiler(Factory factory, List<SpoonResource> inputSources, List<SpoonResource> templateSources)
Creates a new Spoon Java compiler in order to process and compile Java source code.Environment
createEnvironment()
Creates a new Spoon environment (may be overridden)Factory
createFactory()
Creates a new Spoon factory (may be overridden)JavaOutputProcessor
createOutputWriter()
PrettyPrinter
createPrettyPrinter()
protected static com.martiansoftware.jsap.JSAP
defineArgs()
Defines the common arguments for sub-launchers.protected com.martiansoftware.jsap.JSAPResult
getArguments()
Returns the command-line given launching arguments in JSAP format.protected SpoonModelBuilder
getCompilerInstance(Factory factory)
Instantiates the compiler.Environment
getEnvironment()
Returns the current environment.Factory
getFactory()
Returns the current factoryCtModel
getModel()
Returns the model built from the sources given viaSpoonAPI.addInputResource(String)
SpoonModelBuilder
getModelBuilder()
protected List<Processor<? extends CtElement>>
getProcessors()
Gets the list of processors instance to be initially applied during the processing.protected List<String>
getProcessorTypes()
Gets the list of processor types to be initially applied during the processing (-p option).static void
main(String[] args)
A default program entry point (instantiates a launcher with the given arguments and callsrun()
).protected com.martiansoftware.jsap.JSAPResult
parseArgs()
Parses the arguments given by the command line.static CtClass<?>
parseClass(String code)
returns the AST of an inline classvoid
prettyprint()
Write the transformed files to diskvoid
printUsage()
void
process()
Processes the model with the processors given previously withSpoonAPI.addProcessor(String)
protected void
processArguments()
protected void
reportClassPathMode()
void
run()
Runs Spoon using the given compiler, with the given run options.void
run(String[] args)
Runs Spoon with these arguments (used by the "main" method)void
setArgs(String[] args2)
void
setBinaryOutputDirectory(File outputDirectory)
Sets the output directory for binary generated.void
setBinaryOutputDirectory(String path)
Sets the output directory for binary generated.void
setOutputFilter(String... qualifedNames)
Creates a filter from names in argument when the output-type is defined on classes.void
setOutputFilter(Filter<CtType<?>> typeFilter)
Applies a filter when the output-type is defined on classes.void
setSourceOutputDirectory(File outputDirectory)
Sets the output directory for source generated.void
setSourceOutputDirectory(String path)
Sets the output directory for source generated.
-
-
-
Field Detail
-
SPOONED_CLASSES
public static final String SPOONED_CLASSES
- See Also:
- Constant Field Values
-
OUTPUTDIR
public static final String OUTPUTDIR
- See Also:
- Constant Field Values
-
factory
protected Factory factory
-
jsapActualArgs
protected com.martiansoftware.jsap.JSAPResult jsapActualArgs
-
LOGGER
public static final org.slf4j.Logger LOGGER
A default logger to be used by Spoon.
-
RESOURCES_FILE_FILTER
public static final org.apache.commons.io.filefilter.IOFileFilter RESOURCES_FILE_FILTER
-
ALL_DIR_FILTER
public static final org.apache.commons.io.filefilter.IOFileFilter ALL_DIR_FILTER
-
-
Constructor Detail
-
Launcher
public Launcher()
-
Launcher
public Launcher(Factory pFactory)
- Parameters:
pFactory
- TheFactory
that will be utilized inbuildModel()
.- Throws:
IllegalArgumentException
- IfpFactory == null
.
-
-
Method Detail
-
main
public static void main(String[] args)
A default program entry point (instantiates a launcher with the given arguments and callsrun()
).
-
run
public void run(String[] args)
Description copied from interface:SpoonAPI
Runs Spoon with these arguments (used by the "main" method)
-
setArgs
public void setArgs(String[] args2)
-
printUsage
public void printUsage()
-
addInputResource
public void addInputResource(String path)
Description copied from interface:SpoonAPI
Adds an input resource to be processed by Spoon (either a file or a folder).- Specified by:
addInputResource
in interfaceSpoonAPI
-
addInputResource
public void addInputResource(SpoonResource resource)
adds a resource to be parsed to build the spoon model
-
addProcessor
public void addProcessor(String name)
Description copied from interface:SpoonAPI
Adds a processor (fully qualified name).- Specified by:
addProcessor
in interfaceSpoonAPI
-
addProcessor
public <T extends CtElement> void addProcessor(Processor<T> processor)
Description copied from interface:SpoonAPI
Adds an instance of a processor. The user is responsible for keeping a pointer to it for later retrieving some processing information.- Specified by:
addProcessor
in interfaceSpoonAPI
-
addTemplateResource
public void addTemplateResource(SpoonResource resource)
-
getEnvironment
public Environment getEnvironment()
Description copied from interface:SpoonAPI
Returns the current environment. This environment is modifiable.- Specified by:
getEnvironment
in interfaceSpoonAPI
-
defineArgs
protected static com.martiansoftware.jsap.JSAP defineArgs()
Defines the common arguments for sub-launchers.- Returns:
- the JSAP arguments
-
getArguments
protected final com.martiansoftware.jsap.JSAPResult getArguments()
Returns the command-line given launching arguments in JSAP format.
-
processArguments
protected void processArguments()
-
reportClassPathMode
protected void reportClassPathMode()
-
getProcessorTypes
protected List<String> getProcessorTypes()
Gets the list of processor types to be initially applied during the processing (-p option).
-
getProcessors
protected List<Processor<? extends CtElement>> getProcessors()
Gets the list of processors instance to be initially applied during the processing.
-
parseArgs
protected com.martiansoftware.jsap.JSAPResult parseArgs()
Parses the arguments given by the command line.- Returns:
- the JSAP-presented arguments
-
createCompiler
public SpoonModelBuilder createCompiler(Factory factory)
Creates a new Spoon Java compiler in order to process and compile Java source code.- Parameters:
factory
- the factory this compiler works on
-
getCompilerInstance
protected SpoonModelBuilder getCompilerInstance(Factory factory)
Instantiates the compiler. This method is invoked bycreateCompiler(Factory)
to retrieve an empty compiler instance. Clients can override this method to use their custom compiler implementation.- Parameters:
factory
- the factory to pass on to the compiler.- Returns:
- a new compiler.
- See Also:
createCompiler(Factory)
-
createCompiler
public SpoonModelBuilder createCompiler(Factory factory, List<SpoonResource> inputSources)
-
createCompiler
public SpoonModelBuilder createCompiler(Factory factory, List<SpoonResource> inputSources, List<SpoonResource> templateSources)
Creates a new Spoon Java compiler in order to process and compile Java source code.
-
createCompiler
public SpoonModelBuilder createCompiler()
Description copied from interface:SpoonAPI
Creates a new Spoon compiler (for building the model)- Specified by:
createCompiler
in interfaceSpoonAPI
-
createCompiler
public SpoonModelBuilder createCompiler(List<SpoonResource> inputSources)
Creates a new Spoon Java compiler with a default factory and a list of input sources.
-
createFactory
public Factory createFactory()
Description copied from interface:SpoonAPI
Creates a new Spoon factory (may be overridden)- Specified by:
createFactory
in interfaceSpoonAPI
-
getFactory
public Factory getFactory()
Description copied from interface:SpoonAPI
Returns the current factory- Specified by:
getFactory
in interfaceSpoonAPI
-
createEnvironment
public Environment createEnvironment()
Description copied from interface:SpoonAPI
Creates a new Spoon environment (may be overridden)- Specified by:
createEnvironment
in interfaceSpoonAPI
-
createOutputWriter
public JavaOutputProcessor createOutputWriter()
-
createPrettyPrinter
public PrettyPrinter createPrettyPrinter()
-
run
public void run()
Runs Spoon using the given compiler, with the given run options. A Spoon run will perform the following tasks:- Source model building in the given compiler:
SpoonModelBuilder.build()
. - Template model building in the given factory (if any template source
is given):
SpoonModelBuilder.build()
. - Model processing with the list of given processors if any:
SpoonModelBuilder.instantiateAndProcess(List)
. - Processed Source code printing and generation (can be disabled with
OutputType.NO_OUTPUT
):SpoonModelBuilder.generateProcessedSourceFiles(OutputType)
. - Processed source code compilation (optional):
- Source model building in the given compiler:
-
buildModel
public CtModel buildModel()
Description copied from interface:SpoonAPI
Builds the model- Specified by:
buildModel
in interfaceSpoonAPI
-
process
public void process()
Description copied from interface:SpoonAPI
Processes the model with the processors given previously withSpoonAPI.addProcessor(String)
-
prettyprint
public void prettyprint()
Description copied from interface:SpoonAPI
Write the transformed files to disk- Specified by:
prettyprint
in interfaceSpoonAPI
-
getModelBuilder
public SpoonModelBuilder getModelBuilder()
-
setSourceOutputDirectory
public void setSourceOutputDirectory(String path)
Description copied from interface:SpoonAPI
Sets the output directory for source generated.- Specified by:
setSourceOutputDirectory
in interfaceSpoonAPI
- Parameters:
path
- Path for the output directory.
-
setSourceOutputDirectory
public void setSourceOutputDirectory(File outputDirectory)
Description copied from interface:SpoonAPI
Sets the output directory for source generated.- Specified by:
setSourceOutputDirectory
in interfaceSpoonAPI
- Parameters:
outputDirectory
-File
for output directory.
-
setOutputFilter
public void setOutputFilter(Filter<CtType<?>> typeFilter)
Description copied from interface:SpoonAPI
Applies a filter when the output-type is defined on classes.- Specified by:
setOutputFilter
in interfaceSpoonAPI
- Parameters:
typeFilter
- Filter on CtType to know which type Spoon must print.
-
setOutputFilter
public void setOutputFilter(String... qualifedNames)
Description copied from interface:SpoonAPI
Creates a filter from names in argument when the output-type is defined on classes.- Specified by:
setOutputFilter
in interfaceSpoonAPI
- Parameters:
qualifedNames
- Names of class in fully qualified name to know which type Spoon must print.
-
setBinaryOutputDirectory
public void setBinaryOutputDirectory(String path)
Description copied from interface:SpoonAPI
Sets the output directory for binary generated.- Specified by:
setBinaryOutputDirectory
in interfaceSpoonAPI
- Parameters:
path
- Path for the binary output directory.
-
setBinaryOutputDirectory
public void setBinaryOutputDirectory(File outputDirectory)
Description copied from interface:SpoonAPI
Sets the output directory for binary generated.- Specified by:
setBinaryOutputDirectory
in interfaceSpoonAPI
- Parameters:
outputDirectory
-File
for the binary output directory.
-
getModel
public CtModel getModel()
Description copied from interface:SpoonAPI
Returns the model built from the sources given viaSpoonAPI.addInputResource(String)
-
-