Programmatically inspect .class files

走远了吗. 提交于 2019-12-18 13:09:27

问题


I'm working on a project where we're doing a lot of remote object transfer between a Java service and clients written in other various languages. Given our current constraints I've decided to see what it would take to generate code based on an existing Java class. Basically I need to take a .class file (or a collection of them) parse the bytecode to determine all of the data members and perhaps getters/setters and then write something that can output code in a different language to create a class with the same structure.

I'm not looking for standard decompilers such as JAD. I need to be able to take a .class file and create an object model of its data members and methods. Is this possible at all?


回答1:


I've used BCEL and find it really quite awkward. ASM is much better. It very extensively uses visitors (which can be a little confusing) and does not create an object model. Not creating an object model turns out to be a bonus, as any model you do want to create is unlikely to look like a literal interpretation of all the data.




回答2:


I have used BCEL in the past and it was pretty easy to use. It was a few years ago so there may be something better now.

Apache Jakarta BCEL




回答3:


From your description, it sounds like simple reflection would suffice. You can discover all of the static structure of the class, as well as accessing the fields of a particular instance.

I would only move on to BCEL if you are trying to translate method instructions. (And if that's what you're trying to automate, good luck!)




回答4:


I'm shocked that no one has mentioned ASM yet. It's the best bytecode library your money can buy. Well, ok it's free.




回答5:


JAD is a java decompiler that doesn't allow programmatic access. It isn't readily available anymore, and probably won't work for newer projects with Java7 bytecodes.




回答6:


I think javassist might help you too.

http://www.jboss.org/javassist/

I have never had the need of using it, but if you give it a try, would you let us know your comments about it?

Although I think it is more for bytecode manipulation than .class inspection.



来源:https://stackoverflow.com/questions/164378/programmatically-inspect-class-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!