Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 76 |  | Methods: | 4
| NCLOC: | 59 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
PMDASMClassLoader.java | 71.4% | 86.2% | 100% | 83% |

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