KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > AbstractClassLoader


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.transform;
17
18 import net.sf.cglib.core.CodeGenerationException;
19 import net.sf.cglib.core.ClassGenerator;
20 import net.sf.cglib.core.DebuggingClassWriter;
21 import org.objectweb.asm.ClassReader;
22 import org.objectweb.asm.ClassWriter;
23 import org.objectweb.asm.util.*;
24 import org.objectweb.asm.Attribute;
25
26 import java.io.IOException JavaDoc;
27
28 abstract public class AbstractClassLoader extends ClassLoader JavaDoc {
29     private ClassFilter filter;
30     private ClassLoader JavaDoc classPath;
31     private static java.security.ProtectionDomain JavaDoc DOMAIN ;
32     
33     static{
34         
35         DOMAIN = (java.security.ProtectionDomain JavaDoc)
36         java.security.AccessController.doPrivileged(
37           new java.security.PrivilegedAction JavaDoc() {
38             public Object JavaDoc run() {
39                return AbstractClassLoader.class.getProtectionDomain();
40             }
41         });
42      }
43     
44     protected AbstractClassLoader(ClassLoader JavaDoc parent, ClassLoader JavaDoc classPath, ClassFilter filter) {
45         super(parent);
46         this.filter = filter;
47         this.classPath = classPath;
48     }
49
50     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
51         
52         Class JavaDoc loaded = findLoadedClass(name);
53         
54         if( loaded != null ){
55             if( loaded.getClassLoader() == this ){
56                return loaded;
57             }//else reload with this class loader
58
}
59         
60         if (!filter.accept(name)) {
61             return super.loadClass(name);
62         }
63         ClassReader r;
64         try {
65             
66            java.io.InputStream JavaDoc is = classPath.getResourceAsStream(
67                        name.replace('.','/') + ".class"
68                   );
69            
70            if (is == null) {
71                
72               throw new ClassNotFoundException JavaDoc(name);
73               
74            }
75            try {
76                
77               r = new ClassReader(is);
78             
79            } finally {
80                
81               is.close();
82              
83            }
84         } catch (IOException JavaDoc e) {
85             throw new ClassNotFoundException JavaDoc(name + ":" + e.getMessage());
86         }
87
88         try {
89             ClassWriter w = new DebuggingClassWriter(true);
90             getGenerator(r).generateClass( w );
91             byte[] b = w.toByteArray();
92             Class JavaDoc c = super.defineClass(name, b, 0, b.length, DOMAIN);
93             postProcess(c);
94             return c;
95         } catch (RuntimeException JavaDoc e) {
96             throw e;
97         } catch (Error JavaDoc e) {
98             throw e;
99         } catch (Exception JavaDoc e) {
100             throw new CodeGenerationException(e);
101         }
102     }
103
104     protected ClassGenerator getGenerator(ClassReader r) {
105         return new ClassReaderGenerator(r, attributes(), skipDebug());
106     }
107
108     protected boolean skipDebug() {
109         return false;
110     }
111     
112     protected Attribute[] attributes() {
113         return null;
114     }
115
116     protected void postProcess(Class JavaDoc c) {
117     }
118 }
119
Popular Tags