001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino;
035:
036: /**
037: * An {@link IClassLoader} that loads {@link IClass}es through a reflection
038: * {@link ClassLoader}.
039: */
040: public class ClassLoaderIClassLoader extends IClassLoader {
041: private static final boolean DEBUG = false;
042:
043: /**
044: * @param classLoader The delegate that loads the classes.
045: */
046: public ClassLoaderIClassLoader(ClassLoader classLoader) {
047: super (null // optionalParentIClassLoader
048: );
049:
050: if (classLoader == null)
051: throw new NullPointerException();
052:
053: this .classLoader = classLoader;
054: super .postConstruct();
055: }
056:
057: /**
058: * Equivalent to
059: * <pre>
060: * ClassLoaderIClassLoader(Thread.currentThread().getContextClassLoader())
061: * </pre>
062: */
063: public ClassLoaderIClassLoader() {
064: this (Thread.currentThread().getContextClassLoader());
065: }
066:
067: public ClassLoader getClassLoader() {
068: return this .classLoader;
069: }
070:
071: /**
072: * Find a new {@link IClass} by descriptor.
073: */
074: protected IClass findIClass(String descriptor)
075: throws ClassNotFoundException {
076:
077: //
078: // See also [ 931385 ] Janino 2.0 throwing exception on arrays of java.io.File:
079: //
080: // "ClassLoader.loadClass()" and "Class.forName()" should be identical,
081: // but "ClassLoader.loadClass("[Ljava.lang.Object;")" throws a
082: // ClassNotFoundException under JDK 1.5.0 beta.
083: // Unclear whether this a beta version bug and SUN will fix this in the final
084: // release, but "Class.forName()" seems to work fine in all cases, so we
085: // use that.
086: //
087:
088: Class clazz;
089: try {
090: // clazz = this.classLoader.loadClass(className);
091: clazz = Class.forName(Descriptor.toClassName(descriptor),
092: false, this .classLoader);
093: } catch (ClassNotFoundException e) {
094: if (e.getException() == null) {
095: return null;
096: } else {
097: throw e;
098: }
099: }
100: if (ClassLoaderIClassLoader.DEBUG)
101: System.out.println("clazz = " + clazz);
102:
103: IClass result = new ReflectionIClass(clazz, this );
104: this .defineIClass(result);
105: return result;
106: }
107:
108: private/*final*/ClassLoader classLoader;
109: }
|