Exemple #1
0
  private static Method findMethod(
      Class<?> start, String methodName, int numberOfArguments, Class<?> arguments[]) {
    for (Class<?> clazz = start; clazz != null; clazz = clazz.getSuperclass()) {
      Method methods[] = clazz.getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];

        if (method == null || !Modifier.isPublic(method.getModifiers())) {
          continue;
        }

        if (method.getName().equals(methodName)) {
          Type[] parameters = method.getGenericParameterTypes();
          if (parameters.length == numberOfArguments) {
            if (arguments != null) {
              boolean differentParameterType = false;
              if (numberOfArguments > 0) {
                for (int j = 0; j < numberOfArguments; j++) {
                  if (TypeResolver.erase(TypeResolver.resolveInClass(start, parameters[j]))
                      != arguments[j]) {
                    differentParameterType = true;
                    continue;
                  }
                }

                if (differentParameterType) {
                  continue;
                }
              }
            }

            return method;
          }
        }
      }
    }

    Class interfaces[] = start.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
      Method method = findMethod(interfaces[i], methodName, numberOfArguments, null);
      if (method != null) {
        return method;
      }
    }

    return null;
  }
 /**
  * Resolves the parameter types of the method.
  *
  * @param base the class that contains the method in the hierarchy
  * @param method the object that represents the method
  * @return an array of classes identifying the parameter types of the method
  * @see Method#getGenericParameterTypes
  * @see Method#getParameterTypes
  */
 static Class<?>[] getParameterTypes(Class<?> base, Method method) {
   if (base == null) {
     base = method.getDeclaringClass();
   }
   return TypeResolver.erase(TypeResolver.resolveInClass(base, method.getGenericParameterTypes()));
 }
 /**
  * Resolves the return type of the method.
  *
  * @param base the class that contains the method in the hierarchy
  * @param method the object that represents the method
  * @return a class identifying the return type of the method
  * @see Method#getGenericReturnType
  * @see Method#getReturnType
  */
 static Class<?> getReturnType(Class<?> base, Method method) {
   if (base == null) {
     base = method.getDeclaringClass();
   }
   return TypeResolver.erase(TypeResolver.resolveInClass(base, method.getGenericReturnType()));
 }