ReflectionHelper { public static Method getCompatibleMethod(Class<?> clazz, String methodName, Object... actualArguments) { Class<?>[] argumentTypes = new Class<?>[actualArguments.length]; for (int i = 0; i < actualArguments.length; i++) { argumentTypes[i] = actualArguments[i] == null ? null : actualArguments[i].getClass(); } try { return clazz.getMethod(methodName, argumentTypes); } catch (NoSuchMethodException e) { } for (Method method : clazz.getMethods()) { if (methodName.equals(method.getName())) { Class<?>[] formalParameterTypes = method.getParameterTypes(); if (argumentTypes.length == formalParameterTypes.length) { boolean parametersMatch = true; for (int i = 0; i < argumentTypes.length && parametersMatch; i++) { Class<?> argumentType = argumentTypes[i]; Class<?> formalParameterType = formalParameterTypes[i]; if (argumentType == null) { parametersMatch = !formalParameterType.isPrimitive(); } else if (argumentType.isPrimitive()) { parametersMatch = isAssignablePrimitive(argumentType, formalParameterType); } else if (formalParameterType.isPrimitive()) { parametersMatch = isAssignablePrimitive(formalParameterType, argumentType); } else { parametersMatch = formalParameterType.isAssignableFrom(argumentType); } } if (parametersMatch) { return method; } } } } return null; }  static Constructor<T> getCompatibleConstructor(Class<T>    clazz,
                                                              Class<?>... argumentTypes); static boolean isAssignablePrimitive(Class<?> clzPrimitive,
                                                Class<?> clzWrapper); static Method getCompatibleMethod(Class<?>  clazz,
                                             String    methodName,
                                             Object... actualArguments); static Object createObject(String      className,
                                      ClassLoader classLoader); static Object createObject(String      className,
                                      ClassLoader classLoader,
                                      Object...   constructorParameterList); static A getAnnotation(Class<?> onClass,
                                                         Class<A> desiredAnnotationClass);  }
@Test public void shouldLocateMethodImplementedByAbstractParentClass() { HashMap map = new HashMap(); Method method = ReflectionHelper.getCompatibleMethod(map.getClass(), "put", "key", "value"); Assert.assertThat(method, is(notNullValue())); }
@Test public void shouldLocateStaticMainMethod() { Method method = ReflectionHelper.getCompatibleMethod(ReflectionHelperMain.class, "main", (Object) new String[] {"hello"}); Assert.assertThat(method, is(notNullValue())); }
@Test public void shouldLocateVoidMethodWithZeroArguments() { Method interfaceMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperInterface.class, "voidMethodWithZeroArguments"); Assert.assertThat(interfaceMethod, is(notNullValue())); Method classMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperClass.class, "voidMethodWithZeroArguments"); Assert.assertThat(classMethod, is(notNullValue())); }
@Test public void shouldLocateVoidMethodWithOnePrimitiveArgument() { Method interfaceMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperInterface.class, "voidMethodWithOnePrimitiveArgument", 42); Assert.assertThat(interfaceMethod, is(notNullValue())); Method classMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperClass.class, "voidMethodWithOnePrimitiveArgument", 42); Assert.assertThat(classMethod, is(notNullValue())); }
@Test public void shouldLocateVoidMethodWithOneWrapperArgument() { Method interfaceMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperInterface.class, "voidMethodWithOneWrapperArgument", 42L); Assert.assertThat(interfaceMethod, is(notNullValue())); Method classMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperClass.class, "voidMethodWithOneWrapperArgument", 42L); Assert.assertThat(classMethod, is(notNullValue())); }
@Test public void shouldLocateVoidMethodWithOneStringArgument() { Method interfaceMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperInterface.class, "voidMethodWithOneStringArgument", "42"); Assert.assertThat(interfaceMethod, is(notNullValue())); Method classMethod = ReflectionHelper.getCompatibleMethod(ReflectionHelperClass.class, "voidMethodWithOneStringArgument", "42"); Assert.assertThat(classMethod, is(notNullValue())); }