I am trying to write a program that takes a .class file and collects all the methods of the .class file as well as the contents of each method. Here is my code
public class ClassReaderTest1 {
public static void main(String[] args) throws Exception{
InputStream in = new FileInputStream("*.class");
ClassReader reader = new ClassReader(in);
ClassNode classNode = new ClassNode();
reader.accept(classNode,0);
@SuppressWarnings("unchecked")
final List<MethodNode> methods = classNode.methods;
for(MethodNode m: methods){
InsnList inList = m.instructions;
System.out.println(m.name);
for(int i = 0; i< inList.size(); i++){
System.out.println(" " + Integer.toHexString(inList.get(i).getOpcode()));
}
}
}
}
and here is my output
init>
ffffffff
ffffffff
19
b7
b1
ffffffff
main
ffffffff
ffffffff
b2
12
b6
ffffffff
ffffffff
3
36
ffffffff
ffffffff
b1
ffffffff
Ultimately I don't want to print these values, I just want to be able to reference them in my program (I'm trying to check that I'm getting the right values). I am getting the methods as expected but the contents of the methods aren't making sense to me. As I see it these are not opcodes; in particular "fffffff" is not a java opcode. What I would like to do is print out all the methods as I have done above and then where I have the opcodes now, print out the java bytecode followed by a few spaces and then the opcode. For example
main
bytecode **
.
.
The file that I am loading into this program consists of only a main method, a single println statement, and an initialization of a int variable.
My question then is am I doing something wrong, or am I just not interpreting my results correctly? In addition, how can I get the the bytecode? I haven't been able to find a way to get it. I can see it when I use the java bytecode outline plugin for eclipse, but I need to be able to reference it in my program.
Thanks in advance