Reflections { public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { return invokeMethodFromArgs(clazzOrInstance, method, args, false); } Reflections(ClassLoader classLoader); Reflections applyClassRenames(Map<String,String> newClassRenames); Object loadInstance(String classname, Object...argValues); Object loadInstance(String classname, Class<?>[] argTypes, Object[] argValues); Object loadInstance(String classname); Class<?> loadClass(String classname); @SuppressWarnings("unchecked") Class<? extends T> loadClass(String classname, Class<T> superType); static Class<?> loadInnerClassPossiblyInheritted(Class<?> clazz, String nestedPart); Class<?> loadInnerClassNotInheritted(String outerClassname, String innerClassname); Class<?> loadInnerClassNotInheritted(Class<?> outerClazz, String innerClassname); Constructor<?> loadConstructor(Class<?> clazz, Class<?>[] argTypes); @Deprecated static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, String className, Object...argsArray); @Deprecated static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, Class<T> clazz, Object[] argsArray, boolean setAccessible); @Deprecated static Optional<T> invokeConstructorWithArgs(Class<? extends T> clazz, Object...argsArray); @Deprecated static Optional<T> invokeConstructorWithArgs(Class<? extends T> clazz, Object[] argsArray, boolean setAccessible); @Deprecated static Optional<T> invokeConstructorWithArgs(Reflections reflections, Class<? extends T> clazz, Object[] argsArray, boolean setAccessible); static Maybe<T> invokeConstructorFromArgs(Class<? extends T> clazz, Object...argsArray); static Maybe<Object> invokeConstructorFromArgs(ClassLoader classLoader, String className, Object...argsArray); @SuppressWarnings("unchecked") static Maybe<T> invokeConstructorFromArgs(ClassLoader classLoader, Class<T> optionalSupertype, String className, Object...argsArray); static Maybe<T> invokeConstructorFromArgsUntyped(ClassLoader classLoader, String className, Object...argsArray); static Maybe<T> invokeConstructorFromArgs(ClassLoader classLoader, Class<T> clazz, Object[] argsArray, boolean setAccessible); static Maybe<T> invokeConstructorFromArgs(Class<? extends T> clazz, Object[] argsArray, boolean setAccessible); static Maybe<T> invokeConstructorFromArgsIncludingPrivate(Class<? extends T> clazz, Object ...argsArray); @SuppressWarnings("unchecked") static Maybe<T> invokeConstructorFromArgs(Reflections reflections, Class<? extends T> clazz, Object[] argsArray, boolean setAccessible); Constructor<?> loadSingleConstructor(Class<?> clazz); T loadInstance(Constructor<T> constructor, Object...argValues); Method loadMethod(Class<?> clazz, String methodName, Class<?>[] argTypes); Method loadMethod(Class<?> clazz, String methodName); Object invokeMethod(Method method, Object obj, Object... argValues); Object invokeStaticMethod(Method method, Object... argValues); Object loadStaticField(Class<?> clazz, String fieldname); Object[] loadStaticFields(Class<?> clazz, String[] fieldnamesArray, Object[] defaults); static void copyFields(T source, T target); Class<?> loadClassFromCanonicalName(String canonicalName); @Nullable URL getResource(String r); final Object updateFromNewClassLoader(Object data); ClassLoader getClassLoader(); @SuppressWarnings("unchecked") static Class<? super T> findSuperType(T impl, String typeName); static Set<Class<?>> getInterfacesIncludingClassAncestors(Class<?> clazz); static Maybe<Method> findMethodMaybe(Class<?> clazz, String name, Class<?>... parameterTypes); static Iterable<Method> findMethodsCompatible(Class<?> clazz, String name, Class<?>... parameterTypes); @Deprecated static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes); static Field findField(Class<?> clazz, String name); static Maybe<Field> findFieldMaybe(Class<?> clazz, String originalName); static Maybe<Object> getFieldValueMaybe(Object instance, String fieldName); @Nonnull static Maybe<Object> getFieldValueMaybe(Object instance, Field field); static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz); static List<Field> findFields(final Class<?> clazz, Predicate<Field> filter, Comparator<Field> fieldOrdering); static List<Method> findPublicMethodsOrderedBySuper(Class<?> clazz); static Field inferSubbestField(Field f1, Field f2); static Method inferSubbestMethod(Method m1, Method m2); static Class<?> inferSubbest(Class<?> c1, Class<?> c2); @SuppressWarnings("unchecked") static T cast(Object candidate, Class<? extends T> type); @Deprecated static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<?> args); @Deprecated static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible); static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args); static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible); static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer); static Maybe<Method> getMethodFromArgs(Object clazzOrInstance, String method, List<?> args); static Maybe<Method> getMethodFromArgs(Object clazzOrInstance, String method, List<?> args, Optional<? extends TypeCoercer> coercer); static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args); static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args, boolean setAccessible); static Object invokeMethodFromArgs(Object clazzOrInstance, Method m, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer); static boolean typesMatch(Object[] argsArray, Class<?>[] parameterTypes); static boolean typesMatch(Object[] argsArray, Class<?>[] parameterTypes, Optional<? extends TypeCoercer> coercer); static boolean typesMatchUpTo(Object[] argsArray, Class<?>[] parameterTypes, int lengthRequired); static boolean typesMatchUpTo(Object[] argsArray, Class<?>[] parameterTypes, int lengthRequired, Optional<? extends TypeCoercer> coercer); static boolean typeMatches(Object arg, Class<?> parameterType, Optional<? extends TypeCoercer> coercer); static List<Class<?>> getAllInterfaces(@Nullable Class<?> type); static boolean hasNoArgConstructor(Class<?> clazz); static boolean hasNoNonObjectFields(Class<? extends Object> clazz); @Deprecated static Optional<String> tryFindMappedName(Map<String, String> renames, String name); static Maybe<String> findMappedNameMaybe(Map<String, String> renames, String name); static String findMappedNameAndLog(Map<String, String> renames, String name); static boolean hasSpecialSerializationMethods(Class<? extends Object> type); static List<?> arrayToList(Object input); static Maybe<Method> findAccessibleMethod(Method method); static boolean trySetAccessible(Method method); static TypeToken<?>[] getGenericParameterTypeTokens(TypeToken<?> t);  }
@Test public void testInvocation() throws Exception { Method m = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(m.invoke(null, "hello", 1, 2, new int[] { 3, 4}), "hello10"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3)).get(), "hello3"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList("hello", 3, 4, 5)).get(), "hello12"); }
@Test public void testInvocationCoercingArgs() throws Exception { TypeCoercerExtensible rawCoercer = TypeCoercerExtensible.newDefault(); new CommonAdaptorTypeCoercions(rawCoercer).registerAllAdapters(); Optional<TypeCoercer> coercer = Optional.<TypeCoercer>of(rawCoercer); Method m1Short = CI1.class.getMethod("m1", String.class, int.class); Method m1Long = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(m1Short.invoke(null, "hello", 1), "hello1"); Assert.assertEquals(m1Long.invoke(null, "hello", 1, 2, new int[] { 3, 4}), "hello10"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', "2"), false, coercer).get(), "a2"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', "3", "4", "5"), false, coercer).get(), "a12"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, "m1", Arrays.<Object>asList('a', (byte)3, (byte)4, (byte)5), false, coercer).get(), "a12"); }
@Test public void testMethodInvocation() throws Exception { Method m1Short = CI1.class.getMethod("m1", String.class, int.class); Method m1Long = CI1.class.getMethod("m1", String.class, int.class, int.class, int[].class); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, m1Short, Arrays.<Object>asList("hello", 3)), "hello3"); Assert.assertEquals(Reflections.invokeMethodFromArgs(CI1.class, m1Long, Arrays.<Object>asList("hello", 3, 4, 5)), "hello12"); }