diff --git a/AndroidDocScraper.java b/AndroidDocScraper.java index ac77c3f..cbaab09 100644 --- a/AndroidDocScraper.java +++ b/AndroidDocScraper.java @@ -30,6 +30,7 @@ import java.util.*; import javax.xml.parsers.*; import org.w3c.dom.*; +import org.objectweb.asm.tree.*; public class AndroidDocScraper implements IDocScraper { @@ -53,9 +54,9 @@ public AndroidDocScraper (File dir) throws IOException { throw new IllegalArgumentException (dir.getAbsolutePath() + " does not appear to be an android doc reference directory."); } - public String[] getParameterNames (Class declarer, String name, Type[] ptypes, boolean isVarArgs) + public String[] getParameterNames (ClassNode asm, String name, Type[] ptypes, boolean isVarArgs) { - String path = declarer.getName ().replace('.', '/').replace ('$', '.') + ".html"; + String path = asm.name.replace ('$', '.') + ".html"; File file = new File(root.getPath() + "/" + path); if (!file.isFile ()) return null; @@ -137,23 +138,17 @@ public static void loadXml (String filename) } } - public static List getDeprecatedFields (Class cls) + public static List getDeprecatedFields (ClassNode asm) { if (deprecatedFields == null) return null; - String name = cls.getName (); - String pkg = cls.getPackage ().getName (); - name = pkg.replace (".", "/") + "/" + name.substring (pkg.length () + 1).replace ("$", "."); // foo/bar/Baz.Nested - return deprecatedFields.get (name); + return deprecatedFields.get (asm.name.replace ('$', '.')); } - public static List getDeprecatedMethods (Class cls) + public static List getDeprecatedMethods (ClassNode asm) { if (deprecatedMethods == null) return null; - String name = cls.getName (); - String pkg = cls.getPackage ().getName (); - name = pkg.replace (".", "/") + "/" + name.substring (pkg.length () + 1).replace ("$", "."); // foo/bar/Baz.Nested - return deprecatedMethods.get (name); + return deprecatedMethods.get (asm.name.replace ('$', '.')); } } diff --git a/IDocScraper.java b/IDocScraper.java index 2b16af9..99aea7c 100644 --- a/IDocScraper.java +++ b/IDocScraper.java @@ -25,9 +25,10 @@ package jar2xml; import java.lang.reflect.Type; +import org.objectweb.asm.tree.ClassNode; public interface IDocScraper { - String[] getParameterNames (Class declarer, String name, Type[] ptypes, boolean isVarArgs); + String[] getParameterNames (ClassNode declarer, String name, Type[] ptypes, boolean isVarArgs); } diff --git a/JavaArchive.java b/JavaArchive.java index a60f91a..bc4d7b5 100644 --- a/JavaArchive.java +++ b/JavaArchive.java @@ -70,6 +70,7 @@ public List getPackages () ClassReader reader = new ClassReader (stream); ClassNode node = new ClassNode (); reader.accept (node, 0); + JavaClass.asmClasses.put (node.name, node); Class c = loader.loadClass (name.replace ('/', '.')); //String pkgname = c.getPackage ().getName (); diff --git a/JavaClass.java b/JavaClass.java index ad65c5d..92b66f7 100644 --- a/JavaClass.java +++ b/JavaClass.java @@ -47,6 +47,8 @@ public class JavaClass implements Comparable { + public static Map asmClasses = new HashMap (); + private Class jclass; private ClassNode asm; private Map asmFields; @@ -56,8 +58,9 @@ public class JavaClass implements Comparable { public JavaClass (Class jclass, ClassNode asm) { this.jclass = jclass; - deprecatedFields = AndroidDocScraper.getDeprecatedFields (jclass); - deprecatedMethods = AndroidDocScraper.getDeprecatedMethods (jclass); + this.asm = asm; + deprecatedFields = AndroidDocScraper.getDeprecatedFields (asm); + deprecatedMethods = AndroidDocScraper.getDeprecatedMethods (asm); asmFields = new HashMap (); for (FieldNode fn : (List) asm.fields) @@ -71,13 +74,13 @@ public int compareTo (JavaClass jc) public String getName () { - return jclass.getName (); + return asm.name.replace ('/', '.'); } String[] getParameterNames (String name, Type[] types, boolean isVarArgs) { for (IDocScraper s : scrapers) { - String[] names = s.getParameterNames (jclass, name, types, isVarArgs); + String[] names = s.getParameterNames (asm, name, types, isVarArgs); if (names != null && names.length > 0) return names; } @@ -104,13 +107,18 @@ void appendParameters (String name, Type[] types, int typeOffset, boolean isVarA } } - String getConstructorName (Class c) + String getSimpleName (ClassNode asm) + { + return asm.name.substring (asm.name.lastIndexOf ('/') + 1).replace ('$', '.'); + } + + String getConstructorName (ClassNode asm) { String n = ""; - Class e = c.getEnclosingClass (); + ClassNode e = asmClasses.get (asm.outerClass); if (e != null) n = getConstructorName (e); - return (n != "" ? n + "." : n) + c.getSimpleName (); + return (n != "" ? n + "." : n) + getSimpleName (asm); } void appendCtor (Constructor ctor, Document doc, Element parent) @@ -119,7 +127,7 @@ void appendCtor (Constructor ctor, Document doc, Element parent) if (!Modifier.isPublic (mods) && !Modifier.isProtected (mods)) return; Element e = doc.createElement ("constructor"); - e.setAttribute ("name", getConstructorName (jclass)); + e.setAttribute ("name", getConstructorName (asm)); e.setAttribute ("type", getClassName (jclass, true)); e.setAttribute ("final", Modifier.isFinal (mods) ? "true" : "false"); e.setAttribute ("static", Modifier.isStatic (mods) ? "true" : "false"); @@ -518,13 +526,32 @@ else if (hret != null && !mret.isAssignableFrom (hret)) { appendMethod (methods.get (sig), doc, e); if (!jclass.isEnum ()) { // enums are somehow skipped. - Field [] fields = jclass.getDeclaredFields (); + Field [] fields = getDeclaredFields (); sortFields (fields); for (Field field : fields) appendField (field, asmFields.get (field.getName ()), doc, e); } parent.appendChild (e); } + + static final Field [] empty_array = new Field [0]; + + Field [] getDeclaredFields () + { + try { + return jclass.getDeclaredFields (); + } catch (NoClassDefFoundError ex) { + List l = new ArrayList (); + for (FieldNode fn : asm.fields) { + try { + l.add (jclass.getField (fn.name)); + } catch (NoClassDefFoundError exx) { + } catch (NoSuchFieldException exx) { + } + } + return l.toArray (empty_array); + } + } void sortFields (Field [] fields) {