blob: bc33dc2e28b9e4d1ec1444c5cd129991791b1e32 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.bnd.make.coverage;
2
3import java.io.*;
4import java.lang.reflect.*;
5import java.util.*;
6
7import aQute.lib.osgi.*;
8import aQute.lib.osgi.Clazz.*;
9
10/**
11 * This class can create a coverage table between two classspaces. The
12 * destination class space is used to create a table of methods. All source
13 * methods that refer to a specific dest are then filled into the table.
14 *
15 */
16public class Coverage {
17
18 /**
19 * Create a cross reference table from source to dest.
20 *
21 * @param source
22 * The methods that refer to dest
23 * @param dest
24 * The methods that are being referred to
25 * @return A mapping of source methods to destination methods.
26 * @throws IOException
27 */
28 public static Map<MethodDef, List<MethodDef>> getCrossRef(
29 Collection<Clazz> source, Collection<Clazz> dest)
30 throws Exception {
31 final Map<MethodDef, List<MethodDef>> catalog = buildCatalog(dest);
32 crossRef(source, catalog);
33 return catalog;
34 }
35
36 private static void crossRef(Collection<Clazz> source,
37 final Map<MethodDef, List<MethodDef>> catalog) throws Exception {
38 for (final Clazz clazz : source) {
39 clazz.parseClassFileWithCollector(new ClassDataCollector() {
40 MethodDef source;
41
42 public void implementsInterfaces(String names[]) {
43 MethodDef def = new MethodDef(0, clazz.getFQN(),
44 "<implements>", "()V");
45 for (String interfaceName : names) {
46 interfaceName = Clazz.internalToFqn(interfaceName);
47 for (Map.Entry<MethodDef, List<MethodDef>> entry : catalog
48 .entrySet()) {
49 String catalogClass = entry.getKey().clazz;
50 List<MethodDef> references = entry.getValue();
51
52 if (catalogClass.equals(interfaceName)) {
53 references.add(def);
54 }
55 }
56 }
57 }
58
59 // Method definitions
60 public void method(MethodDef source) {
61 this.source = source;
62 }
63
64 public void reference(MethodDef reference) {
65 List<MethodDef> references = catalog.get(reference);
66 if (references != null) {
67 references.add(source);
68 }
69 }
70 });
71 }
72 }
73
74 private static Map<MethodDef, List<MethodDef>> buildCatalog(
75 Collection<Clazz> sources) throws Exception {
76 final Map<MethodDef, List<MethodDef>> catalog = new TreeMap<MethodDef, List<MethodDef>>();
77 for (final Clazz clazz : sources) {
78 clazz.parseClassFileWithCollector(new ClassDataCollector() {
79
80 public boolean classStart(int access, String name) {
81 return clazz.isPublic();
82 }
83
84 public void method(MethodDef source) {
85 if (java.lang.reflect.Modifier.isPublic(source.access)
86 || Modifier.isProtected(source.access))
87 catalog.put(source, new ArrayList<MethodDef>());
88 }
89
90 });
91 }
92 return catalog;
93 }
94
95}