?? methodutils.java
字號:
/* * $Header: /cvs/osworkflow/src/designer/com/opensymphony/workflow/designer/beanutils/MethodUtils.java,v 1.2 2004/04/16 10:36:29 hani Exp $ * $Revision: 1.2 $ * $Date: 2004/04/16 10:36:29 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgement may appear in the software itself, * if and wherever such third-party acknowledgements normally appear. * * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache" nor may "Apache" appear in their names without prior * written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package com.opensymphony.workflow.designer.beanutils;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.WeakHashMap;/** * <p> Utility reflection methods focussed on methods in general rather than properties in particular. </p> * * <h3>Known Limitations</h3> * <h4>Accessing Public Methods In A Default Access Superclass</h4> * <p>There is an issue when invoking public methods contained in a default access superclass. * Reflection locates these methods fine and correctly assigns them as public. * However, an <code>IllegalAccessException</code> is thrown if the method is invoked.</p> * * <p><code>MethodUtils</code> contains a workaround for this situation. * It will attempt to call <code>setAccessible</code> on this method. * If this call succeeds, then the method can be invoked as normal. * This call will only succeed when the application has sufficient security privilages. * If this call fails then a warning will be logged and the method may fail.</p> * * @author Craig R. McClanahan * @author Ralph Schaer * @author Chris Audley * @author Rey Fran鏾is * @author Gregor Ra齧an * @author Jan Sorensen * @author Robert Burrell Donkin */public class MethodUtils { // --------------------------------------------------------- Private Methods /** Only log warning about accessibility work around once */ private static boolean loggedAccessibleWarning = false; /** An empty class array */ private static final Class[] emptyClassArray = new Class[0]; /** An empty object array */ private static final Object[] emptyObjectArray = new Object[0]; /** * Stores a cache of Methods against MethodDescriptors, in a WeakHashMap. */ private static WeakHashMap cache = new WeakHashMap(); // --------------------------------------------------------- Public Methods /** * <p>Invoke a named method whose parameter type matches the object type.</p> * * <p>The behaviour of this method is less deterministic * than {@link #invokeExactMethod}. * It loops through all methods with names that match * and then executes the first it finds with compatable parameters.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a <code>Boolean</code> class * would match a <code>boolean</code> primitive.</p> * * <p> This is a convenient wrapper for * {@link #invokeMethod(Object object,String methodName,Object [] args)}. * </p> * * @param object invoke method on this object * @param methodName get method with this name * @param arg use this argument * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeMethod( Object object, String methodName, Object arg) throws NoSuchMethodException, InvocationTargetException { Object[] args = {arg}; return invokeMethod(object, methodName, args); } /** * <p>Invoke a named method whose parameter type matches the object type.</p> * * <p>The behaviour of this method is less deterministic * than {@link #invokeExactMethod(Object object,String methodName,Object [] args)}. * It loops through all methods with names that match * and then executes the first it finds with compatable parameters.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a <code>Boolean</code> class * would match a <code>boolean</code> primitive.</p> * * <p> This is a convenient wrapper for * {@link #invokeMethod(Object, String, Object[], Class[])} } }. * </p> * * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeMethod( Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (args == null) { args = emptyObjectArray; } int arguments = args.length; Class parameterTypes [] = new Class[arguments]; for (int i = 0; i < arguments; i++) { parameterTypes[i] = args[i].getClass(); } return invokeMethod(object, methodName, args, parameterTypes); } /** * <p>Invoke a named method whose parameter type matches the object type.</p> * * <p>The behaviour of this method is less deterministic * than {@link * #invokeExactMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}. * It loops through all methods with names that match * and then executes the first it finds with compatable parameters.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a <code>Boolean</code> class * would match a <code>boolean</code> primitive.</p> * * * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @param parameterTypes match these parameters - treat null as empty array * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeMethod( Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (parameterTypes == null) { parameterTypes = emptyClassArray; } if (args == null) { args = emptyObjectArray; } Method method = getMatchingAccessibleMethod( object.getClass(), methodName, parameterTypes); if (method == null) throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + object.getClass().getName()); return method.invoke(object, args); } /** * <p>Invoke a method whose parameter type matches exactly the object * type.</p> * * <p> This is a convenient wrapper for * {@link #invokeExactMethod(Object object,String methodName,Object [] args)}. * </p> * * @param object invoke method on this object * @param methodName get method with this name * @param arg use this argument * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeExactMethod( Object object, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -