示范类加载器 : 类加载器 « 语言基础知识 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 语言基础知识 » 类加载器屏幕截图 
示范类加载器
示范类加载器

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;

/**
 * Demonstration of a ClassLoader
 */
public class ClassLoaderDemo1 extends ClassLoader {
  /** The Hashtable to keep track of classes, to avoid re-loading them */
  protected Hashtable cache = new Hashtable();

  /** data's expected length */
  private final int dataLength = 433;

  /** data, obtained by dumping a compiled .class file */
  private int[] data = 2022541861900304503180207,
      0177025702670271004099050,
      101003011120140121202802212029,
      01310340418610214076106971189747,
      1089711010347831161141051101035941861,
      0660105110105116621046711110010110,
      13671111101151169711011686971081171011,
      0468101109111109681011091114610697,
      1189710106912099101112116105111110115,
      101072101108108111327497118971015,
      761051101017811710998101114849798108,
      1011021761069711897471051114780114,
      105110116831161141019710959101476111,
      999710886971141059798108101115101083,
      1111171149910170105108101101910697118,
      9747105111478011410511011683116114101,
      971091016106971189747108971101034779,
      9810610199116101610697118974710897,
      1101034783121115116101109103111117116,
      107112114105110116108110104116101115,
      11603302040000020903001201,
      015000370200000917807181182,
      0817700010210001002000908,
      0801014012010150002901010,
      00542183061770001021000601,
      0007010240002018 };

  /** "load", that is, make up, the data for the class */
  private byte[] genClassData(String name) {
    if (dataLength != data.length)
      throw new IllegalArgumentException("data corrupt");
    byte[] bd = new byte[data.length];
    for (int i = 0; i < bd.length; i++)
      bd[i(bytedata[i];
    return bd;
  }

  public synchronized Class loadClass(String name, boolean resolve)
      throws ClassNotFoundException {
    /**
     * We can expect to be called to resolve at least demo's superclass
     * (java.lang.Object). Fortunatetely, we can just use
     * super.findSystemClass() to load such things...
     */
    if (name.startsWith("java.")) {
      System.out.println("loadClass: SystemLoading " + name);
      return findSystemClass(name);
    }
    Class c = (Classcache.get(name);
    if (c == null) {
      System.out.println("loadClass: About to genClassData " + name);
      byte mydata[] = genClassData(name);
      System.out.println("loadClass: About to defineClass " + name);
      c = defineClass(name, mydata, 0, mydata.length);
      System.out.println("loadClass: storing " + name + " in cache.");
      cache.put(name, c);
    else
      System.out.println("loadClass: found " + name + " in cache.");
    if (resolve) {
      System.out.println("loadClass: About to resolveClass " + name);
      resolveClass(c);
    }
    return c;
  }

  public static void main(String[] argv) {
    System.out.println("ClassLoaderDemo1 starting");
    ClassLoaderDemo1 loader = new ClassLoaderDemo1();
    Class c = null;
    Object demo;
    try {
      /* Load the "Demo" class from memory */

      System.out.println("About to load class  Demo");
      c = loader.loadClass("Demo"true);
      System.out.println("About to instantiate class Demo");
      demo = c.newInstance();
      System.out.println("Got Demo class loaded: " + demo);

      /* Now try to call a method */

      Method mi = c.getMethod("test"null);
      mi.invoke(demo, null);

    catch (InvocationTargetException e) {
      // The invoked method threw an exception. We get it
      // wrapped up inside another exception, hence the
      // extra call here:
      e.getTargetException().printStackTrace();
      System.out.println("Could not run test method");
    catch (Exception e) {
      e.printStackTrace();
      System.out.println("Could not run test method");
    }

    /**
     * Try to load some arbitrary class, to see if our ClassLoader gets
     * called.
     */
    System.out.println("Trying to load an unrelated class");
    java.awt.image.DirectColorModel jnk = new java.awt.image.DirectColorModel(
        24888);
    System.out
        .println("Load an unrelated class - was your ClassLoader called?");

    /** Try to instantiate a second ClassLoader */
    System.out.println("Trying to install another ClassLoader");
    ClassLoaderDemo1 loader2 = new ClassLoaderDemo1();
    System.out.println("Instantiated another ClassLoader...");
  }
}

           
       
Related examples in the same category
1. 在任何网址运行jar应用程序
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.