import java.lang.reflect.constructor;import java.lang.reflect.field;import java.lang.reflect.method;import java.lang.reflect.modifier;public class reflectiondemo { Public static void main (String[] args) throws classnotfoundexception { String classnamestr = "java.lang.String";//string classnamestr = "java.lang.Double"; Class<?> cls =class.forname (CLASSNAMESTR); Class<?> supercls =cls.getsuperclass (); String modifier =modifier.tostring (Cls.getmodifiers ());if (Modifier.length () >0) {// Output class modifier word System.out.print (modifier+ "");} Output class name System.out.print ("class " +classnamestr);//Output parent class if (supercls != null && supercls != object.class) {system.out.print (" extends " +supercls.getname ());} System.out.print ("\n{\n");p rintconstrctors (CLS); System.out.println ();p rintmethods (CLS); System.out.println ();p Rintfields (CLS); System.out.println ("}");} /** * Output Properties * @param cls */private static void printfields (Class <?>&NBSP;CLS) {field[] fields = cls.getdeclaredfields ();for (Field f : fields) {class<?> ftype = f.gettype (); String fname = f.getname (); System.out.print (" "); String modifiers = modifier.tostring (F.getmodifiers ());if (Modifiers.length () >0) { System.out.print (modifiers+ " ");} System.out.println (Ftype.getname () + " " +fname+ ";");}} /** * Output Method * @param cls */private static void printmethods (Class <?>&NBSP;CLS) {method[] methods = cls.getdeclaredmethods (); for (Method m : methods) {class<?> rettype = m.getreturntype (); String methodname = m.getname (); System.out.print (" "); String&nbSp;modifiers = modifier.tostring (M.getmodifiers ()); if (Modifiers.length () >0) {System.out.print ( modifiers+ " ");} System.out.print (rettype+ " " +methodname+ "("); Class<?>[] paramtypes = m.getparametertypes ();for (int i = 0; i < paramtypes.length; i++) {if (i>0) {system.out.print (", ");} System.out.print (Paramtypes[i].getname ());} System.out.println (");");}} /** * output Constructors * @param cls */private static void printconstrctors ( CLASS<?>&NBSP;CLS) {constructor<?>[] constructors =cls.getdeclaredconstructors (); for (constructor<?> c : constructors) {string name = c.getname () ; System.out.print (" "); String modifiers =modifier.tostring (C.getmodifiers ());if (Modifiers.length () >0) { System.out.print (modifiers + " ");} System.out.print (NAMe+ "("); Class<?>[] paramtypes = c.getparametertypes ();for (int i = 0; i < paramtypes.length; i++) {if (i>0) {system.out.print (", ");} System.out.print (Paramtypes[i].getname ());} System.out.println (")");}}
All information for the Java reflection output class