Class JavaSourceClassLoader


  • public class JavaSourceClassLoader
    extends AbstractJavaSourceClassLoader
    A ClassLoader that loads classes by looking for their source files through a "source path" and compiling them on-the-fly.

    Notice that this class loader does not support resoures in the sense of ClassLoader.getResource(String), ClassLoader.getResourceAsStream(String) nd ClassLoader.getResources(String).

    See Also:
    ClassLoaders
    • Field Detail

      • SYSTEM_JAVA_COMPILER

        private static final javax.tools.JavaCompiler SYSTEM_JAVA_COMPILER
      • sourceCharset

        private java.nio.charset.Charset sourceCharset
      • debuggingInfoLines

        private boolean debuggingInfoLines
      • debuggingInfoVars

        private boolean debuggingInfoVars
      • debuggingInfoSource

        private boolean debuggingInfoSource
      • compilerOptions

        private java.util.Collection<java.lang.String> compilerOptions
      • fileManager

        @Nullable
        private javax.tools.JavaFileManager fileManager
    • Method Detail

      • getSystemJavaCompiler

        private static javax.tools.JavaCompiler getSystemJavaCompiler()
      • init

        private void init()
      • getJavaFileManager

        private javax.tools.JavaFileManager getJavaFileManager()
        Creates the underlying JavaFileManager lazily, because setSourcePath(File[]) and consorts are called after initialization.
      • setSourcePath

        public void setSourcePath​(java.io.File[] sourcePath)
        Specified by:
        setSourcePath in class AbstractJavaSourceClassLoader
        Parameters:
        sourcePath - The sequence of directories to search for Java source files
      • setSourceCharset

        public void setSourceCharset​(java.nio.charset.Charset charset)
        Specified by:
        setSourceCharset in class AbstractJavaSourceClassLoader
        Parameters:
        charset - The character set to using when reading characters from a source file
      • setDebuggingInfo

        public void setDebuggingInfo​(boolean lines,
                                     boolean vars,
                                     boolean source)
        Specified by:
        setDebuggingInfo in class AbstractJavaSourceClassLoader
        Parameters:
        lines - Whether line number debugging information should be generated
        vars - Whether variables debugging information should be generated
        source - Whether source file debugging information should be generated
      • setCompilerOptions

        public void setCompilerOptions​(java.lang.String[] compilerOptions)
        Notice: Don't use the '-g' options - these are controlled through setDebuggingInfo(boolean, boolean, boolean).
        Parameters:
        compilerOptions - All command line options supported by the JDK JAVAC tool
      • findClass

        protected java.lang.Class<?> findClass​(java.lang.String className)
                                        throws java.lang.ClassNotFoundException
        Implementation of ClassLoader.findClass(String).
        Overrides:
        findClass in class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • findClass2

        private java.lang.Class<?> findClass2​(java.lang.String className)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • findClassFile

        private javax.tools.JavaFileObject findClassFile​(java.lang.String className)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getSourceResourceName

        private static java.lang.String getSourceResourceName​(java.lang.String className)
        Constructs the name of a resource that could contain the source code of the class with the given name.

        Notice that member types are declared inside a different type, so the relevant source file is that of the outermost declaring class.

        Parameters:
        className - Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
        Returns:
        the name of the resource, e.g. "pkg1/pkg2/Outer.java"