Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically inspect .class files

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?

like image 281
Mike Deck Avatar asked Oct 02 '08 20:10

Mike Deck


People also ask

How do I view a .class file?

A simple way to see what String literals are used in a ". class" file is to use the javap utility in your JDK installation to dump the file using the "-v" option. Then grep for text that looks like <String "..."> where ... is the String you are looking for.

Can we read .class file?

The language it produces is still bytecode (not anything like Java), but it's fairly readable and extremely instructive. Also, if you really want to, you can open up any . class file in a hex editor and read the bytecode directly. The result is identical to using javap .

How do I view a .class file in eclipse?

Clicking anywhere on the class name that we want to open and pressing F3. Clicking anywhere on the class name and going to the menu under Navigate > Open Declaration.


2 Answers

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.

like image 174
Tom Hawtin - tackline Avatar answered Sep 22 '22 22:09

Tom Hawtin - tackline


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

like image 42
Ken Avatar answered Sep 19 '22 22:09

Ken