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