3

this is about a hw assignment that includes reflection in Java. suppose I have 2 classe :

public class A{
   bool foo(){...}
}

public class B extends A{
   bool foo(){...}
}

suppose I have a function that receives Class<?> parameter, and I want to check if foo is overridden in some derived class, without knowing if there are any, how do I do that?

I read a few questing regarding the same subject, that mentioned isBridge, or getDeclaringclass but can't figure out what those do.

I have to mention that I don't know Java all that well. this is part of an Object Oriented Programming class I'm taking, so it's not meant to make us experts in Java but to demonstrate the principals. which means the solution to my problem shouldn't be too complex.

littlerunaway
  • 443
  • 2
  • 8
  • 18
  • If you need to use reflection, any solution will be complex. – Sotirios Delimanolis Jun 12 '15 at 21:10
  • Is the method public or package private like in your sample? – Sotirios Delimanolis Jun 12 '15 at 21:11
  • the method can be private or public or anything in between. I meant complex comparing to reflection in general. the whole assignment is about reflection – littlerunaway Jun 12 '15 at 21:34
  • What do you (or the exercise) mean by “overridden in some derived class”? There can be any number of derived classes, even added at run-time and in general, you'd have to solve the halting problem before deciding what classes are derived. I don't think that this question is meaningful without the derived class(es) given explicitly as arguments. – 5gon12eder Jun 12 '15 at 21:45
  • have fun:) - http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1 – ZhongYu Jun 12 '15 at 22:23
  • It's 'trivial' to parse backwards on the tree (going from derived to it's parent). But how in the world do you find derived classes from a parent class? Seems to border on impossible in the grand scheme of things, and damn near impossible unless you're dealing with some very finite environment. – KevinDTimm Jun 15 '15 at 13:06

1 Answers1

1

Given:

class A 
    {
        public void foo()
        {
            System.out.println("Ok on A");
        }
    }
    class B extends A
    {
        public void foo()           
        {
            System.out.println("Ok on B");
        }
    }

You could traverse the hierarchy tree of classes searching for the method

String methodName="foo";
Class currentClass=B.class.getSuperclass();
boolean overwritten=false;
while(currentClass!=Object.class && !overwritten)
{
    try
    {
        Method m=currentClass.getMethod(methodName);
        overwritten=true;
        break;
    }
    catch (NoSuchMethodException e) {
        // Method does not exists
    } catch (SecurityException e) {
        // Security exception
    }
    currentClass=currentClass.getSuperclass();
}

if overwritten is true, we found the method in some parent class.

Notice we are checking a method without parameters (foo), to find a method with parameters, you have to specify them as second parameter in getMethod call.

Now, if you want to find if "any class" has overriden the foo method, you will have to scan all classes in your classpath, search if they contains the target method, and do the same test for each one, or use a reflection library like org.reflections , you can find more information on subclass finding in the following post: how-do-you-find-all-subclasses-of-a-given-class-in-java

Community
  • 1
  • 1