Public class Lovereflect { publicstaticclass Demo implements serializable{ } publicstaticvoid main (string[] arg) { New Demo (); System.out.println (Demo.getclass (). GetName ());} }
Output
Lovereflect$demo
ERROR:JDWP unable to get JNI 1.2 environment, JVM->GETENV () Return code = 2
JDWP Exit Error Agent_error_no_jni_env (183): [.. /.. /.. /SRC/SHARE/BACK/UTIL.C:820]
Importjava.io.Serializable;ImportJava.lang.reflect.Array;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Modifier;ImportJava.util.HashMap;ImportJava.util.Map; Public classLovereflect { Public Static classSubclassImplementsSerializable {Private intID; PrivateString name; Publicsubclass () {} PublicSubclass (Integer ID, String name) { This. ID =ID; This. Name =name; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public voidSubmethod () {System.out.println ("Hello World"); } } Public Static voidMain (string[] arg) {Subclass Subclass=Newsubclass (); //"Case 1" obtains the class declaration by name, then obtains the nameSystem.out.println (Subclass.getclass (). GetName ()); Class<?>Demo1; Try { //"Case 2" obtains the declaration of the class by nameDemo1 = Class.forName ("Lovereflect$subclass"); System.out.println (Demo1.getname ()); //Case 3 finds the declaration of the class by name, and then creates the instanceSubclass SubClass2 = (subclass) class.forname ("Lovereflect$subclass"). newinstance (); Subclass2.setid (1); Subclass2.setname ("AAAA"); System.out.println (Subclass2.getname ()); //"Case 4" takes the constructor of the class new instanceClass<?>[] Parametertypes = {Integer.class, String.class}; Constructor Constructor= Class.forName ("Lovereflect$subclass"). Getdeclaredconstructor (Parametertypes); Subclass SUBCLASS3= (subclass) constructor.newinstance (2, "222222222222222222"); System.out.println (Subclass3.getname ()); //change the value of a propertyField f = Subclass3.getclass (). Getdeclaredfield ("name"); Class<?> type =F.gettype (); System.out.println ("Field type=" +type.getname ()); intMoD =f.getmodifiers (); System.out.println ("Modify=" +modifier.tostring (mod)); F.setaccessible (true); F.set (SUBCLASS3,"BBBBBBBBBBBBBBB"); System.out.println (Subclass3.getname ()); Map<Integer,Method> methods =NewHashmap<integer,method>(); Class<?>[] ParameterTypes2 = {}; Method Method= Class.forName ("Lovereflect$subclass"). Getdeclaredmethod ("Submethod"), ParameterTypes2); //using example to adjust the methodMethod.invoke (SUBCLASS3,NULL); //working with arrays by reflectionSubclass[] Subclass_ = {NewSubclass (1, "BBBBBBBBBBBBBBB"), subclass, SubClass2, SUBCLASS3}; Class<?> arraytype =Subclass_.getclass (). Getcomponenttype (); System.out.println ("Array type=" +arraytype.getname ()); System.out.println ("Array Leangth=" +array.getlength (Subclass_)); System.out.println ("First one=" + ((subclass) Array.get (Subclass_, 0) . GetName ()); Array.set (Subclass_,0, SUBCLASS2); System.out.println ("First one=" + ((subclass) Array.get (Subclass_, 0) . GetName ()); //to modify the size of an arraySubclass[] newtemp= (subclass[]) arrayinc (subclass_,15); System.out.println ("Array Leangth=" +array.getlength (newtemp)); Print (newtemp); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(instantiationexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SecurityException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchmethodexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(InvocationTargetException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchfieldexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** Modify Array size **/ Public StaticObject Arrayinc (Object obj,intLen) {Class<?>arr=Obj.getclass (). Getcomponenttype (); Object NEWARR=array.newinstance (arr, Len); intco=array.getlength (obj); System.arraycopy (obj,0, NEWARR, 0, CO); returnNEWARR; } /*** Print **/ Public Static voidprint (Object obj) {Class<?>c=Obj.getclass (); if(!C.isarray ()) { return; } System.out.println ("Array length is:" +array.getlength (obj)); for(inti = 0; I < array.getlength (obj); i++) {System.out.print (Array.get (obj, i)+" "); } }}
Java Reflection brush up