The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

solution

Posted by Erik Buitenhuis on July 18, 2000 at 4:11 AM

Thanks to Kishori I found a solution to the problem.
Because the method I need to invoke is private, I need to use getDeclaredMethod() instead of getMethod(). This method does not work through superclass methods so I needed to sort of re-invent inheritance. see the code snippet below!

WHY ISN'T THIS IN THE JAVA LANGUAGE?

// call setParent method of node's subclass
// if it's not in the subclass NoSuchMethodException is thrown
// and we use the super class method

for (Class cls = node.getClass(); ; cls = cls.getSuperclass()) {
try {

// getMethod() gets only public methods,
// getDeclaredMethod () gets all
// methods you have access to (but no inherited methods!).
Class[] argClass = {Node.class};
Method m = cls.getDeclaredMethod("setParent", argClass);

// invoke cls.setParent()
Object[] arg = {this};
if (((Boolean)(m.invoke(node, arg))).booleanValue()) break;
return false;

} catch (NoSuchMethodException e) {
continue; // try super class
} catch (Exception e) {
Debug.println(""+e);
Debug.assert(false); // never gets here
}
}






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us