Take the 2-minute tour ×

I have byte[] array which I know is a bytecode sequence for a method inside a class file. Is there some way to perform selective decompilation of this byte[] array? I do not have access to the actual .class file. I have looked at Javassist library that has a lot of methods (javassist.bytecode.CodeAttribute class) but I was unable to find such specific methods.
Anyone have a clue?

I can take this byte[] array and produce the actual bytecode to JavaByte code mnemonics using the JVM reference manual but I need to produce the decompiled Java source code. Thanks!

share|improve this question
add comment

2 Answers

I've never heard of dealing with the bytecode for a single method. I think you're going to find that you can't do anything with it. What bytecode do you have? Where did you get it from? Are you familiar with the class file format and how methods are stored? For instance, the name of a method is stored in the constant pool, which is part of the overall class file, not part of any one method. Keep reading about the method format to see what else has to be there. I very strongly suspect that either you don't have what you think you have in that byte[], or else you're not going to have enough information to make anything of it.

Disclaimer: I've only dabbled in bytecode manipulation, so don't take what I say as authoritative. Believe what the spec says.

share|improve this answer
I got the bytecode from the JPDA method com.sun.jdi.Method.bytecodes() function call. I want to decompile that so that I can just display the decompiled version of that function. I understand that there might be issues (e.g., xrefs to protected, private, static variables, etc.). But I was hoping there would be some way to decompile it. I do have valid bytecode in my byte[] array because I wrote my own bytecode decompilation method which creates mnemonics from the obtained bytecode (I verified it with the output of javap). –  tr4nc3 May 12 '12 at 17:01
Interesting. Well, there are decompilers out there. The only one I know for sure that offers a programmatic API is Java Decompiler. I guess it'll depend on how flexibile the decompiler is with regard to the input bytecode. I don't know JDI, but if you have a com.sun.jdi.Method, it seems you could make your way up to something representing the class and get more bytecode info. Perhaps something like method.declaringType(), which should give you a ClassType? –  Ryan Stewart May 12 '12 at 23:39
Right, the Method.declaringType() only gives a ReferenceType object. There is no method to get the bytecode from that object. There is a sourcePath() and sourceName() function call though which may get me the filename of the source (assuming I have the source) but that may not be the case most of the time (which is why I need a decompiler in the first place). I am not sure that JD-Eclipse guys offer a programmatic API. I believe they have it for Eclipse. Also, since it's closed source, I'd be reversing his / her released files which is not allowed per their license. –  tr4nc3 May 13 '12 at 6:11
add comment

It is impossible to produce valid Java source using only the bytecode of a specific method because all names are stored in the class wide constant pool, among other things. The best you can hope for is to generate bytecode mnemonics.

share|improve this answer
add comment

Your Answer


