Package spoon.support

Class StandardEnvironment

java.lang.Object
spoon.support.StandardEnvironment
All Implemented Interfaces:
Serializable, Environment

public class StandardEnvironment extends Object implements Serializable, Environment
This class implements a simple Spoon environment that reports messages in the standard output stream (Java-compliant).
See Also:
Serialized Form
  • Field Details

    • DEFAULT_CODE_COMPLIANCE_LEVEL

      public static final int DEFAULT_CODE_COMPLIANCE_LEVEL
      See Also:
      Constant Field Values
  • Constructor Details

    • StandardEnvironment

      public StandardEnvironment()
      Creates a new environment with a null default file generator.
  • Method Details

    • getPrettyPrintingMode

      public Environment.PRETTY_PRINTING_MODE getPrettyPrintingMode()
      Description copied from interface: Environment
      Get the current pretty-printing mode. most robust: Environment.PRETTY_PRINTING_MODE.DEBUG most sophisticated: Environment.PRETTY_PRINTING_MODE.AUTOIMPORT
      Specified by:
      getPrettyPrintingMode in interface Environment
      Returns:
      the kind of pretty-printing expected.
    • setPrettyPrintingMode

      public void setPrettyPrintingMode(Environment.PRETTY_PRINTING_MODE prettyPrintingMode)
      Specified by:
      setPrettyPrintingMode in interface Environment
    • debugMessage

      public void debugMessage(String message)
      Description copied from interface: Environment
      Logs a debug message
      Specified by:
      debugMessage in interface Environment
      Parameters:
      message - a message to write to the debug log
    • isAutoImports

      public boolean isAutoImports()
      Description copied from interface: Environment
      Returns true if we let Spoon handle imports and turn fully qualified type names int simply qualified names.
      Specified by:
      isAutoImports in interface Environment
      Returns:
      true iff Spoon is set to automatically import types and simplify type names
    • setAutoImports

      public void setAutoImports(boolean autoImports)
      Description copied from interface: Environment
      Tell to the Java printer to automatically generate imports and use simple names instead of fully-qualified name.
      Specified by:
      setAutoImports in interface Environment
      Parameters:
      autoImports - whether Spoon should auto-import types and simplify names
    • getDefaultFileGenerator

      public FileGenerator<? extends CtElement> getDefaultFileGenerator()
      Description copied from interface: Environment
      Returns the default file generator for this environment (gives the default output directory for the created files).
      Specified by:
      getDefaultFileGenerator in interface Environment
      Returns:
      the default file generator
    • getLevel

      public Level getLevel()
      Description copied from interface: Environment
      Gets the level of loggers asked by the user.
      Specified by:
      getLevel in interface Environment
      Returns:
      the current logging level
    • setLevel

      public void setLevel(String level)
      Description copied from interface: Environment
      Sets the level of loggers asked by the user.
      Specified by:
      setLevel in interface Environment
      Parameters:
      level - the logging level to set, see Level for options
    • shouldCompile

      public boolean shouldCompile()
      Description copied from interface: Environment
      Checks if we want compile the target source code and get their binary.
      Specified by:
      shouldCompile in interface Environment
      Returns:
      true iff Spoon should compile target source code
    • setShouldCompile

      public void setShouldCompile(boolean shouldCompile)
      Description copied from interface: Environment
      Sets the compile argument.
      Specified by:
      setShouldCompile in interface Environment
      Parameters:
      shouldCompile - whether Spoon should compile target source code
    • checksAreSkipped

      public boolean checksAreSkipped()
      Description copied from interface: Environment
      Tells whether Spoon does no checks at all. - parents are consistent (see AstParentConsistencyChecker) - hashcode violation (see CtElementImpl.equals(Object)) - method violation (see CtType.addMethod(CtMethod)) are active or not. By default all checks are enabled and Environment.checksAreSkipped() return false.
      Specified by:
      checksAreSkipped in interface Environment
      Returns:
      true iff Spoon skips consistency checks
    • disableConsistencyChecks

      public void disableConsistencyChecks()
      Description copied from interface: Environment
      Disable all consistency checks on the AST. Dangerous! The only valid usage of this is to keep full backward-compatibility.
      Specified by:
      disableConsistencyChecks in interface Environment
    • getManager

      public ProcessingManager getManager()
      Description copied from interface: Environment
      Gets the processing manager.
      Specified by:
      getManager in interface Environment
      Returns:
      the current processing manager
    • getProcessorProperties

      public ProcessorProperties getProcessorProperties(String processorName)
      Description copied from interface: Environment
      Returns the properties for a given processor.
      Specified by:
      getProcessorProperties in interface Environment
      Parameters:
      processorName - fully qualified name of a processor
      Returns:
      properties for the processor, or null if there is no processor by that name
    • isProcessingStopped

      public boolean isProcessingStopped()
      Tells if the processing is stopped, generally because one of the processors called setProcessingStopped(boolean) after reporting an error.
      Specified by:
      isProcessingStopped in interface Environment
      Returns:
      true iff processing has been forcibly aborted
    • report

      public void report(Processor<?> processor, Level level, CtElement element, String message)
      Description copied from interface: Environment
      Helper method called by a processor to report an error, warning or message as dictated by the severity parameter. Note that this does not stop the processing or any remaining task. To do so, use Environment.setProcessingStopped(boolean).
      Specified by:
      report in interface Environment
      Parameters:
      processor - The processor that report this message. Can be null.
      level - The level of the report
      element - The CtElement to which the report is associated
      message - The message to report
    • report

      public void report(Processor<?> processor, Level level, String message)
      Description copied from interface: Environment
      This method should be called to print out a message during the processing.
      Specified by:
      report in interface Environment
      Parameters:
      processor - The processor that report this message. Can be null.
      level - The level of the report
      message - The message to report
    • reportEnd

      public void reportEnd()
      This method should be called to report the end of the processing.
      Specified by:
      reportEnd in interface Environment
    • reportProgressMessage

      public void reportProgressMessage(String message)
      Description copied from interface: Environment
      This method should be called to print out a progress message during the processing. On contrary to regular messages, progress messages are not meant to remain in the message logs and just indicate to the user some task progression information.
      Specified by:
      reportProgressMessage in interface Environment
      Parameters:
      message - a message to print
    • setDebug

      public void setDebug(boolean debug)
    • setDefaultFileGenerator

      public void setDefaultFileGenerator(FileGenerator<? extends CtElement> defaultFileGenerator)
      Description copied from interface: Environment
      Sets the default file generator for this environment.
      Specified by:
      setDefaultFileGenerator in interface Environment
      Parameters:
      defaultFileGenerator - a file generator to set as the default
    • setManager

      public void setManager(ProcessingManager manager)
      Description copied from interface: Environment
      Sets the processing manager of this environment.
      Specified by:
      setManager in interface Environment
      Parameters:
      manager - a processing manager to set as the default
    • setProcessingStopped

      public void setProcessingStopped(boolean processingStopped)
      Description copied from interface: Environment
      This method can be called to stop the processing and all the remaining tasks. In general, a processor calls it after reporting a fatal error.
      Specified by:
      setProcessingStopped in interface Environment
      Parameters:
      processingStopped - if true, any ongoing processing is aborted as soon as possible and future processing is prohibited
    • setVerbose

      public void setVerbose(boolean verbose)
    • getComplianceLevel

      public int getComplianceLevel()
      Description copied from interface: Environment
      Gets the Java version compliance level.
      Specified by:
      getComplianceLevel in interface Environment
      Returns:
      the compliance level
    • setComplianceLevel

      public void setComplianceLevel(int level)
      Description copied from interface: Environment
      Sets the Java version compliance level.
      Specified by:
      setComplianceLevel in interface Environment
      Parameters:
      level - the compliance level
    • isPreviewFeaturesEnabled

      public boolean isPreviewFeaturesEnabled()
      Description copied from interface: Environment
      Returns true if preview language features are enabled.
      Specified by:
      isPreviewFeaturesEnabled in interface Environment
      Returns:
      true iff preview features are enabled
    • setPreviewFeaturesEnabled

      public void setPreviewFeaturesEnabled(boolean previewFeaturesEnabled)
      Description copied from interface: Environment
      Set to true to enable latest preview language features. Note: compliance level should be set to the latest.
      Specified by:
      setPreviewFeaturesEnabled in interface Environment
      Parameters:
      previewFeaturesEnabled - whether to enable preview features
    • setProcessorProperties

      public void setProcessorProperties(String processorName, ProcessorProperties prop)
      Description copied from interface: Environment
      Sets the properties for a given processor.
      Specified by:
      setProcessorProperties in interface Environment
      Parameters:
      processorName - fully qualified name of the processor
      prop - properties to set
    • isUsingTabulations

      public boolean isUsingTabulations()
      Description copied from interface: Environment
      Tells if Spoon uses tabulations in the source code.
      Specified by:
      isUsingTabulations in interface Environment
      Returns:
      true iff Spoon uses tabulations when pretty-printing
    • useTabulations

      public void useTabulations(boolean tabulation)
      Description copied from interface: Environment
      Sets Spoon to use tabulations in the source code.
      Specified by:
      useTabulations in interface Environment
      Parameters:
      tabulation - whether Spoon should use tabulations when pretty-printing
    • getTabulationSize

      public int getTabulationSize()
      Description copied from interface: Environment
      Gets the size of the tabulations in the generated source code.
      Specified by:
      getTabulationSize in interface Environment
      Returns:
      the current tabulation size
    • setTabulationSize

      public void setTabulationSize(int tabulationSize)
      Description copied from interface: Environment
      Sets the size of the tabulations in the generated source code.
      Specified by:
      setTabulationSize in interface Environment
      Parameters:
      tabulationSize - tabulation size to set
    • setInputClassLoader

      public void setInputClassLoader(ClassLoader aClassLoader)
      Description copied from interface: Environment
      Sets a specific classloader for JDT and reference resolution
      Specified by:
      setInputClassLoader in interface Environment
      Parameters:
      aClassLoader - a classloader to set
    • getInputClassLoader

      public ClassLoader getInputClassLoader()
      Description copied from interface: Environment
      Returns the ClassLoader which is used by JDT and to resolve classes from references. By default, returns a class loader able to load classes from the Spoon-specific class path set with Environment.setSourceClasspath(String[])
      Specified by:
      getInputClassLoader in interface Environment
      Returns:
      the currently configured classloader
    • urlClasspath

      public URL[] urlClasspath()
      Creates a URL class path from Environment.getSourceClasspath()
    • getSourceClasspath

      public String[] getSourceClasspath()
      Description copied from interface: Environment
      Returns the source class path of the Spoon model. This class path is used when the SpoonCompiler is building the model and also to find external classes, referenced from within the model.
      Specified by:
      getSourceClasspath in interface Environment
      Returns:
      all paths in the classpath
    • setSourceClasspath

      public void setSourceClasspath(String[] sourceClasspath)
      Description copied from interface: Environment
      Sets the source class path of the Spoon model. After the class path is set, it can be retrieved by Environment.getSourceClasspath(). Only .jar files or directories with *.class files are accepted. The *.jar or *.java files contained in given directories are ignored.
      Specified by:
      setSourceClasspath in interface Environment
      Parameters:
      sourceClasspath - classpath to set
    • getErrorCount

      public int getErrorCount()
      Description copied from interface: Environment
      Gets the error count from building, processing, and compiling within this environment.
      Specified by:
      getErrorCount in interface Environment
      Returns:
      the amount of errors that have occurred
    • getWarningCount

      public int getWarningCount()
      Description copied from interface: Environment
      Gets the warning count from building, processing, and compiling within this environment.
      Specified by:
      getWarningCount in interface Environment
      Returns:
      the amount of warnings that have occurred
    • isPreserveLineNumbers

      public boolean isPreserveLineNumbers()
      Description copied from interface: Environment
      Tells if the source generator will try to preserve the original line numbers.
      Specified by:
      isPreserveLineNumbers in interface Environment
      Returns:
      true iff Spoon attempts to preserve line numbers of elements when pretty-printing
    • setPreserveLineNumbers

      public void setPreserveLineNumbers(boolean preserveLineNumbers)
      Description copied from interface: Environment
      When set, the generated source code will try to generate code that preserves the line numbers of the original source code. This option may lead to difficult-to-read indentation and formatting.
      Specified by:
      setPreserveLineNumbers in interface Environment
      Parameters:
      preserveLineNumbers - whether Spoon should attempt to preserve line numbers when pretty-printing
    • setNoClasspath

      public void setNoClasspath(boolean option)
      Description copied from interface: Environment
      Sets the option "noclasspath", use with caution (see explanation below). With this option, Spoon does not require the full classpath to build the model. In this case, all references to classes that are not in the classpath are handled with the reference mechanism. The "simplename" of the reference object refers to the unbound identifier. This option facilitates the use of Spoon when it is hard to have the complete and correct classpath, for example for mining software repositories. For writing analyses, this option works well if you don't cross the reference by a call to getDeclaration() (if you really want to do so, then check for nullness of the result before). In normal mode, compilation errors are signaled as exception, with this option enabled they are signaled as message only. The reason is that in most cases, there are necessarily errors related to the missing classpath elements.
      Specified by:
      setNoClasspath in interface Environment
      Parameters:
      option - whether to set Spoon to noclasspath mode
    • getNoClasspath

      public boolean getNoClasspath()
      Description copied from interface: Environment
      Returns the value ot the option noclasspath
      Specified by:
      getNoClasspath in interface Environment
      Returns:
      true iff Spoon is currently in noclasspath mode
    • setIgnoreSyntaxErrors

      public void setIgnoreSyntaxErrors(boolean ignoreSyntaxErrors)
      Description copied from interface: Environment
      Sets the option ignore-syntax-errors to remove files with any syntax errors or JLS violations from the compilation batch. Also while transformations no checks for JLS correctness are reported as error.
      Specified by:
      setIgnoreSyntaxErrors in interface Environment
      Parameters:
      ignoreSyntaxErrors - whether Spoon should ignore files with any syntax errors or JLS violations
    • getIgnoreSyntaxErrors

      public boolean getIgnoreSyntaxErrors()
      Description copied from interface: Environment
      Returns the value ot the option ignore-syntax-errors.
      Specified by:
      getIgnoreSyntaxErrors in interface Environment
      Returns:
      true iff Spoon ignores files with any syntax errors, JLS violations or reports JLS correctness problems as exception.
    • isCopyResources

      public boolean isCopyResources()
      Description copied from interface: Environment
      Returns the value of the option copy-resources.
      Specified by:
      isCopyResources in interface Environment
      Returns:
      true iff Spoon should copy resource files from the project when pretty-printing
    • setCopyResources

      public void setCopyResources(boolean copyResources)
      Description copied from interface: Environment
      Sets the option copy-resources to copy all resources in a project on the folder destination.
      Specified by:
      setCopyResources in interface Environment
      Parameters:
      copyResources - whether Spoon should copy resources
    • isCommentsEnabled

      public boolean isCommentsEnabled()
      Description copied from interface: Environment
      Returns the value of the option enable-comments.
      Specified by:
      isCommentsEnabled in interface Environment
      Returns:
      true iff Spoon respects comments in source code
    • setCommentEnabled

      public void setCommentEnabled(boolean commentEnabled)
      Description copied from interface: Environment
      Sets the option enable-comments to parse comments of the target project.
      Specified by:
      setCommentEnabled in interface Environment
      Parameters:
      commentEnabled - whether Spoon should respect comments in source code
    • setBinaryOutputDirectory

      public void setBinaryOutputDirectory(String s)
      Description copied from interface: Environment
      Set the directory where binary .class files are created
      Specified by:
      setBinaryOutputDirectory in interface Environment
      Parameters:
      s - path to set for the binary output directory
    • getBinaryOutputDirectory

      public String getBinaryOutputDirectory()
      Description copied from interface: Environment
      Get the directory where binary .class files are created
      Specified by:
      getBinaryOutputDirectory in interface Environment
      Returns:
      path to the binary output directory
    • setSourceOutputDirectory

      public void setSourceOutputDirectory(File directory)
      Description copied from interface: Environment
      Sets the directory where source files are written
      Specified by:
      setSourceOutputDirectory in interface Environment
      Parameters:
      directory - path to set for the source output directory
    • getSourceOutputDirectory

      public File getSourceOutputDirectory()
      Description copied from interface: Environment
      Returns the directory where source files are written
      Specified by:
      getSourceOutputDirectory in interface Environment
      Returns:
      path to the source output directory
    • setOutputDestinationHandler

      public void setOutputDestinationHandler(OutputDestinationHandler outputDestinationHandler)
      Description copied from interface: Environment
      Set the output destination that handles where source files are written
      Specified by:
      setOutputDestinationHandler in interface Environment
      Parameters:
      outputDestinationHandler - handler for determining where to write source files
    • getOutputDestinationHandler

      public OutputDestinationHandler getOutputDestinationHandler()
      Description copied from interface: Environment
      Returns the output destination that handles where source files are written
      Specified by:
      getOutputDestinationHandler in interface Environment
      Returns:
      the current output destination handler
    • getModelChangeListener

      public FineModelChangeListener getModelChangeListener()
      Description copied from interface: Environment
      get the model change listener that is used to follow the change of the AST.
      Specified by:
      getModelChangeListener in interface Environment
      Returns:
      the current change listener
    • setModelChangeListener

      public void setModelChangeListener(FineModelChangeListener modelChangeListener)
      Description copied from interface: Environment
      Set the model change listener
      Specified by:
      setModelChangeListener in interface Environment
      Parameters:
      modelChangeListener - change listener to set
    • getEncoding

      public Charset getEncoding()
      Description copied from interface: Environment
      Get the encoding used inside the project
      Specified by:
      getEncoding in interface Environment
      Returns:
      encoding used in the project
    • getEncodingProvider

      public EncodingProvider getEncodingProvider()
      Description copied from interface: Environment
      Get encoding provider, which is used to detect encoding for each file separately
      Specified by:
      getEncodingProvider in interface Environment
      Returns:
      the current encoding provider
    • setEncoding

      public void setEncoding(Charset encoding)
      Description copied from interface: Environment
      Set the encoding to use for parsing source code
      Specified by:
      setEncoding in interface Environment
      Parameters:
      encoding - the character set to use for source file encoding
    • setEncodingProvider

      public void setEncodingProvider(EncodingProvider encodingProvider)
      Description copied from interface: Environment
      Set encoding provider, which is used to detect encoding for each file separately
      Specified by:
      setEncodingProvider in interface Environment
      Parameters:
      encodingProvider - the encoding provider to set
    • setOutputType

      public void setOutputType(OutputType outputType)
      Description copied from interface: Environment
      Set the output type used for processing files
      Specified by:
      setOutputType in interface Environment
      Parameters:
      outputType - output type to use when pretty-printing
    • getOutputType

      public OutputType getOutputType()
      Description copied from interface: Environment
      Get the output type
      Specified by:
      getOutputType in interface Environment
      Returns:
      the current output type
    • getSpoonProgress

      public SpoonProgress getSpoonProgress()
      Description copied from interface: Environment
      Get the spoonProgress logger. This method mustn't return null.
      Specified by:
      getSpoonProgress in interface Environment
      Returns:
      the spoonProgress
    • setSpoonProgress

      public void setSpoonProgress(SpoonProgress spoonProgress)
      Specified by:
      setSpoonProgress in interface Environment
    • getCompressionType

      public CompressionType getCompressionType()
      Description copied from interface: Environment
      Get the type of serialization to be used by default
      Specified by:
      getCompressionType in interface Environment
      Returns:
      the current type of serialization
    • setCompressionType

      public void setCompressionType(CompressionType serializationType)
      Description copied from interface: Environment
      Set the type of serialization to be used by default
      Specified by:
      setCompressionType in interface Environment
      Parameters:
      serializationType - the type of serialization to set
    • createPrettyPrinterAutoImport

      public PrettyPrinter createPrettyPrinterAutoImport()
      Description copied from interface: Environment
      Creates a pretty-printer that automatically imports used types and turns fully qualified type names into simply qualified names. This is roughly equivalent to setting Environment.setAutoImports(boolean) to true and then invokin Environment.createPrettyPrinter(), except that the environment is not modified.
      Specified by:
      createPrettyPrinterAutoImport in interface Environment
      Returns:
      new instance of PrettyPrinter which prints nice code
    • createPrettyPrinter

      public PrettyPrinter createPrettyPrinter()
      Description copied from interface: Environment
      Creates the default pretty-printer.
      Specified by:
      createPrettyPrinter in interface Environment
      Returns:
      new instance of PrettyPrinter which is configured for this environment
    • setPrettyPrinterCreator

      public void setPrettyPrinterCreator(Supplier<PrettyPrinter> creator)
      Description copied from interface: Environment
      Sets a custom pretty-printer that overrides the default pretty-printer. Can for example be used to create a SniperJavaPrettyPrinter for enabling the sniper mode. env.setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(env));
      Specified by:
      setPrettyPrinterCreator in interface Environment
      Parameters:
      creator - a Supplier, which creates new instance of pretty printer.
    • useLegacyTypeAdaption

      public boolean useLegacyTypeAdaption()
      Description copied from interface: Environment
      Whether to use the new or legacy and (soon to be) deprecated type adaption. You should not use this method.
      Specified by:
      useLegacyTypeAdaption in interface Environment
      Returns:
      true if spoon uses the old and (soon to be) deprecated type adaption.
    • setUseLegacyTypeAdaption

      public void setUseLegacyTypeAdaption(boolean useLegacyTypeAdaption)
      Description copied from interface: Environment
      Sets whether to use the new or legacy and (soon to be) deprecated type adaption. You should not use this method.
      Specified by:
      setUseLegacyTypeAdaption in interface Environment
      Parameters:
      useLegacyTypeAdaption - whether to use the old type adaption implementation
    • isIgnoreDuplicateDeclarations

      public boolean isIgnoreDuplicateDeclarations()
      Description copied from interface: Environment
      Whether Spoon currently ignores duplicate declarations of types.
      Specified by:
      isIgnoreDuplicateDeclarations in interface Environment
      Returns:
      true if spoon is allowed to create a model of a project that contains multiple copies of the same class
    • setIgnoreDuplicateDeclarations

      public void setIgnoreDuplicateDeclarations(boolean ignoreDuplicateDeclarations)
      Description copied from interface: Environment
      Set Spoon to ignore duplicate type declarations in a project. Setting this option to true causes Spoon to attempt to build a model even when the same qualified name appears for multiple types. This mode of operation makes Spoon less stable as duplicated types do not make sense in Java, and causes strange behavior in the underlying JDT compiler. Type resolution can become unpredictable as the order in which types are parsed becomes a determining factor in which duplicated type actually makes it into the model.
      Specified by:
      setIgnoreDuplicateDeclarations in interface Environment
      Parameters:
      ignoreDuplicateDeclarations - (default false) set to true to allow spoon to create a model of a project that contains multiple times the same class