Java Method Call invokePrivate(String name, final Object obj, Object[] objects)

Here you can find the source of invokePrivate(String name, final Object obj, Object[] objects)

Description

invoke Private

License

Apache License

Declaration

public static Object invokePrivate(String name, final Object obj,
            Object[] objects) throws IllegalArgumentException,
            IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static Object invokePrivate(String name, final Object obj,
            Object[] objects) throws IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Method method = null;/*from  w w  w .  ja v a2s . c om*/
        Class<?> objClass = obj instanceof Class ? (Class<?>) obj : obj
                .getClass();
        while (objClass != null && method == null) {
            for (Method m : objClass.getDeclaredMethods()) {
                if (name.equals(m.getName())
                        && (objects == null && m.getParameterTypes().length == 0)
                        || (objects != null && objects.length == m
                                .getParameterTypes().length)) {
                    method = m;
                    break;
                }
            }
            objClass = objClass.getSuperclass();
        }
        if (method == null) {
            throw new IllegalArgumentException("method with name " + name
                    + " was not found, check class definition.");
        }
        final boolean wasAccessible = method.isAccessible();
        try {
            method.setAccessible(true);
            return method.invoke(obj, objects);
        } finally {
            if (wasAccessible != method.isAccessible()) {
                method.setAccessible(wasAccessible);
            }
        }
    }
}

Related

  1. invokeOrBailOut(Object invokee, Method method, Object[] params)
  2. invokeParameterlessMethod(T theObject, String methodName)
  3. invokePrepare(Object instance)
  4. invokePrimivite(Object entity, Method writeMethod, Class propertyType, String propertyValue)
  5. invokePrivate(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
  6. invokePrivateMethod(Method method, Object object, Object... args)
  7. invokePrivateMethod(Object instance, String name, Object... args)
  8. invokePrivateMethod(Object obj, String methodName, Class[] parameterTypes, Object[] args)
  9. invokePrivateMethod(String methodName, Class clazz, Object object)