예제 #1
0
 @Test
 public void getAllDeclaredMethods() throws Exception {
   class Foo {
     @Override
     public String toString() {
       return super.toString();
     }
   }
   int toStringMethodCount = 0;
   for (Method method : ReflectionUtils.getAllDeclaredMethods(Foo.class)) {
     if (method.getName().equals("toString")) {
       toStringMethodCount++;
     }
   }
   assertThat(toStringMethodCount, is(2));
 }
예제 #2
0
  /**
   * Find a matching method with the specified name for the specified arguments.
   *
   * @return a matching method, or {@code null} if none
   * @see #getTargetClass()
   * @see #getTargetMethod()
   * @see #getArguments()
   */
  protected Method findMatchingMethod() {
    String targetMethod = getTargetMethod();
    Object[] arguments = getArguments();
    int argCount = arguments.length;

    Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
    int minTypeDiffWeight = Integer.MAX_VALUE;
    Method matchingMethod = null;

    for (Method candidate : candidates) {
      if (candidate.getName().equals(targetMethod)) {
        Class<?>[] paramTypes = candidate.getParameterTypes();
        if (paramTypes.length == argCount) {
          int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
          if (typeDiffWeight < minTypeDiffWeight) {
            minTypeDiffWeight = typeDiffWeight;
            matchingMethod = candidate;
          }
        }
      }
    }

    return matchingMethod;
  }