Skip to content

Commit

Permalink
Replaced couple of Class based code with asm ClassNode stuff.
Browse files Browse the repository at this point in the history
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
Atsushi Eno committed Sep 8, 2011
1 parent 8f49f78 commit 8510ea0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
19 changes: 7 additions & 12 deletions AndroidDocScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down Expand Up @@ -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 ('$', '.'));
}
}
3 changes: 2 additions & 1 deletion IDocScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
1 change: 1 addition & 0 deletions JavaArchive.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public List<JavaPackage> 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 ();
Expand Down
45 changes: 36 additions & 9 deletions JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,8 +58,9 @@ public class JavaClass implements Comparable<JavaClass> {
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)
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -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");
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 8510ea0

Please sign in to comment.