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