jodd.util
Class JavaSourceClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by jodd.util.JavaSourceClassLoader

public class JavaSourceClassLoader
extends java.lang.ClassLoader

A ClassLoader that, unlike usual class loaders, does not load byte code, but reads JavaTM source code and then scans, parses, compiles and loads it into the virtual machine.

It requires a root source folder. When a class is required, JavaSourceClassLoader will first check if source is available. If source exists, it will be compiled with current classpath. If source doesn't exist, or if it is one of the system classes, class will be loaded with the parent classloader.

JavaSourceClassLoader may work in two ways:

  • Aggressively: when not only the requested class is compiled and loaded, but also all the depending class.Althought this is a common behaviour of a class loader, sometimes it might not be useful for this one (e.g. when replacing existing classes).
  • Non-aggressively: when only the requested class is loaded by this class loader and all others are loaded by parent - but then some non-common methods should be used.

    See Also:
    RuntimeJavaSourceLoader

    Field Summary
    protected  boolean aggressive
               
    protected  java.util.Map<java.lang.String,java.lang.Class> classBuffer
               
    protected  boolean debugInfo
               
    protected  java.lang.String destinationFolder
               
    protected  java.lang.String encoding
               
    protected  java.lang.String sourceFolder
               
    static java.lang.String[] SYSTEM_CLASSES
              Prefixes for system classes that should be always loaded by parent constructor.
     
    Constructor Summary
    JavaSourceClassLoader(java.lang.ClassLoader parent, java.lang.String sourceFolder, boolean aggressive)
               
    JavaSourceClassLoader(java.lang.String sourceFolder, boolean aggressive)
               
     
    Method Summary
     java.lang.Class compileAndLoadClass(java.lang.String className)
               
     java.lang.Class compileAndLoadClass(java.lang.String className, boolean resolveIt)
              Compiles and then loads the class with the specified name.
     int compileClass(java.lang.String className)
              Compiles java source file.
     java.lang.String getDestinationFolder()
               
     java.lang.String getEncoding()
               
     boolean isClassAlreadyLoaded(java.lang.String className)
              Returns true if this classloader has already loaded a class with provided name.
     boolean isDebugInfo()
               
     java.lang.Class<?> loadClass(java.lang.String className)
               
    protected  java.lang.Class<?> loadClass(java.lang.String className, boolean resolve)
              Loads the class with the specified name.
     java.lang.Class readAndDefineClass(java.lang.String className)
              Reads compiled class bytes and defines a class.
     void setDebugInfo(boolean debugInfo)
               
     void setDestinationFolder(java.lang.String destinationFolder)
               
     void setEncoding(java.lang.String encoding)
               
     
    Methods inherited from class java.lang.ClassLoader
    clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    SYSTEM_CLASSES

    public static java.lang.String[] SYSTEM_CLASSES
    Prefixes for system classes that should be always loaded by parent constructor.


    sourceFolder

    protected final java.lang.String sourceFolder

    destinationFolder

    protected java.lang.String destinationFolder

    debugInfo

    protected boolean debugInfo

    encoding

    protected java.lang.String encoding

    aggressive

    protected final boolean aggressive

    classBuffer

    protected java.util.Map<java.lang.String,java.lang.Class> classBuffer
    Constructor Detail

    JavaSourceClassLoader

    public JavaSourceClassLoader(java.lang.String sourceFolder,
                                 boolean aggressive)

    JavaSourceClassLoader

    public JavaSourceClassLoader(java.lang.ClassLoader parent,
                                 java.lang.String sourceFolder,
                                 boolean aggressive)
    Method Detail

    getDestinationFolder

    public java.lang.String getDestinationFolder()

    setDestinationFolder

    public void setDestinationFolder(java.lang.String destinationFolder)

    isDebugInfo

    public boolean isDebugInfo()

    setDebugInfo

    public void setDebugInfo(boolean debugInfo)

    getEncoding

    public java.lang.String getEncoding()

    setEncoding

    public void setEncoding(java.lang.String encoding)

    isClassAlreadyLoaded

    public boolean isClassAlreadyLoaded(java.lang.String className)
    Returns true if this classloader has already loaded a class with provided name.


    loadClass

    public java.lang.Class<?> loadClass(java.lang.String className)
                                 throws java.lang.ClassNotFoundException
    Overrides:
    loadClass in class java.lang.ClassLoader
    Throws:
    java.lang.ClassNotFoundException

    loadClass

    protected java.lang.Class<?> loadClass(java.lang.String className,
                                           boolean resolve)
                                    throws java.lang.ClassNotFoundException
    Loads the class with the specified name.

    Overrides:
    loadClass in class java.lang.ClassLoader
    Throws:
    java.lang.ClassNotFoundException
    See Also:
    compileAndLoadClass(String, boolean)

    compileAndLoadClass

    public java.lang.Class compileAndLoadClass(java.lang.String className)
                                        throws java.lang.ClassNotFoundException
    Throws:
    java.lang.ClassNotFoundException

    compileAndLoadClass

    public java.lang.Class compileAndLoadClass(java.lang.String className,
                                               boolean resolveIt)
                                        throws java.lang.ClassNotFoundException
    Compiles and then loads the class with the specified name. Should be used in non-agressivly mode.

    Throws:
    java.lang.ClassNotFoundException
    See Also:
    loadClass(String, boolean)

    compileClass

    public int compileClass(java.lang.String className)
                     throws java.io.FileNotFoundException
    Compiles java source file. Returns compilation error code, 0 is for no errors. .

    Throws:
    java.io.FileNotFoundException - if class source file doesn't exist (indicates that class might be loaded with next classloader in chain)

    readAndDefineClass

    public java.lang.Class readAndDefineClass(java.lang.String className)
                                       throws java.lang.ClassNotFoundException
    Reads compiled class bytes and defines a class.

    Throws:
    java.lang.ClassNotFoundException


    Copyright ©2008 Jodd Team