prime
prime

Reputation: 15564

View a method's implementation (method body) using Java Reflection

I can view the methods of a class using java reflection.

    Method[] methods = Person.class.getMethods();
    for(Method method : methods){
        System.out.println("method = " + method);
    }

the person class ,

class Person{
String name;
int age;

Person(String name,int age){
    this.name = name;
    this.age = age;
}
public int count(){
    int count = 10*20;
    return count;
} 
}

Using reflection the count method will view like, method = public int Person.count() but not the implementation (method body).

is there anyway to view the full implementation details of a method using this kind of approach or with any other ?

Upvotes: 2

Views: 1074

Answers (2)

Rajesh
Rajesh

Reputation: 2155

There is a sourceforge project. It takes class file as input, decompiles it, provides the source file and is platform independent: Java Decompiler

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100139

It's not possible to do this using standard Java tools. The source method implementation is not accessible in runtime. You can however load this class file as a resource and pass it to some Java decompiler, which will try its best to produce the original source code of the class. Or course it's not guaranteed that the result will be 100% exact and it will be even worse if your class is compiled without debugging information.

Upvotes: 2

Related Questions