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 (see main(String[])) for detailed usage.
  • Field Details

    • 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 Details

  • Method Details

    • main

      public static void main(String[] args)
      A default program entry point (instantiates a launcher with the given arguments and calls run()).
    • run

      public void run(String[] args)
      Description copied from interface: SpoonAPI
      Runs Spoon with these arguments (used by the "main" method)
      Specified by:
      run in interface SpoonAPI
    • 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 interface SpoonAPI
    • 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 interface SpoonAPI
    • 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 interface SpoonAPI
    • 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 interface SpoonAPI
    • 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 by createCompiler(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 interface SpoonAPI
    • 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 interface SpoonAPI
    • getFactory

      public Factory getFactory()
      Description copied from interface: SpoonAPI
      Returns the current factory
      Specified by:
      getFactory in interface SpoonAPI
    • createEnvironment

      public Environment createEnvironment()
      Description copied from interface: SpoonAPI
      Creates a new Spoon environment (may be overridden)
      Specified by:
      createEnvironment in interface SpoonAPI
    • 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:
      1. Source model building in the given compiler: SpoonModelBuilder.build().
      2. Template model building in the given factory (if any template source is given): SpoonModelBuilder.build().
      3. Model processing with the list of given processors if any: SpoonModelBuilder.instantiateAndProcess(List).
      4. Processed Source code printing and generation (can be disabled with OutputType.NO_OUTPUT): SpoonModelBuilder.generateProcessedSourceFiles(OutputType).
      5. Processed source code compilation (optional):
      Specified by:
      run in interface SpoonAPI
    • buildModel

      public CtModel buildModel()
      Description copied from interface: SpoonAPI
      Builds the model
      Specified by:
      buildModel in interface SpoonAPI
    • process

      public void process()
      Description copied from interface: SpoonAPI
      Processes the model with the processors given previously with SpoonAPI.addProcessor(String)
      Specified by:
      process in interface SpoonAPI
    • prettyprint

      public void prettyprint()
      Description copied from interface: SpoonAPI
      Write the transformed files to disk
      Specified by:
      prettyprint in interface SpoonAPI
    • 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 interface SpoonAPI
      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 interface SpoonAPI
      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 interface SpoonAPI
      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 interface SpoonAPI
      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 interface SpoonAPI
      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 interface SpoonAPI
      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 via SpoonAPI.addInputResource(String)
      Specified by:
      getModel in interface SpoonAPI
    • parseClass

      public static CtClass<?> parseClass(String code)
      returns the AST of an inline class