Exemplo n.º 1
0
  public static void main(String[] args) throws Exception {
    Class<ClassTest> clazz = ClassTest.class;

    System.out.println("=============================================");
    Constructor[] ctors = clazz.getDeclaredConstructors();
    System.out.println("classTest的全部构造器如下:");
    for (Constructor c : ctors) {
      System.out.println(c);
    }

    System.out.println("=============================================");
    Constructor[] publicCtors = clazz.getConstructors();
    System.out.println("ClassTest的全部public构造器如下:");
    for (Constructor c : publicCtors) {
      System.out.println(c);
    }

    System.out.println("=============================================");
    Method[] mtds = clazz.getMethods();
    System.out.println("ClassTest的全部public方法如下:");
    for (Method md : mtds) {
      System.out.println(md);
    }

    System.out.println("=============================================");
    System.out.println("ClassTest带一个字符串参数的info方法为:" + clazz.getMethod("info", String.class));

    Annotation[] ans = clazz.getAnnotations();
    System.out.println("ClassTest的全部annotation为:");
    for (Annotation an : ans) {
      System.out.println(an);
    }
    System.out.println("该元素上的@SuppressWarnings注释为:" + clazz.getAnnotation(SuppressWarnings.class));

    System.out.println("=============================================");
    Class<?>[] inners = clazz.getDeclaredClasses();
    System.out.println("ClassTest的全部内部类如下:");
    for (Class c : inners) {
      System.out.println(c);
    }

    System.out.println("=============================================");

    Class inClazz = Class.forName("ClassTest$Inner");
    System.out.println("inClazz对应的外部类为:" + inClazz.getDeclaringClass());
    System.out.println("ClassTest的包为:" + clazz.getPackage());
    System.out.println("ClassTest的父类:" + clazz.getSuperclass());
  }
Exemplo n.º 2
0
 public static <T extends Annotation> T digAnnotation(
     Class<?> target, Class<T> annotationType, List<Class<? extends Annotation>> visited) {
   T result = target.getAnnotation(annotationType);
   if (result == null) {
     for (Annotation a : target.getAnnotations()) {
       if (!visited.contains(a.annotationType())) {
         visited.add(a.annotationType());
         result = digAnnotation(a.annotationType(), annotationType, visited);
         if (result != null) {
           return result;
         }
       }
     }
   }
   return result;
 }
  protected void loadClassDependencies(Class aClass) throws ClassNotFoundException {
    String name = aClass.getName();
    if (myVisited.add(aClass)) {
      try {
        for (Method method : aClass.getDeclaredMethods()) {
          loadTypeDependencies(method.getGenericReturnType());
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }
        for (Constructor method : aClass.getDeclaredConstructors()) {
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }

        for (Field field : aClass.getDeclaredFields()) {
          loadTypeDependencies(field.getGenericType());
        }

        Type superclass = aClass.getGenericSuperclass();
        if (superclass != null) {
          loadClassDependencies(aClass);
        }

        for (Type intf : aClass.getGenericInterfaces()) {
          loadTypeDependencies(intf);
        }

        aClass.getAnnotations();
        Package aPackage = aClass.getPackage();
        if (aPackage != null) {
          aPackage.getAnnotations();
        }
      } catch (LinkageError e) {
        throw new ClassNotFoundException(name);
      } catch (TypeNotPresentException e) {
        throw new ClassNotFoundException(name);
      }
    }
  }