I have an assignment where i need to write a Java program that parses a .class file and retrieves things like :
1.name of the .java file
2.implemented interfaces
3.variables
4.constructors
5.methods
I don't have any ideeas where to begin from?
For example,what kind of Data I/O structure should I use?
You can you ClassParser which is available in Apache commons library. you can read the Javadoc here. You can download apache commons from here
You can also use Java reflection API which provides method such as getDeclaredFileds, getDeclaredMethods etc.
There are already several libraries for classfile parsing out there. Objectweb ASM is the most popular.
If you have to do it from scratch, that I'd recommend starting by the JVM specification, which explains the binary layout of classfiles in detail. After that, parsing is just a simple matter of programming. I've written a classfile parser before, it's not that hard.
You don't need any external library, just use java.lang.Class. Write the name of your class:
[NameOfMyClass].class.getDeclaredFields();
[NameOfMyClass].class.getDeclaredConstructors();
[NameOfMyClass].class.getDeclaredMethods();
It's the same for interfaces and many other attributes.
You can use Java Reflection. Here is a good tutorial -->
Java Reflection Tutorial
OpenJDK actually comes with an API that lets you parse and manipulate class files programmatically that most programmers don't know about. It is located at the package com.sun.org.apache.bcel.internal.
Related
I wanted to decode a Base64 string in my XPage for which I was using sun.misc.BASE64Decoder class. But according to Java developer should not write programs that call 'sun' packages. I was searching for an alternative when I stumbled on com.ibm.misc.BASE64Decoder. It worked for me with same results as sun.misc.BASE64Decoder. So I would like to know if it is okay for developers to use this package and its classes? Or is it to be avoided like 'sun' package?
Also I know that I can use Apache Commons for Base64 but I would like to minimize my dependency on external JARs.
With com.ibm.misc.BASE64Decoder you'll have exactly the same problem as with sun.misc.BASE64Decoder: it's an internal class which only exists in a specific JVM implementation, in this case IBM's JVM.
Note that there is no com.ibm.misc.BASE64Decoder in Oracle's JVM, so if you use this class, your program is not going to work on Oracle's JVM; it will fail with a NoClassDefFoundError.
You could use the method that mre refers to in his comment, which is in the class javax.xml.bind.DatatypeConverter - part of the JAXB API, which is part of the standard Java API (since Java SE 6).
I want to use this which says to use a particular method I have to include tcutil.h in my java code. Can anybody help me, how to do that ? Another point: we can easily create an header file and include it in to C code but why reverse is so hard (means lots of work have to do) ? May be stupid, but little bit hints will be helpful.
This might be more complicated than you think. The .h files are C language include files which usually describe the methods and data structures of a C-library. You would have to Java Native Interface (JNI) in order to include these libraries in your java code.
You have basically two options
Go through a tutorial like this
Or look for a java implementation.
There're already java-bindings available.
You can't include C/C++ headers into java source code.
Maybe you want to define a native implementation for a java method.
http://java.sun.com/docs/books/jni/
They seem to have a Java API, which you need to download and include in your classpath. You can't include a C header file in Java.
you cannot do it directly in java. You have to include the header files in your C program and use JNI to call the functions that you want to use.
Refer this : JNI reference
To run native code from Java, you need using JNI technology. Try http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html or http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html of google by keywords "JNI, tutorial".
The page mentions that there is an API for Java available, but does not show it. You should ask them for the Java documentation. Preferrably, the API should be a JDBC driver.
I would like to write toy IDE for Java, so I ask a question about one particular thing that as I hope can help me get started.
I have editor implemented on top of swing and i have some text in there. There is for example:
import java.util.List;
Now I need a way to send "java.util.List" string to a method that returns me all the information I may need including JavaDoc document.
So is there any tool that can set up classpath with libraries, that would parse every string I send and try to find if there is any Class/Interface with documentation to return?
So is there any tool that can set up classpath with libraries, that would parse every string I send and try to find if there is any Class/Interface with documentation to return?
AFAIK, no. There is no such free-standing tool or library. You will need to implement it yourself. (Don't expect that writing a Java IDE is simple ... even a "toy" one.)
Libraries will have class files, which will not have javadocs.. So it is not clear what you want to do.
There are many byte code engineering tools to analyse and extract information from class files. For example asm or bcel. Javassist allows to process both source and byte code, so may be close to what you need.
You could use html parser to get the javadoc and other info from the web using the full path to the class (including package names to construct the correct URL per class). This will of course depend on the version of java you are using.
You can also use the javadoc tool from within java to generate the desired documentation from java source files (which can be downloaded from the web). The source code of the tool could also help you out. See http://java.sun.com/j2se/javadoc/faq/#developingwithjavadoc
Lastly, if you need information based on runtime types in your program, you might want to check reflection capabilities.
First you need to know How to print imported java libraries?. Then download java API documentation here. Once you find out imported libraries, open an inputStream in order to read appropriate HTML file.
Beware! This technic will only work when importing from jdk.
How can I create Java Pojo at runtime? ASM or BCEL is required for this?
I am reading a XML file and I want to generate Pojo according to this xml.
There are lot of libraries available to generate classes in runtime. If you want to create a class and write it back to disk, BCEL and CGLIB is good. If you want most of them for runtime only, CGLIB is probably the best
You might find that generating the code in memory is much easier to work with. (ASM is very good otherwise) With generated code you just have to create the java you would need.
You can use tools like BeanShell or the Compiler API
There's a nice open source library for reading xml to objects called Xstream. Use that and you don't need to worry about manually parsing the XML or manually creating objects.
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?
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.
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
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!)
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.
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.
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.