Example #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;
  }
Example #2
0
    Foo() {
      Logger logger = Logger.getLogger(getClass());

      Type t = TypeResolver.resolveInClass(Foo.class, Foo.class);
      logger.debug("........" + t.getClass());
      Field field = null;
      try {
        field = Foo.class.getDeclaredField("var");
      } catch (NoSuchFieldException e) {
        e.printStackTrace();
      }
      logger.debug(field.getType()); // class java.lang.String.
    }
 /**
  * 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()));
 }
 private static void test(Class<?> c) throws Exception {
   /* Every public nested class represents a test.  In each case, either
    * the class contains further nested classes, in which case we
    * call this method recursively; or it declares or inherits a
    * method called getThing() and it declares a static field
    * called "expect" which
    * is the Type of that method's return value.  The test consists
    * of checking that the value returned by
    * TypeResolver.resolveInClass is indeed this Type.
    */
   System.out.println("Test " + c);
   Class<?>[] nested = c.getClasses();
   Arrays.sort(nested, classNameComparator);
   for (Class<?> n : nested) test(n);
   final Method m;
   try {
     m = c.getMethod("getThing");
   } catch (NoSuchMethodException e) {
     if (nested.length == 0) {
       System.out.println(
           "TEST ERROR: class "
               + c.getName()
               + " has neither "
               + "nested classes nor getThing() method");
       failedCases.add(c);
     }
     return;
   }
   Object expect = null;
   try {
     Field f = c.getDeclaredField("expect");
     expect = f.get(null);
   } catch (NoSuchFieldException e) {
     Class<?> outer = c.getDeclaringClass();
     if (outer != null) {
       try {
         Field f = outer.getDeclaredField("expect" + c.getSimpleName());
         expect = f.get(null);
       } catch (NoSuchFieldException e1) {
       }
     }
   }
   if (expect == null) {
     System.out.println(
         "TEST ERROR: class " + c.getName() + " has getThing() method " + "but not expect field");
     failedCases.add(c);
     return;
   }
   Type t = m.getGenericReturnType();
   //        t = FixType.fixType(t, c);
   t = TypeResolver.resolveInClass(c, t);
   System.out.print("..." + t);
   // check expected value, and incidentally equals method defined
   // by private implementations of the various Type interfaces
   if (expect.equals(t) && t.equals(expect)) System.out.println(", as expected");
   else if ((expect.equals(t) || t.equals(expect)) && expect.toString().equals(t.toString()))
     System.out.println(", as workaround of the 8023301 bug");
   else {
     System.out.println(" BUT SHOULD BE " + expect);
     failedCases.add(c);
   }
 }