Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.compatibility; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 5 | import aQute.bnd.osgi.*; |
| 6 | import aQute.bnd.osgi.Descriptors.TypeRef; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 7 | |
| 8 | public class ParseSignatureBuilder { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | final Scope root; |
| 10 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | public ParseSignatureBuilder(Scope root) { |
| 12 | this.root = root; |
| 13 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | |
| 15 | public void add(Jar jar) throws Exception { |
| 16 | for (Resource r : jar.getResources().values()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | InputStream in = r.openInputStream(); |
| 18 | try { |
| 19 | parse(in); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | } |
| 21 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | in.close(); |
| 23 | } |
| 24 | } |
| 25 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 26 | |
| 27 | public Scope getRoot() { |
| 28 | return root; |
| 29 | } |
| 30 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | public void parse(InputStream in) throws Exception { |
| 32 | Analyzer analyzer = new Analyzer(); |
| 33 | Clazz clazz = new Clazz(analyzer, "", null); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 49 | // s.setBase(new GenericType(name)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 60 | // tss[i] = new GenericType(names[i]); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 97 | s.setEnclosing(enclosing); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 98 | if (declaring != null) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 99 | s.setDeclaring(declaring); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 100 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 111 | public void innerClass(TypeRef innerClass, TypeRef outerClass, String innerName, int innerClassAccessFlags) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 112 | if (outerClass != null && innerClass != null && innerClass.getBinary().equals(s.name)) |
| 113 | declaring = root.getScope(outerClass.getBinary()); |
| 114 | } |
| 115 | }); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 116 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 117 | } |
| 118 | } |