Reputation: 26518
My query is suppose we have number of classes suppose three classes are there like this:
See my updated query. That is the case they have asked for.
class MethodContainerA {
public void display() {
System.out.println("Display from MethodContainerA");
}
}
class MethodContainerB extends MethodContainerA {
public void display() {
System.out.println("Display from MethodContainerB");
}
}
class MethodContainerC extends MethodContainerB {
}
public class MethodCallRecognization {
public static void main(String[] args) {
MethodContainerC methodContainerC = new MethodContainerC();
methodContainerC.display();
// I want to know here
}
}
My question is that after calling can I get the information display()
method is called from which class. Note, this question is not for debugging purposes - it is theoretical.
Upvotes: 0
Views: 132
Reputation: 5625
Try this.
public class MethodCallRecognization {
public static void main(String[] args) {
MethodContainerC methodContainerC = new MethodContainerC();
methodContainerC.display();
// I want to know here
String className=getClassName(methodContainerC.getClass(), "display") ;
System.out.println(className);
}
public static String getClassName(Class c,String methodName){
Method m[]=c.getDeclaredMethods();
for(Method m1:m){
if(m1.getName().equals(methodName)){
return c.getName();
}
}
return getClassName(c.getSuperclass(),methodName);
}
}
If methods are overloaded you also want to check with Argument Types instead of names. but this should give you idea.
As per comment here is better version of getClassName
public static String getClassname(Class c, String methodName){
try{
Method m= c.getDeclaredMethod( methodName);
return c.getName();
}catch(NoSuchMethodException nse){
return getClassName(c.getSuperclass(),methodName);
}
}
If dsiplay Method is taking Parameter then
public class MethodCallRecognization {
public static void main(String[] args) {
MethodContainerC methodContainerC = new MethodContainerC();
methodContainerC.display("" , "");//Suppose it takes String id and String name parameter
// I want to know here
Class[] parametertype={String.class,String.class};
String className=getClassName(methodContainerC.getClass(), "display", parametertype) ;
System.out.println(className);
}
public static String getClassname(Class c, String methodName, Class[] parametertype){
try{
Method m= c.getDeclaredMethod( methodName, parametertype);
return c.getName();
}catch(NoSuchMethodException nse){
return getClassName(c.getSuperclass(),methodName, parametertype);
}
}
}
Upvotes: 2
Reputation: 47699
Well, actually there probably is a way to do this (haven't tried):
Get the Class of the instance used. Do getDeclaredMethod to get the Method. Do getDeclaringClass on the Method.
Upvotes: 3
Reputation:
You will be calling display()
from MethodContainerC
, and will thus print out Display from MethodContainerC
.
This is the way overriding works: If you want to call the superclass's method, you need to explicitly invoke super
from within the overridden method. Otherwise, you'll be calling the overridden method.
However, if you do not have a method display()
in MethodContainerC
, you will instead invoke MethodContainerB
's display method. This is because inheritance follows the closest path:
MethodContianerA
has a method display()
...MethodContainerB
...MethodContianerC
.)If you cut out the last step, MethodContainerB
still overrides display()
for all MethodContainerC
. The closest implementation of the method will always be run.
Upvotes: 1