1 package net.sf.cglib.transform;
2
3 import net.sf.cglib.core.CodeGenerationException;
4 import net.sf.cglib.core.ClassGenerator;
5 import net.sf.cglib.core.DebuggingClassWriter;
6 import org.objectweb.asm.ClassReader;
7 import org.objectweb.asm.ClassWriter;
8 import org.objectweb.asm.util.*;
9
10 import java.io.IOException;
11
12 abstract public class AbstractClassLoader extends ClassLoader {
13 private ClassFilter filter;
14 private ClassLoader classPath;
15 private static java.security.ProtectionDomain DOMAIN ;
16
17 static{
18
19 DOMAIN = (java.security.ProtectionDomain)
20 java.security.AccessController.doPrivileged(
21 new java.security.PrivilegedAction() {
22 public Object run() {
23 return AbstractClassLoader.class.getProtectionDomain();
24 }
25 });
26 }
27
28 protected AbstractClassLoader(ClassLoader parent, ClassLoader classPath, ClassFilter filter) {
29 super(parent);
30 this.filter = filter;
31 this.classPath = classPath;
32 }
33
34 public Class loadClass(String name) throws ClassNotFoundException {
35
36 Class loaded = findLoadedClass(name);
37
38 if( loaded != null ){
39 if( loaded.getClassLoader() == this ){
40 return loaded;
41 }//else reload with this class loader
42 }
43
44 if (!filter.accept(name)) {
45 return super.loadClass(name);
46 }
47 ClassReader r;
48 try {
49
50 java.io.InputStream is = classPath.getResourceAsStream(
51 name.replace('.','/') + ".class"
52 );
53
54 if (is == null) {
55
56 throw new ClassNotFoundException(name);
57
58 }
59 try {
60
61 r = new ClassReader(is);
62
63 } finally {
64
65 is.close();
66
67 }
68 } catch (IOException e) {
69 throw new ClassNotFoundException(name + ":" + e.getMessage());
70 }
71
72 try {
73 ClassWriter w = new DebuggingClassWriter(true);
74 getGenerator(r).generateClass( w );
75 byte[] b = w.toByteArray();
76 return super.defineClass(name, b, 0, b.length, DOMAIN);
77 } catch (RuntimeException e) {
78 throw e;
79 } catch (Error e) {
80 throw e;
81 } catch (Exception e) {
82 throw new CodeGenerationException(e);
83 }
84 }
85
86 protected ClassGenerator getGenerator(ClassReader r) {
87 return new ClassReaderGenerator(r, true); // skipDebug?
88 }
89 }
This page was automatically generated by Maven