use gnu.bytecode directly, without java.lang.reflect

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@98 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent 771af8c5bb
commit 3136614896
  1. 180
      jode/jode/decompiler/MethodAnalyzer.java

@ -18,76 +18,109 @@
*/ */
package jode; package jode;
import java.lang.reflect.*; import java.lang.reflect.Modifier;
import gnu.bytecode.Attribute; import gnu.bytecode.Attribute;
import gnu.bytecode.CodeAttr; import gnu.bytecode.CodeAttr;
import gnu.bytecode.Spy;
public class MethodAnalyzer implements Analyzer { public class MethodAnalyzer implements Analyzer {
JodeEnvironment env; JodeEnvironment env;
CodeAnalyzer code = null; CodeAnalyzer code = null;
ClassAnalyzer classAnalyzer; ClassAnalyzer classAnalyzer;
boolean isConstructor; boolean isConstructor;
Method method; int modifiers;
Constructor constr; String methodName;
MethodType methodType;
Type[] exceptions;
private MethodAnalyzer(ClassAnalyzer cla, Method m, Constructor c, // private MethodAnalyzer(ClassAnalyzer cla, Method m, Constructor c,
JodeEnvironment e) // JodeEnvironment e)
{ // {
classAnalyzer = cla; // classAnalyzer = cla;
method = m; // isConstructor = (c != null);
constr = c; // env = e;
isConstructor = (c != null);
env = e; // this.modifiers = isConstructor
// ? c.getModifiers() : m.getModifiers();
// {
// Class[] exceptClasses = isConstructor
// ? c.getExceptionTypes() : m.getExceptionTypes();
// this.exceptions = new Type[exceptClasses.length];
// for (int i=0; i< exceptClasses.length; i++)
// exceptions[i] = Type.tType(exceptClasses[i]);
// }
// {
// Class[] paramTypes = (isConstructor
// ? c.getParameterTypes()
// : m.getParameterTypes());
// Class returnType = (isConstructor
// ? Void.TYPE : m.getReturnType());
// methodType = new MethodType
// (!isConstructor && Modifier.isStatic(modifiers),
// paramTypes, returnType);
// methodName = isConstructor ? "<init>" : m.getName();
// }
String name = isConstructor ? "<init>" : m.getName(); // gnu.bytecode.Method mdef;
Class[] paramTypes = (isConstructor // for (mdef = cla.classType.getMethods(); ; mdef = mdef.getNext()) {
? c.getParameterTypes() // if (mdef.getName().equals(methodName)) {
: m.getParameterTypes()); // MethodType type = new MethodType(mdef.getStaticFlag(),
// mdef.getSignature());
// if (type.equals(methodType))
// break;
// }
// }
// Attribute attr = Attribute.get(mdef, "Code");
// if (attr != null && attr instanceof CodeAttr)
// code = new CodeAnalyzer(this, (CodeAttr) attr, env);
// }
gnu.bytecode.Method mdef = cla.classType.getMethods(); public MethodAnalyzer(ClassAnalyzer cla, gnu.bytecode.Method mdef,
while (mdef != null) { JodeEnvironment env) {
if (mdef.getName().equals(name)) { this.classAnalyzer = cla;
gnu.bytecode.Type[] argtypes = mdef.getParameterTypes(); this.env = env;
if (argtypes.length == paramTypes.length) { this.modifiers = Spy.getModifiers(mdef);
int i; this.methodType = new MethodType(mdef.getStaticFlag(),
for (i=0; i<argtypes.length; i++) { mdef.getSignature());
if (!Type.tType(paramTypes[i]).equals(Type.tType(argtypes[i].getSignature()))) this.methodName = mdef.getName();
break; this.isConstructor =
} methodName.equals("<init>") || methodName.equals("<clinit>");
if (i == argtypes.length)
break; Attribute codeattr = Attribute.get(mdef, "Code");
if (codeattr != null && codeattr instanceof CodeAttr)
code = new CodeAnalyzer(this, (CodeAttr) codeattr, env);
Attribute excattr = Attribute.get(mdef, "Exceptions");
if (excattr == null) {
exceptions = new Type[0];
} else {
java.io.DataInputStream stream = Spy.getAttributeStream
((gnu.bytecode.MiscAttr) excattr);
try {
int throwCount = stream.readUnsignedShort();
this.exceptions = new Type[throwCount];
for (int t=0; t< throwCount; t++) {
int idx = stream.readUnsignedShort();
gnu.bytecode.CpoolClass cpcls = (gnu.bytecode.CpoolClass)
classAnalyzer.getConstant(idx);
exceptions[t] = Type.tClass(cpcls.getName().getString());
} }
} catch (java.io.IOException ex) {
throw new AssertError("exception attribute too long?");
} }
mdef = mdef.getNext();
} }
Attribute attr = Attribute.get(mdef, "Code");
if (attr != null && attr instanceof CodeAttr)
code = new CodeAnalyzer(this, (CodeAttr) attr, env);
}
public MethodAnalyzer(ClassAnalyzer cla, Method method,
JodeEnvironment env)
{
this(cla, method, null, env);
}
public MethodAnalyzer(ClassAnalyzer cla, Constructor constr,
JodeEnvironment env)
{
this(cla, null, constr, env);
} }
public int getParamCount() { public int getParamCount() {
return isConstructor return (methodType.isStatic() ? 0 : 1)
? (constr.getParameterTypes().length + 1) + methodType.getParameterTypes().length;
: ((Modifier.isStatic(method.getModifiers()) ? 0 : 1)
+ method.getParameterTypes().length);
} }
public Type getReturnType() { public Type getReturnType() {
return isConstructor return methodType.getReturnType();
? Type.tVoid : Type.tType(method.getReturnType());
} }
public void analyze() public void analyze()
@ -97,30 +130,26 @@ public class MethodAnalyzer implements Analyzer {
return; return;
int offset = 0; int offset = 0;
if (isConstructor || !Modifier.isStatic(method.getModifiers())) { if (!methodType.isStatic()) {
LocalInfo clazz = code.getParamInfo(0); LocalInfo clazz = code.getParamInfo(0);
clazz.setType(Type.tType(this.classAnalyzer.clazz)); clazz.setType(Type.tType(this.classAnalyzer.clazz));
clazz.setName("this"); clazz.setName("this");
offset++; offset++;
} }
Class[] paramTypes = isConstructor Type[] paramTypes = methodType.getParameterTypes();
? constr.getParameterTypes() : method.getParameterTypes();
for (int i=0; i< paramTypes.length; i++) for (int i=0; i< paramTypes.length; i++)
code.getParamInfo(offset+i).setType(Type.tType(paramTypes[i])); code.getParamInfo(offset+i).setType(paramTypes[i]);
Class[] exceptions = isConstructor
? constr.getExceptionTypes() : method.getExceptionTypes();
for (int i= 0; i< exceptions.length; i++) for (int i= 0; i< exceptions.length; i++)
env.useClass(exceptions[i]); exceptions[i].useType();
if (!isConstructor) if (!isConstructor)
getReturnType().useType(); methodType.getReturnType().useType();
if (!Decompiler.immediateOutput) { if (!Decompiler.immediateOutput) {
if (Decompiler.isVerbose) if (Decompiler.isVerbose)
System.err.print((isConstructor System.err.print(methodName+": ");
? "<init>" : method.getName())+": ");
code.analyze(); code.analyze();
if (Decompiler.isVerbose) if (Decompiler.isVerbose)
System.err.println(""); System.err.println("");
@ -135,56 +164,47 @@ public class MethodAnalyzer implements Analyzer {
// immediate output. // immediate output.
if (Decompiler.isVerbose) if (Decompiler.isVerbose)
System.err.print((isConstructor System.err.print(methodName+": ");
? "<init>" : method.getName())+": ");
code.analyze(); code.analyze();
if (Decompiler.isVerbose) if (Decompiler.isVerbose)
System.err.println(""); System.err.println("");
} }
writer.println(""); writer.println("");
String modif = Modifier.toString(isConstructor String modif = Modifier.toString(modifiers);
? constr.getModifiers()
: method.getModifiers());
if (modif.length() > 0) if (modif.length() > 0)
writer.print(modif+" "); writer.print(modif+" ");
if (isConstructor && Modifier.isStatic(constr.getModifiers())) if (isConstructor && methodType.isStatic())
writer.print(""); /* static block */ writer.print(""); /* static block */
else { else {
if (isConstructor) if (isConstructor)
writer.print(env.classString(classAnalyzer.clazz)); writer.print(env.classString(classAnalyzer.clazz));
else else
writer.print(getReturnType().toString() writer.print(getReturnType().toString()
+ " " + method.getName()); + " " + methodName);
writer.print("("); writer.print("(");
Class[] paramTypes = isConstructor Type[] paramTypes = methodType.getParameterTypes();
? constr.getParameterTypes() : method.getParameterTypes(); int offset = methodType.isStatic()?0:1;
int offset = (!isConstructor
&& Modifier.isStatic(method.getModifiers()))?0:1;
for (int i=0; i<paramTypes.length; i++) { for (int i=0; i<paramTypes.length; i++) {
if (i>0) if (i>0)
writer.print(", "); writer.print(", ");
LocalInfo li; LocalInfo li;
if (code == null) { if (code == null) {
li = new LocalInfo(i+offset); li = new LocalInfo(i+offset);
li.setType(Type.tType(paramTypes[i])); li.setType(paramTypes[i]);
} else } else
li = code.getParamInfo(i+offset); li = code.getParamInfo(i+offset);
writer.print(li.getType().toString()+" "+li.getName()); writer.print(li.getType().toString()+" "+li.getName());
} }
writer.print(")"); writer.print(")");
} }
Class[] exceptions = isConstructor if (exceptions.length > 0) {
? constr.getExceptionTypes() : method.getExceptionTypes();
if (exceptions != null && exceptions.length > 0) {
writer.println(""); writer.println("");
writer.print("throws "); writer.print("throws ");
for (int i= 0; i< exceptions.length; i++) { for (int i= 0; i< exceptions.length; i++) {
if (exceptions[i] != null) { if (i > 0)
if (i > 0) writer.print(", ");
writer.print(", "); writer.print(exceptions[i].toString());
writer.print(env.classString(exceptions[i]));
}
} }
} }
if (code != null) { if (code != null) {

Loading…
Cancel
Save