Java Reflection Method Invoke invokeMethod(Method m, Object[] o)

Here you can find the source of invokeMethod(Method m, Object[] o)

Description

attempt to invoke a Method with the given Object arguments, performing static method auto-detection and automatic array compression

License

Open Source License

Declaration

public static Object invokeMethod(Method m, Object[] o)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code


//package com.java2s;
/*/* w  ww  . j  a  v  a  2  s .  c  o m*/
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2011 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.
    
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

import java.lang.reflect.*;

public class Main {
    /** attempt to invoke a Method with the given Object arguments, performing
    static method auto-detection and automatic array compression */
    public static Object invokeMethod(Method m, Object[] o)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Object obj;
        Object[] args;
        Class[] c = m.getParameterTypes();
        int num = (o == null) ? 0 : o.length;
        int len = -1;
        int a = -1;
        if (c != null) {
            len = c.length;
            for (int i = 0; i < len; i++) {
                if (c[i].isArray())
                    a = i;
            }
        }
        if (Modifier.isStatic(m.getModifiers())) {
            // static method
            obj = null;
            if (num > 0) {
                if (a < 0) {
                    args = new Object[num];
                    System.arraycopy(o, 0, args, 0, num);
                } else {
                    // compress some of the arguments into array form
                    args = new Object[len];
                    if (a > 0)
                        System.arraycopy(o, 0, args, 0, a);
                    Object array = Array.newInstance(c[a].getComponentType(), num - len + 1);
                    System.arraycopy(o, a, array, 0, num - len + 1);
                    args[a] = array;
                    if (a < len - 1)
                        System.arraycopy(o, num - len + a + 1, args, a + 1, len - a - 1);
                }
            } else
                args = null;
        } else {
            // object method
            if (num > 0)
                obj = o[0];
            else {
                // invalid object method
                return null;
            }
            if (num > 1) {
                if (a < 0) {
                    args = new Object[num - 1];
                    System.arraycopy(o, 1, args, 0, num - 1);
                } else {
                    // compress some of the arguments into array form
                    args = new Object[len];
                    if (a > 0)
                        System.arraycopy(o, 1, args, 0, a);
                    Object array = Array.newInstance(c[a].getComponentType(), num - len);
                    System.arraycopy(o, a + 1, array, 0, num - len);
                    args[a + 1] = array;
                    if (a < len - 1)
                        System.arraycopy(o, num - len + a + 1, args, a + 1, len - a - 1);
                }
            } else
                args = null;
        }
        return m.invoke(obj, args);
    }
}

Related

  1. invokeMethod(final Object object, final String methodName, final Class[] paramTypes, final Object[] parameters)
  2. invokeMethod(final Object p, final String methodName)
  3. invokeMethod(java.lang.Object toObj, String tcMethodName, Class toResultClass)
  4. invokeMethod(Method inMethod, Object inObject, Object[] inArgs)
  5. invokeMethod(Method m, Object instance, Object[] args)
  6. invokeMethod(Method meth, String str)
  7. invokeMethod(Method method, Class beanClass, Object element)
  8. invokeMethod(Method method, Object bean, Object[] values)
  9. invokeMethod(Method method, Object instance, Object... parameters)