001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.transform;
017:
018: import net.sf.cglib.core.CodeGenerationException;
019: import net.sf.cglib.core.ClassGenerator;
020: import net.sf.cglib.core.DebuggingClassWriter;
021: import org.objectweb.asm.ClassReader;
022: import org.objectweb.asm.ClassWriter;
023: import org.objectweb.asm.util.*;
024: import org.objectweb.asm.Attribute;
025:
026: import java.io.IOException;
027:
028: abstract public class AbstractClassLoader extends ClassLoader {
029: private ClassFilter filter;
030: private ClassLoader classPath;
031: private static java.security.ProtectionDomain DOMAIN;
032:
033: static {
034:
035: DOMAIN = (java.security.ProtectionDomain) java.security.AccessController
036: .doPrivileged(new java.security.PrivilegedAction() {
037: public Object run() {
038: return AbstractClassLoader.class
039: .getProtectionDomain();
040: }
041: });
042: }
043:
044: protected AbstractClassLoader(ClassLoader parent,
045: ClassLoader classPath, ClassFilter filter) {
046: super (parent);
047: this .filter = filter;
048: this .classPath = classPath;
049: }
050:
051: public Class loadClass(String name) throws ClassNotFoundException {
052:
053: Class loaded = findLoadedClass(name);
054:
055: if (loaded != null) {
056: if (loaded.getClassLoader() == this ) {
057: return loaded;
058: }//else reload with this class loader
059: }
060:
061: if (!filter.accept(name)) {
062: return super .loadClass(name);
063: }
064: ClassReader r;
065: try {
066:
067: java.io.InputStream is = classPath.getResourceAsStream(name
068: .replace('.', '/')
069: + ".class");
070:
071: if (is == null) {
072:
073: throw new ClassNotFoundException(name);
074:
075: }
076: try {
077:
078: r = new ClassReader(is);
079:
080: } finally {
081:
082: is.close();
083:
084: }
085: } catch (IOException e) {
086: throw new ClassNotFoundException(name + ":"
087: + e.getMessage());
088: }
089:
090: try {
091: ClassWriter w = new DebuggingClassWriter(true);
092: getGenerator(r).generateClass(w);
093: byte[] b = w.toByteArray();
094: Class c = super .defineClass(name, b, 0, b.length, DOMAIN);
095: postProcess(c);
096: return c;
097: } catch (RuntimeException e) {
098: throw e;
099: } catch (Error e) {
100: throw e;
101: } catch (Exception e) {
102: throw new CodeGenerationException(e);
103: }
104: }
105:
106: protected ClassGenerator getGenerator(ClassReader r) {
107: return new ClassReaderGenerator(r, attributes(), skipDebug());
108: }
109:
110: protected boolean skipDebug() {
111: return false;
112: }
113:
114: protected Attribute[] attributes() {
115: return null;
116: }
117:
118: protected void postProcess(Class c) {
119: }
120: }
|