java 通过反射技术调用基类私用方法,可适用于任何类的私有方法调用?

/** 
     * 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。 
     *  
     * @param clazz 
     *            目标类 
     * @param methodName 
     *            方法名 
     * @param classes 
     *            方法参数类型数组 
     * @return 方法对象 
     * @throws Exception 
     */  
    public static Method getMethod(Class clazz, String methodName,  
            final Class[] classes) throws Exception {  
        Method method = null;  
        try {  
            method = clazz.getDeclaredMethod(methodName, classes);  
        } catch (NoSuchMethodException e) {  
            try {  
                method = clazz.getMethod(methodName, classes);  
            } catch (NoSuchMethodException ex) {  
                if (clazz.getSuperclass() == null) {  
                    return method;  
                } else {  
                    method = getMethod(clazz.getSuperclass(), methodName,  
                            classes);  
                }  
            }  
        }  
        return method;  
    }  
  
    /** 
     *  
     * @param obj 
     *            调整方法的对象 
     * @param methodName 
     *            方法名 
     * @param classes 
     *            参数类型数组 
     * @param objects 
     *            参数数组 
     * @return 方法的返回值 
     */  
    public static Object invoke(final Object obj, final String methodName,  
            final Class[] classes, final Object[] objects) {  
        try {  
            Method method = getMethod(obj.getClass(), methodName, classes);  
            method.setAccessible(true);// 调用private方法的关键一句话  
            return method.invoke(obj, objects);  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  

  注意:参数类型名列表,必须和方法接口严格一致,如接口中使用的是基类类型,则类型列表中,也必须使用基类类型,而不能使用其具体类型;

另外,对于double,也必须使用double.class,而不能使用Double.class.