Mega Code Archive

 
Categories / Java / Reflection
 

Using Reflection to browse a java class

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main {   public static void main(String argv[]) throws Exception{     String className = "String";     Class c = Class.forName(className);     Constructor cst[] = c.getDeclaredConstructors();     // get fields from the Class object     Field f[] = c.getDeclaredFields();     // get methods from the Class object     Method m[] = c.getDeclaredMethods();     // filling the constructors list box     for (int i = 0; i < cst.length; i++) {       System.out.println(cst[i].getName());     }     // filling the fields list box     for (int i = 0; i < f.length; i++) {       System.out.println(f[i].getName());     }     // filling the methods list box     for (int i = 0; i < m.length; i++) {       System.out.println(m[i].getName());     }   } }