Пример #1
0
  private void addClass(Class<?> c) {
    if (classes.add(c)) {
      if (c.getSuperclass() != null) {
        addClass(c.getSuperclass());
      }
      for (Class<?> sc : c.getInterfaces()) {
        addClass(sc);
      }
      for (Class<?> dc : c.getDeclaredClasses()) {
        addClass(dc);
      }
      for (Method m : c.getDeclaredMethods()) {
        addClass(m.getReturnType());
        for (Class<?> p : m.getParameterTypes()) {
          addClass(p);
        }
      }

      if (c != void.class && dimensions(c) < 2) {
        Class<?> arrayClass = Array.newInstance(c, 0).getClass();
        arrayClasses.put(c, arrayClass);
        addClass(arrayClass);
      }
    }
  }
Пример #2
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());
  }