KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > typeresolution > PMDASMClassLoader


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd.typeresolution;
5
6 import net.sourceforge.pmd.typeresolution.visitors.PMDASMVisitor;
7
8 import org.objectweb.asm.ClassReader;
9 import org.objectweb.asm.ClassWriter;
10
11 import java.io.IOException JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 public class PMDASMClassLoader extends ClassLoader JavaDoc {
19
20     public PMDASMClassLoader() {
21     }
22
23     public synchronized Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
24         return defineClass(name);
25     }
26
27     private Map JavaDoc importedClasses = new HashMap JavaDoc();
28
29     private Set JavaDoc dontBother = new HashSet JavaDoc();
30
31     public Map JavaDoc getImportedClasses(String JavaDoc className) {
32         Map JavaDoc ret = (Map JavaDoc) importedClasses.get(className);
33         return ret == null ? new HashMap JavaDoc() : ret;
34     }
35
36     private Class JavaDoc defineClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
37
38         if (dontBother.contains(name)) {
39             throw new ClassNotFoundException JavaDoc(name);
40         }
41         try {
42             if (name.startsWith("java.")) {
43                 return Class.forName(name);
44             }
45             if (importedClasses.containsKey(name)) {
46                 if (super.findLoadedClass(name) != null) {
47                     return super.findLoadedClass(name);
48                 }
49             }
50             ClassReader reader = new ClassReader(getResourceAsStream(name.replace('.', '/') + ".class"));
51             PMDASMVisitor asmVisitor = new PMDASMVisitor();
52             reader.accept(asmVisitor, 0);
53
54             List JavaDoc inner = asmVisitor.getInnerClasses();
55             if (inner != null && !inner.isEmpty()) {
56                 for (int ix = 0; ix < inner.size(); ix++) {
57                     String JavaDoc str = (String JavaDoc) inner.get(ix);
58                     ClassReader innerReader = new ClassReader(getResourceAsStream(str.replace('.', '/') + ".class"));
59                     innerReader.accept(asmVisitor, 0);
60                 }
61             }
62             importedClasses.put(name, asmVisitor.getPackages());
63             ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
64             reader.accept(writer, 0);
65
66             byte[] byteCode = writer.toByteArray();
67             return defineClass(name, byteCode, 0, byteCode.length);
68         } catch (ClassNotFoundException JavaDoc e) {
69             dontBother.add(name);
70             throw e;
71         } catch (IOException JavaDoc e) {
72             dontBother.add(name);
73             throw new ClassNotFoundException JavaDoc(name, e);
74         }
75     }
76 }
Popular Tags