blob: f1f91d14cb4e275624f28e0592ad9613617edb3d [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.bnd.compatibility;
2
3import java.io.*;
4
5import aQute.lib.osgi.*;
6import aQute.lib.osgi.Descriptors.TypeRef;
7
8public class ParseSignatureBuilder {
9 final Scope root;
10
11 public ParseSignatureBuilder(Scope root) {
12 this.root = root;
13 }
14
15 public void add( Jar jar ) throws Exception {
16 for ( Resource r : jar.getResources().values()) {
17 InputStream in = r.openInputStream();
18 try {
19 parse(in);
20 } finally {
21 in.close();
22 }
23 }
24 }
25
26 public Scope getRoot() { return root; }
27
28
29 public void parse(InputStream in) throws Exception {
30 Analyzer analyzer = new Analyzer();
31 Clazz clazz = new Clazz(analyzer, "", null);
32
33 clazz.parseClassFile(in, new ClassDataCollector() {
34 Scope s;
35 Scope enclosing;
36 Scope declaring;
37
38 @Override
39 public void classBegin(int access, TypeRef name) {
40 s = root.getScope(name.getBinary());
41 s.access = Access.modifier(access);
42 s.kind = Kind.CLASS;
43 }
44
45 @Override
46 public void extendsClass(TypeRef name) {
47// s.setBase(new GenericType(name));
48 }
49
50 @Override
51 public void implementsInterfaces(TypeRef names[]) {
52 s.setParameterTypes(convert(names));
53 }
54
55 GenericType[] convert(TypeRef names[]) {
56 GenericType tss[] = new GenericType[names.length];
57 for (int i = 0; i < names.length; i++) {
58// tss[i] = new GenericType(names[i]);
59 }
60 return tss;
61 }
62
63 @Override
64 public void method(Clazz.MethodDef defined) {
65 String descriptor;
66 Kind kind;
67 if (defined.isConstructor()) {
68 descriptor = ":" + defined.getDescriptor();
69 kind = Kind.CONSTRUCTOR;
70 } else {
71 descriptor = defined.getName() + ":" + defined.getDescriptor();
72 kind = Kind.METHOD;
73 }
74 Scope m = s.getScope(descriptor);
75 m.access = Access.modifier(defined.getAccess());
76 m.kind = kind;
77 m.declaring = s;
78 s.add(m);
79 }
80
81 @Override
82 public void field(Clazz.FieldDef defined) {
83 String descriptor = defined.getName() + ":" + defined.getDescriptor();
84 Kind kind = Kind.FIELD;
85 Scope m = s.getScope(descriptor);
86 m.access = Access.modifier(defined.getAccess());
87 m.kind = kind;
88 m.declaring = s;
89 s.add(m);
90 }
91
92 @Override
93 public void classEnd() {
94 if (enclosing != null)
95 s.setEnclosing( enclosing );
96 if (declaring != null)
97 s.setDeclaring( declaring );
98 }
99
100 @Override
101 public void enclosingMethod(TypeRef cName, String mName, String mDescriptor) {
102 enclosing = root.getScope(cName.getBinary());
103 if (mName != null) {
104 enclosing = enclosing.getScope(Scope.methodIdentity(mName, mDescriptor));
105 }
106 }
107
108 @Override
109 public void innerClass(TypeRef innerClass, TypeRef outerClass, String innerName,
110 int innerClassAccessFlags) {
111 if (outerClass != null && innerClass != null && innerClass.getBinary().equals(s.name))
112 declaring = root.getScope(outerClass.getBinary());
113 }
114 });
115
116
117 }
118}
119