Showing posts with label decompile. Show all posts
Showing posts with label decompile. Show all posts

Saturday, 30 April 2011

Every annotation in JDK 1.5 onwards has a corresponding class

Every annotation in JDK 1.5 onwards has a corresponding class. For example, the @override annotation which tells that a method is an overridden version of the method inherited from the super class, has a corresponding Override.class.  

Similarly we have the Deprecated.class for the @deprecated annotation. We can see the contents of the corresponding source file to check what all attributes/parameters are being supported by the @override annotation. The Override.class has the following contents:

/  (version 1.5 : 49.0, no super bit)
@java.lang.annotation.Target(value={java.lang.annotation.ElementType.METHOD})
@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.SOURCE)
public abstract @interface java.lang.Override extends java.lang.annotation.Annotation {

}


This suggests us that the annotation @Override can have two attributes viz Target and Retention. The value accepted by Target attribute is of type METHOD and of Retention is SOURCE.

The decompiled code of the Override.class is:

package java.lang;
import java.lang.annotation.Annotation;
// Referenced classes of package java.lang:
// Object

public interface Override extends Annotation {
}


This is applicable to all annotations that you may come across in various frameworks/tools. All annotations in Spring, Hibernate, JPA are backed by corresponding classes. If you decompile the code which makes use of annotations, you will not see any reference to annotation classes as the dependency with annotations is resolved at compile time only. Thus annotations are Meta-data for the compiler which it makes use of to perform common functions for developers thus easing the life of a Java application developer.

Friday, 15 April 2011

Decompile Java Class file using decompilers.

Byte codes generated by javac compiler can again be converted into java source. For this we need a decompiler tool. Decompilers are the utilities that generate the source code from input java class file. A Decompiler knows about the structure of a Java class and parse it to generated Java source code.
Java decompilers will not give the exact Java source file from which class was generated and executed by Java Virtual Machine. But most of the structure will be maintained.

JAD: Java Decompiler

A lot of Decompilers are available in the market to decompile a class file. We will use one of the free decomipler called JAD.

I have used a simple Hello World java class to demonstrate JAD decompiler. Following is the code of HelloWorld.java file.
public class HelloWorld {
public static void main(String args[]) {
String fooBar = "Hello World from Java.";
System.out.println(fooBar);
}
}
Use following output when we use command line utility JAD to decompile our class file.
$> jad HelloWorld.class
Parsing HelloWorld.class... Generating HelloWorld.jad
The source file output of JAD is in file with .jad extension. You can change the extension to .java.
Following is the source that you get after decompillng our HelloWorld.class file.
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: HelloWorld.java

import java.io.PrintStream;

public class HelloWorld
{

public HelloWorld()
{
}

public static void main(String args[])
{
String s = "Hello World from Java.";
System.out.println(s);
}
}

Note that the string variable that we used in our original Java class was fooBar and it got changed into s.