You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Rashid Desai <ra...@hotmail.com> on 2001/04/03 21:05:14 UTC

PropertyUtils.java

I have found out that the method getAccessibleMethod(Method method) in 
PropertyUtils.java does not return the method as acessible public method of 
the class if the class implements an interface which inherits from other 
interfaces.

e.g. com.sun.xml.CommentNode implements org.w3c.dom.Comment Interface which 
implements org.w3c.dom.Node and org.w3c.dom.CharacterData Interfaces.

The method getNodeName is a get (Read) Property of the Node Interface. 
Looking for that property/method returns null in the current implementation.

I have modified the getAccessibleMethod() which uses the added new private 
method called getAccessibleMethodFromInterfaceNest(..) which recursively 
searches for the method through the interface heirarchy.

I Would appreciate any feedback and hope that this change can be 
incorporated into the core.

Rashid Desai

/**
* Return an accessible method (that is, one that can be invoked via
* reflection) that implements the specified Method. If no such method
* can be found, return null.
*
* @param method The method that we wish to call
*/
private static Method getAccessibleMethod(Method method) {
   // Make sure we have a method to check
   if (method == null) {
      return (null);
    }

   // If the requested method is not public we cannot call it
   if (!Modifier.isPublic(method.getModifiers())) {
      return (null);
   }

   // If the declaring class is public, we are done
   Class clazz = method.getDeclaringClass();
   if (Modifier.isPublic(clazz.getModifiers())) {
   return (method);
   }

   // Check the implemented interfaces
   String methodName = method.getName();
   Class[] parameterTypes = method.getParameterTypes();

   //nest through interface heirarchy looking for the method
   method = 
getAccessibleMethodFromInterfaceNest(clazz,methodName,parameterTypes);
   return (method);
}

/**
* Return an accessible method (that is, one that can be invoked via
* reflection) by nesting from parent interface through Inherited interfaces
* to look for the interface that implements the specific Method.
* If no such method can be found, return null.
*
*@param parentInterfaceClass The parent Interface from which we nest through 
looking for accessible method
*@param method The method that we wish to call
*/
private static Method getAccessibleMethodFromInterfaceNest(Class 
parentInterfaceClass, String methodName, Class[] parameterTypes)
{
    Method method=null;
    // Check the implemented interfaces
    Class[] interfaces = parentInterfaceClass.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {

      // Is this interface public?
      if (!Modifier.isPublic(interfaces[i].getModifiers())) {
      continue;
      }

      // Does the method exist on this interface?
      try {
        method = interfaces[i].getDeclaredMethod(methodName,
        parameterTypes);
      } catch (NoSuchMethodException e) {

      //try recursively looking for the method
      method = 
getAccessibleMethodFromInterfaceNest(interfaces[i],methodName,parameterTypes);
      }
    }
   // Return whatever we have null or a good method
   return (method);
}
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com