• Post Reply Bookmark Topic Watch Topic
  • New Topic

actual class of method  RSS feed

 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a peculiar situation in which I need to know the name of the class in which a method was declared.

Suppose there is a hierarchy of classes A<-B<-C(C extends B and B extends A), and there is a method func() declared in A. As C extends A automatically, so the method will be available in Class C also.

In this situation there are only 3 classes but this could go upto a number of classes and now I am at a situation where I feel that I need to change the method which was declared in the first class, but I dont know which class is that.

So, is there any way to get the name of the class in which method was declared. e.g. in class C, get the name of class in which method was written for the first time.

Regards
Kapil
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generate javadocs for your code, and you will easily find the class where the method is declared.
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is fine but is there any way to do it programmatically.

I am from inside the program, I could find out the class in which that method was declared.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16049
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Class.getDeclaredMethods - see the API documentation. Quote from the API documentation:
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods. ...
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never tried it myself, but I did notice that they have


See :

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Method.html
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sol and Jesper for your help.

I have been able to get the solution with the help of java.lang.reflect.Method class.

The code run by me is

class Invoke2


class Invoke1


class Invoke


Now run the main method in Invoke.java and you will get the name of the class which declares the method.

Regards
Kapil
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!