Java Reflection class get all fields including the inherited fields

Description

Java Reflection class get all fields including the inherited fields

import java.lang.reflect.Field;

public class Main {
   public static void main(String args[]) throws Exception {
      print(Class.forName("javax.swing.JLabel"));
   }//  w  w  w.ja  v a 2 s .c  o m

   static int v = 0;

   static void print(Class c) {
      if (c != null) {
         print(c.getSuperclass());
         Field[] fields = c.getDeclaredFields();
         indent(v);
         System.out.println("Class: " + c);
         for (Field f : fields) {
            indent(v);
            System.out.println(f);
         }
         v++;
      }
   }

   static void indent(int n) {
      for (int i = 0; i < n; i++)
         System.out.print("  ");
   }
}



PreviousNext

Related