/** * 反射调用对象方法, 无视private/protected修饰符. * * 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况 * * 性能较差,仅用于单次调用. */ public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }
/** * <p>Invokes a method with no parameters.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class,String,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 * @return The value returned by the invoked method * * @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(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactMethod(object, methodName, args, parameterTypes); }
/** * <p>Invokes a {@code static} method whose parameter types match exactly the object * types.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.</p> * * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @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 invokeExactStaticMethod(final Class<?> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactStaticMethod(cls, methodName, args, parameterTypes); }
/** * <p>Returns a new instance of the specified class inferring the right constructor * from the types of the arguments.</p> * * <p>This locates and calls a constructor. * The constructor signature must match the argument types by assignment compatibility.</p> * * @param <T> the type to be constructed * @param cls the class to be constructed, not {@code null} * @param args the array of arguments, {@code null} treated as empty * @return new instance of {@code cls}, not {@code null} * * @throws NullPointerException if {@code cls} is {@code null} * @throws NoSuchMethodException if a matching constructor cannot be found * @throws IllegalAccessException if invocation is not permitted by security * @throws InvocationTargetException if an error occurs on invocation * @throws InstantiationException if an error occurs on instantiation * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ public static <T> T invokeConstructor(final Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { args = ArrayUtils.nullToEmpty(args); final Class<?> parameterTypes[] = ClassUtils.toClass(args); return invokeConstructor(cls, args, parameterTypes); }
/** * <p>Returns a new instance of the specified class inferring the right constructor * from the types of the arguments.</p> * * <p>This locates and calls a constructor. * The constructor signature must match the argument types exactly.</p> * * @param <T> the type to be constructed * @param cls the class to be constructed, not {@code null} * @param args the array of arguments, {@code null} treated as empty * @return new instance of {@code cls}, not {@code null} * * @throws NullPointerException if {@code cls} is {@code null} * @throws NoSuchMethodException if a matching constructor cannot be found * @throws IllegalAccessException if invocation is not permitted by security * @throws InvocationTargetException if an error occurs on invocation * @throws InstantiationException if an error occurs on instantiation * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ public static <T> T invokeExactConstructor(final Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { args = ArrayUtils.nullToEmpty(args); final Class<?> parameterTypes[] = ClassUtils.toClass(args); return invokeExactConstructor(cls, args, parameterTypes); }
/** * <p>Invokes a named method whose parameter type matches the object type.</p> * * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@code Boolean} object * would match a {@code boolean} primitive.</p> * * <p>This is a convenient wrapper for * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}. * </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 * @return The value returned by the invoked method * * @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(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeMethod(object, methodName, args, parameterTypes); }
/** * <p>Invokes a named method whose parameter type matches the object type.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@code Boolean} object * would match a {@code boolean} primitive.</p> * * <p>This is a convenient wrapper for * {@link #invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)}. * </p> * * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @return The value returned by the invoked method * * @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 * * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeMethod(object, forceAccess, methodName, args, parameterTypes); }
IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeStaticMethod(cls, methodName, args, parameterTypes);
@Test public void testToClass_object() { // assertNull(ClassUtils.toClass(null)); // generates warning assertNull(ClassUtils.toClass((Object[]) null)); // equivalent explicit cast // Additional varargs tests assertTrue("empty -> empty", Arrays.equals(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass())); final Class<?>[] castNull = ClassUtils.toClass((Object) null); // == new Object[]{null} assertTrue("(Object)null -> [null]", Arrays.equals(new Object[]{null}, castNull)); assertSame(ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.toClass(ArrayUtils.EMPTY_OBJECT_ARRAY)); assertTrue(Arrays.equals(new Class[] { String.class, Integer.class, Double.class }, ClassUtils.toClass("Test", Integer.valueOf(1), Double.valueOf(99d)))); assertTrue(Arrays.equals(new Class[] { String.class, null, Double.class }, ClassUtils.toClass("Test", null, Double.valueOf(99d)))); }
/** * 直接调用对象方法, 无视private/protected修饰符. * * 根据传入参数的实际类型进行匹配 */ public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return (T) invokeMethod(obj, methodName, theArgs, parameterTypes); }
/** * 直接调用对象方法, 无视private/protected修饰符. * * 根据传入参数的实际类型进行匹配 */ public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return (T) invokeMethod(obj, methodName, theArgs, parameterTypes); }
/** * 反射调用对象方法, 无视private/protected修饰符. * * 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况 * * 性能较差,仅用于单次调用. */ public static <T> T invokeMethod(Object obj, String methodName, Object... args) { Object[] theArgs = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs); return invokeMethod(obj, methodName, theArgs, parameterTypes); }
/** * <p>Invokes a method with no parameters.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class,String,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 * @return The value returned by the invoked method * * @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(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactMethod(object, methodName, args, parameterTypes); }
/** * <p>Invokes a method with no parameters.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class,String,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 * @return The value returned by the invoked method * * @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(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactMethod(object, methodName, args, parameterTypes); }
/** * <p>Returns a new instance of the specified class inferring the right constructor * from the types of the arguments.</p> * * <p>This locates and calls a constructor. * The constructor signature must match the argument types exactly.</p> * * @param <T> the type to be constructed * @param cls the class to be constructed, not {@code null} * @param args the array of arguments, {@code null} treated as empty * @return new instance of {@code cls}, not {@code null} * * @throws NullPointerException if {@code cls} is {@code null} * @throws NoSuchMethodException if a matching constructor cannot be found * @throws IllegalAccessException if invocation is not permitted by security * @throws InvocationTargetException if an error occurs on invocation * @throws InstantiationException if an error occurs on instantiation * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ public static <T> T invokeExactConstructor(final Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { args = ArrayUtils.nullToEmpty(args); final Class<?> parameterTypes[] = ClassUtils.toClass(args); return invokeExactConstructor(cls, args, parameterTypes); }
/** * <p>Returns a new instance of the specified class inferring the right constructor * from the types of the arguments.</p> * * <p>This locates and calls a constructor. * The constructor signature must match the argument types by assignment compatibility.</p> * * @param <T> the type to be constructed * @param cls the class to be constructed, not {@code null} * @param args the array of arguments, {@code null} treated as empty * @return new instance of {@code cls}, not {@code null} * * @throws NullPointerException if {@code cls} is {@code null} * @throws NoSuchMethodException if a matching constructor cannot be found * @throws IllegalAccessException if invocation is not permitted by security * @throws InvocationTargetException if an error occurs on invocation * @throws InstantiationException if an error occurs on instantiation * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ public static <T> T invokeConstructor(final Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { args = ArrayUtils.nullToEmpty(args); final Class<?> parameterTypes[] = ClassUtils.toClass(args); return invokeConstructor(cls, args, parameterTypes); }
/** * <p>Invokes a method with no parameters.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class,String,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 * @return The value returned by the invoked method * * @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(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactMethod(object, methodName, args, parameterTypes); }
/** * <p>Invokes a {@code static} method whose parameter types match exactly the object * types.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.</p> * * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @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 invokeExactStaticMethod(final Class<?> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactStaticMethod(cls, methodName, args, parameterTypes); }
/** * <p>Invokes a {@code static} method whose parameter types match exactly the object * types.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.</p> * * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @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 invokeExactStaticMethod(final Class<?> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactStaticMethod(cls, methodName, args, parameterTypes); }
/** * <p>Invokes a {@code static} method whose parameter types match exactly the object * types.</p> * * <p>This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.</p> * * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @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 invokeExactStaticMethod(final Class<?> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeExactStaticMethod(cls, methodName, args, parameterTypes); }