了解 java classloader.txt

来自「含有许多JAVA的技巧!」· 文本 代码 · 共 373 行 · 第 1/2 页

TXT
373
字号
    fin.close();
    // And finally return the file contents as an array
    return raw;
  }
  // Spawn a process to compile the java source code file
  // specified in the 'javaFile' parameter.  Return a true if
  // the compilation worked, false otherwise.
  private boolean compile( String javaFile ) throws IOException {
    // Let the user know what's going on
    System.out.println( "CCL: Compiling "+javaFile+"..." );
    // Start up the compiler
    Process p = Runtime.getRuntime().exec( "javac "+javaFile );
    // Wait for it to finish running
    try {
      p.waitFor();
    } catch( InterruptedException ie ) { System.out.println( ie ); }
    // Check the return code, in case of a compilation error
    int ret = p.exitValue();
    // Tell whether the compilation worked
    return ret==0;
  }
  // The heart of the ClassLoader -- automatically compile
  // source as necessary when looking for class files
  public Class loadClass( String name, boolean resolve )
      throws ClassNotFoundException {
    // Our goal is to get a Class object
    Class clas = null;
    // First, see if we've already dealt with this one
    clas = findLoadedClass( name );
    //System.out.println( "findLoadedClass: "+clas );
    // Create a pathname from the class name
    // E.g. java.lang.Object => java/lang/Object
    String fileStub = name.replace( '.', '/' );
    // Build objects pointing to the source code (.java) and object
    // code (.class)
    String javaFilename = fileStub+".java";
    String classFilename = fileStub+".class";
    File javaFile = new File( javaFilename );
    File classFile = new File( classFilename );
    //System.out.println( "j "+javaFile.lastModified()+" c "+
    //  classFile.lastModified() );
    // First, see if we want to try compiling.  We do if (a) there
    // is source code, and either (b0) there is no object code,
    // or (b1) there is object code, but it's older than the source
    if (javaFile.exists() &&
         (!classFile.exists() ||
          javaFile.lastModified() > classFile.lastModified())) {
      try {
        // Try to compile it.  If this doesn't work, then
        // we must declare failure.  (It's not good enough to use
        // and already-existing, but out-of-date, classfile)
        if (!compile( javaFilename ) || !classFile.exists()) {
          throw new ClassNotFoundException( "Compile failed: "+javaFilename );
        }
      } catch( IOException ie ) {
        // Another place where we might come to if we fail
        // to compile
        throw new ClassNotFoundException( ie.toString() );
      }
    }
    // Let's try to load up the raw bytes, assuming they were
    // properly compiled, or didn't need to be compiled
    try {
      // read the bytes
      byte raw[] = getBytes( classFilename );
      // try to turn them into a class
      clas = defineClass( name, raw, 0, raw.length );
    } catch( IOException ie ) {
      // This is not a failure!  If we reach here, it might
      // mean that we are dealing with a class in a library,
      // such as java.lang.Object
    }
    //System.out.println( "defineClass: "+clas );
    // Maybe the class is in a library -- try loading
    // the normal way
    if (clas==null) {
      clas = findSystemClass( name );
    }
    //System.out.println( "findSystemClass: "+clas );
    // Resolve the class, if any, but only if the "resolve"
    // flag is set to true
    if (resolve && clas != null)
      resolveClass( clas );
    // If we still don't have a class, it's an error
    if (clas == null)
      throw new ClassNotFoundException( name );
    // Otherwise, return the class
    return clas;
  }
}

CCRun.java 
以下是 CCRun.java 的源代码 

// $Id$
import java.lang.reflect.*;
/*
CCLRun executes a Java program by loading it through a
CompilingClassLoader.
*/
public class CCLRun
{
  static public void main( String args[] ) throws Exception {
    // The first argument is the Java program (class) the user
    // wants to run
    String progClass = args[0];
    // And the arguments to that program are just
    // arguments 1..n, so separate those out into
    // their own array
    String progArgs[] = new String[args.length-1];
    System.arraycopy( args, 1, progArgs, 0, progArgs.length );
    // Create a CompilingClassLoader
    CompilingClassLoader ccl = new CompilingClassLoader();
    // Load the main class through our CCL
    Class clas = ccl.loadClass( progClass );
    // Use reflection to call its main() method, and to
    // pass the arguments in.
    // Get a class representing the type of the main method's argument
    Class mainArgType[] = { (new String[0]).getClass() };
    // Find the standard main method in the class
    Method main = clas.getMethod( "main", mainArgType );
    // Create a list containing the arguments -- in this case,
    // an array of strings
    Object argsArray[] = { progArgs };
    // Call the method
    main.invoke( null, argsArray );
  }
}

Foo.java 
以下是 Foo.java 的源代码

// $Id$
public class Foo
{
  static public void main( String args[] ) throws Exception {
    System.out.println( "foo! "+args[0]+" "+args[1] );
    new Bar( args[0], args[1] );
  }
}

Bar.java 
以下是 Bar.java 的源代码 

// $Id$
import baz.*;
public class Bar
{
  public Bar( String a, String b ) {
    System.out.println( "bar! "+a+" "+b );
    new Baz( a, b );
    try {
      Class booClass = Class.forName( "Boo" );
      Object boo = booClass.newInstance();
    } catch( Exception e ) {
      e.printStackTrace();
    }
  }
}

baz/Baz.java 
以下是 baz/Baz.java 的源代码 

// $Id$
package baz;
public class Baz
{
  public Baz( String a, String b ) {
    System.out.println( "baz! "+a+" "+b );
  }
}

Boo.java 
以下是 Boo.java 的源代码 

// $Id$
public class Boo
{
  public Boo() {
    System.out.println( "Boo!" );
  }
}

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( ProgramFan.Com )
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?