Permalink
Browse files

Replaced couple of Class based code with asm ClassNode stuff.

Field retrieval is now more cautious. Try getDeclaredFields() first then
try to retrieve all fields based on asm.fields. Fixed maps.jar issue.
  • Loading branch information...
1 parent 8f49f78 commit 8510ea015df789abea152e0186b1ec7aea10ba4f @atsushieno atsushieno committed Sep 8, 2011
Showing with 46 additions and 22 deletions.
  1. +7 −12 AndroidDocScraper.java
  2. +2 −1 IDocScraper.java
  3. +1 −0 JavaArchive.java
  4. +36 −9 JavaClass.java
View
@@ -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<String> getDeprecatedFields (Class cls)
+ public static List<String> 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<String> getDeprecatedMethods (Class cls)
+ public static List<String> 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 ('$', '.'));
}
}
View
@@ -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);
}
View
@@ -70,6 +70,7 @@ public JavaArchive (String filename, List<String> additionalJars) throws Excepti
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 ();
View
@@ -47,6 +47,8 @@
public class JavaClass implements Comparable<JavaClass> {
+ public static Map<String,ClassNode> asmClasses = new HashMap<String,ClassNode> ();
+
private Class jclass;
private ClassNode asm;
private Map<String,FieldNode> asmFields;
@@ -56,8 +58,9 @@
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<String,FieldNode> ();
for (FieldNode fn : (List<FieldNode>) 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<Field> l = new ArrayList<Field> ();
+ 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)
{

0 comments on commit 8510ea0

Please sign in to comment.