Is it possible?
Hello all,
I posted this topic on another forum.. But cant find a solutiuon.... So i am trying this forum ...
you are given a Class A and a Method "method_a".
1) Is it possible to determine whether the given method is an overridden method of any super class of A ?
2) is it possible to find whether the given method is a method of any interface that the class A implements?
Thanks in advance
[429 byte] By [
apppua] at [2007-9-19]

do you want this find out in compile time or runtime?
if you don't want find to run time
have another class which calls this method. Change the method name in the subclass to something else and compile. if it gives an error method not declared it is not overriding else overriding.
Tiju
I want to know these during run time.... i get class and want to find all those overridden methods
> Reflection...?Yea. You can check what methods a class has, and you can ask what the superclass and implemented interfaces are. Check out java.lang.reflect.* and java.lang.Class. Everything you need is there.
This sample code will give you some idea. (It is not perfect!).
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) throws Exception {
String className = args[0];
String methodName = args[1];
if (isOverridden(className, methodName)) {
System.out.println("Method " + methodName + " of class " + className + " is overridden");
} // if
else {
System.out.println("Method " + methodName + " of class " + className + " is not an overridden one");
} // else
} // main
public static boolean isOverridden(String className, String methodName) throws Exception {
Class base;
boolean overridden = false;
// load child class
Class child = Class.forName(className);
// get base class
base = child.getSuperclass();
// if method exists in methods of superclass
Method[] methods = base.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(methodName)) {
// found
overridden = true;
break;
} // if
} // for
return overridden;
} // isOverridden
} // Demo
That code is flawed in that it doesn't check the input parameters to the methods. There are some other details, but I just wanted to point this out.
Is there any api in reflect package that gives you all the inherited members names?
The code is meant to be a sample of what can be achieved via reflection and is solely meant to give some pointers. That's why I clearly mentioned that the code was not perfect while posting. Thanks anyway.
Thank you all dear friends for your help... I thought there exist some method by which i dont have to search up to the Object class to make sure that the given method is not an overrideen method... Any way i think it would be usefull if i publish my final program... If any one finds any effiecient method please post it....
/**
* Prints all the overridden methods of the given Class that is passed as an argument
*
*/
void findOverriddenMethods(Class cla){
Method decMethods[]=cla.getDeclaredMethods();
Class decClass=null;
for(int i=0;i<decMethods.length;i++){
decClass=findDeclaredClass(cla,decMethods[i]);
if(decClass!=null && !(decClass.equals(cla))){
System.out.println(decMethods.getName()+" is an overridden method");
}
}
}
/**
*Return the first super class where the same signature is found... or null is returned
*
*/
Class findDeclaredClass(Class startingClass,Method method){
Class parentCLass=startingClass.getSuperclass();
if(parentCLass==null)
return null;
else{
try{
parentCLass.getDeclaredMethod(method.getName(),method.getParameterTypes());
return parentCLass;
}
catch(NoSuchMethodException ex){
return findDeclaredClass(parentCLass,method);
}
}
}
>
