Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 38953 invoked from network); 21 Jul 2002 05:46:19 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 21 Jul 2002 05:46:19 -0000 Received: (qmail 25075 invoked by uid 97); 21 Jul 2002 05:46:42 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 25059 invoked by uid 97); 21 Jul 2002 05:46:41 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 25047 invoked by uid 98); 21 Jul 2002 05:46:41 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) X-Server-Uuid: 6E802067-ECFC-4FC2-A617-DD5220DD9CBB Message-ID: <7382FCA44E27D411BD4A00508BD68F9504BB09A2@pigeon.tumbleweed.com> From: "Martin Cooper" To: "'commons-dev@jakarta.apache.org'" Subject: [BeanUtils] Calling a static method via MethodUtils Date: Sat, 20 Jul 2002 22:41:35 -0700 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) X-WSS-ID: 112494E51194258-01-01 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 7bit X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N I posted the following to commons-user, but it turned out to be more of a -dev message, so I thought I'd repost here. Since writing this, I've discovered that invoking a static method which has primitive types as parameters doesn't seem to work, but I haven't yet determined whether that's because of the static method, the primitive types, or something else. -- Martin Cooper -----Original Message----- From: Martin Cooper [mailto:martin.cooper@tumbleweed.com] Sent: Friday, July 19, 2002 8:39 PM To: 'commons-user@jakarta.apache.org' Subject: [BeanUtils] Calling a static method via MethodUtils As far as I can tell, there is no way to use MethodUtils to call a static method. This is because the 'object' parameter to invokeMethod() (and invokeExactMethod()) is used both to obtain the class object and to pass to Method.invoke(). To call a static method, the value passed to Method.invoke() should be null, but if I pass null to invokeMethod(), the call to object.getClass() is going to fail with an NPE. Is this something that might be fixed/added in MethodUtils? Or does invoking static methods fall outside the scope of the BeanUtils package? I've appended some code to consider at the end of this message. Also, since I can't use invokeMethod() to do what I need, I thought perhaps I could at least leverage getMatchingAccessibleMethod(), but that method is private. (Just to complicate matters, I need to call a static method which has primitive types in its parameter list. :) Since this method does some handy stuff, it might be worth considering making it public. Thanks for your consideration. -- Martin Cooper One way to enable calling static methods would be to factor the lowest-level invokeMethod() method as follows: // This is the existing method, refactored to call the new one. public static Object invokeMethod( Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object.getClass(), object, methodName, args, parameterTypes); } // This is the new method, which takes a Class parameter. public static Object invokeMethod( Class clazz, Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method method = getMatchingAccessibleMethod( clazz, methodName, parameterTypes); if (method == null) throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + object.getClass().getName()); return method.invoke(object, args); } This way, the new method uses the 'object' parameter only to pass to method.invoke(), so that I can pass a value for 'clazz' and null for 'object' in order to invoke a static method. -- To unsubscribe, e-mail: For additional commands, e-mail: -- To unsubscribe, e-mail: For additional commands, e-mail: