001: /*
002: * AdaptorClassLoader.java --
003: *
004: * ClassLoader for loading event adaptor classes.
005: *
006: * Copyright (c) 1997 Sun Microsystems, Inc.
007: *
008: * See the file "license.terms" for information on usage and
009: * redistribution of this file, and for a DISCLAIMER OF ALL
010: * WARRANTIES.
011: *
012: * RCS: @(#) $Id: AdaptorClassLoader.java,v 1.3 2006/02/08 23:53:47 mdejong Exp $
013: */
014:
015: package tcl.lang;
016:
017: import java.util.*;
018: import java.lang.reflect.*;
019: import java.beans.*;
020:
021: // This class is a ClassLoader which loads event adaptor classes
022: // generated by AdaptorGen.
023:
024: class AdaptorClassLoader extends ClassLoader {
025:
026: // Number of classes generated so far.
027:
028: private static int genCount = 0;
029:
030: // The event adaptor generator.
031:
032: private AdaptorGen adaptorGen = new AdaptorGen();
033:
034: /*
035: *----------------------------------------------------------------------
036: *
037: * getNewClassName --
038: *
039: * Generate a unique class name for a new adaptor class. The
040: * first adaptor class is tcl.lang.Adaptor0, the second is
041: * tcl.lang.Adaptor1, and so on.
042: *
043: * Results:
044: * A unique name for an adaptor class.
045: *
046: * Side effects:
047: * None.
048: *
049: *----------------------------------------------------------------------
050: */
051:
052: private synchronized String getNewClassName() {
053: return "tcl.lang.Adaptor" + (genCount++);
054: }
055:
056: /*
057: *----------------------------------------------------------------------
058: *
059: * loadEventAdaptor --
060: *
061: * Generates and loads an event adaptor class to handle the given
062: * event set.
063: *
064: * Results:
065: * The Class object of the newly generated adaptor class.
066: *
067: * Side effects:
068: * The adaptor class is generated and loaded into the JVM.
069: *
070: *----------------------------------------------------------------------
071: */
072:
073: synchronized Class loadEventAdaptor(Interp interp, // Current interpreter.
074: EventSetDescriptor desc) // The descriptor of the event interface
075: // to generate an adaptor for.
076: {
077: String classToGen = getNewClassName();
078: byte classData[] = adaptorGen.generate(desc,
079: EventAdaptor.class, classToGen);
080: Class cls = defineClass(classToGen, classData, 0,
081: classData.length);
082: resolveClass(cls);
083:
084: return cls;
085: }
086:
087: /*
088: *----------------------------------------------------------------------
089: *
090: * loadClass --
091: *
092: * Loads the given class in the JVM. This implementation uses
093: * the class loader that loaded this class.
094: *
095: * Results:
096: * The Class object of the requested class.
097: *
098: * Side effects:
099: * The class is loaded into the JVM.
100: *
101: *----------------------------------------------------------------------
102: */
103:
104: protected synchronized Class loadClass(String className, // The name of the class to load.
105: boolean resolve) // True if we must resolve the class now.
106: throws ClassNotFoundException // If the class cannot be found.
107: {
108: Class cls = Class.forName(className);
109:
110: if (resolve) {
111: resolveClass(cls);
112: }
113: return cls;
114: }
115:
116: } // end AdaptorClassLoader
|