Demonstration of a ClassLoader : Class Loader « Language Basics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Language Basics » Class LoaderScreenshots 
Demonstration of a ClassLoader
Demonstration of a ClassLoader

/*
 * 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. Runs a jar application from any url
w__ww__.___ja___v_a_2_s_.___c_o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.