• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

actual class of method

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. ...
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic