Class SimpleCompiler

java.lang.Object
org.codehaus.commons.compiler.Cookable
org.codehaus.commons.compiler.jdk.SimpleCompiler
All Implemented Interfaces:
ICookable, ISimpleCompiler

public class SimpleCompiler extends Cookable implements ISimpleCompiler
javax.tools-based implementation of ISimpleCompiler.
  • Field Details

  • Constructor Details

    • SimpleCompiler

      public SimpleCompiler()
    • SimpleCompiler

      public SimpleCompiler(JavaCompiler javaCompiler)
      Initializes with a different, javax.tools.JavaCompiler-compatible Java compiler.
  • Method Details

    • setSourceVersion

      public void setSourceVersion(int version)
      Description copied from interface: ICookable
      Specifies the version of source code accepted, in analogy with JAVAC's -source command line option. May be ignored by an implementation (e.g. the janino implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation. -1 means to use a default version.
      Specified by:
      setSourceVersion in interface ICookable
    • setTargetVersion

      public void setTargetVersion(int version)
      Description copied from interface: ICookable
      Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM. -1 means to use a default version.
      Specified by:
      setTargetVersion in interface ICookable
    • getBytecodes

      public Map<String,byte[]> getBytecodes()
      Specified by:
      getBytecodes in interface ICookable
      Returns:
      The generated Java bytecode; maps class name to bytes
    • assertCooked

      private Map<String,byte[]> assertCooked()
    • getClassLoader

      public ClassLoader getClassLoader()
      Description copied from interface: ISimpleCompiler
      Returns a ClassLoader object through which the previously compiled classes can be accessed. This ClassLoader can be used for subsequent ISimpleCompilers in order to compile compilation units that use types (e.g. declare derived types) declared in the previous one.

      This method must only be called after exactly one of the ICookable.cook(String, java.io.Reader) methods was called.

      Specified by:
      getClassLoader in interface ISimpleCompiler
    • cook

      public void cook(@Nullable String fileName, Reader r) throws CompileException, IOException
      Description copied from interface: ICookable
      Reads, scans, parses and compiles Java tokens from the given Reader.
      Specified by:
      cook in interface ICookable
      Specified by:
      cook in class Cookable
      Parameters:
      fileName - Used when reporting errors and warnings
      Throws:
      CompileException
      IOException
    • setDebuggingInformation

      public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
      Description copied from interface: ISimpleCompiler
      Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none".
      Specified by:
      setDebuggingInformation in interface ISimpleCompiler
    • setParentClassLoader

      public void setParentClassLoader(@Nullable ClassLoader parentClassLoader)
      Description copied from interface: ISimpleCompiler
      The "parent class loader" is used to load referenced classes. Useful values are:
      System.getSystemClassLoader() The running JVM's class path
      Thread.currentThread().getContextClassLoader() or null The class loader effective for the invoking thread
      ClassLoaders.BOOTCLASSPATH_CLASS_LOADER The running JVM's boot class path

      The parent class loader defaults to the current thread's context class loader.

      Specified by:
      setParentClassLoader in interface ISimpleCompiler
    • setParentClassLoader

      @Deprecated public void setParentClassLoader(@Nullable ClassLoader parentClassLoader, Class<?>[] auxiliaryClasses)
      Deprecated.
      Auxiliary classes never really worked... don't use them.
    • setCompileErrorHandler

      public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
      Description copied from interface: ISimpleCompiler
      Installs an ErrorHandler which is invoked during compilation on each error. (By default, the compilation throws a CompileException on the first error and terminates.)

      If the given ErrorHandler throws a CompileException, then the compilation terminates and the exception is propagated.

      If the given ErrorHandler does not throw a CompileException but completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation completes normally but errors were reported, then it will throw a CompileException indicating the number of errors.

      In other words: The ErrorHandler may throw a CompileException or not, but the compilation will definitely throw a CompileException if one or more compile errors have occurred.

      Specified by:
      setCompileErrorHandler in interface ISimpleCompiler
      Parameters:
      compileErrorHandler - null to restore the default behavior (throwing a CompileException)
    • setWarningHandler

      public void setWarningHandler(@Nullable WarningHandler warningHandler)
      Description copied from interface: ISimpleCompiler
      By default, warnings are discarded, but an application my install a custom WarningHandler which is invoked for each warning. If, for some untypical reason, that warning handler wants to terminate the compilation as quickly as possible, then it would throw a CompileException.
      Specified by:
      setWarningHandler in interface ISimpleCompiler
      Parameters:
      warningHandler - null to indicate that no warnings be issued
    • addOffset

      protected void addOffset(@Nullable String fileName)
      Derived classes call this method to "reset" the current line and column number at the currently read input character, and also changes the "file name" (see cook(String, Reader)).