1. How to get the Reflection class class
Class.forName ("Full class name");
The class name. class//needs to be loaded by the JVM
Object. GetClass
2.java four ways to operate the JavaBean
public class Descriptor {public static void main (string[] args) throws Exception {//mode 1 PropertyDescriptor descriptor = new PropertyDescriptor ("X", Testdemo1.class); Method Readmethod = Descriptor.getreadmethod (); TestDemo1 testDemo1 = new TestDemo1 (); Object invoke = Readmethod.invoke (TESTDEMO1); if (Invoke instanceof String) {System.out.println (string) invoke); }//Mode 2 This method can only get all the methods BeanInfo BeanInfo = Introspector.getbeaninfo (Testdemo1.class); propertydescriptor[] descriptors = beaninfo.getpropertydescriptors (); for (PropertyDescriptor propertydescriptor:descriptors) {if (Propertydescriptor.getname (). Equals ( "X")) {System.out.println (Propertydescriptor.getname ()); }}//Mode 3 Class TestClass = Testdemo1.class; Object newinstance = Testclass.newinstance (); if (newinstance instanceof TestDemo1) {System.out.println (((TESTDEMO1) newinstance). GetX ()); }//Mode 4 Class TestClass1 = Class.forName ("Cn.reflect.collection.TestDemo1"); Field Declaredfield = Testclass1.getdeclaredfield ("x"); if (!declaredfield.isaccessible ()) {//is similar to and is judged on whether the property is public//if (! Modifier.ispublic (Declaredfield.getmodifiers ())) {declaredfield.setaccessible (true); } System.out.println (Declaredfield.get (TESTDEMO1)); }}class TestDemo1 {private String x = "Qwe"; Public String GetX () {return x; public void SetX (String x) {this.x = x; }}
3. Capturing annotations with Reflection
Retentionpolicy Enumeration Retentionpolicy.runtime run time, must use runtime to be able to reflect @retention (retentionpolicy.runtime)// Meta annotations//ElementType enumeration Elementtype.type decorated with classes, interfaces, enumerations, annotations @target (elementtype.type) @interface ReflectAnnotationdemo1 { Value can omit value=, omitted if only String value ();} @ReflectAnnotationdemo1 ("111") class AnnotationDemo1 {} public class Annotationdemo {public static void main (String [] args) { AnnotationDemo1 annotationDemo1 = new AnnotationDemo1 (); class<? Extends annotationdemo1> class1 = Annotationdemo1.getclass (); if (Class1.isannotationpresent (Reflectannotationdemo1.class)) { ReflectAnnotationdemo1 annotation = Class1.getannotation (reflectannotationdemo1.class); System.out.println (Annotation.value ());}}}
4. Reflection on Generics
public class Reflectgenericity {private static final String Parameterizedtype = null;public static void Main (string[] args ) throws Nosuchmethodexception, SecurityException {//Gets the parameterized type of the parent class. Genericsuperclass = ReflectGenericityTest1.class.getGenericSuperclass (); if (Genericsuperclass instanceof Parameterizedtype) {type[] Actualtypearguments = ((Parameterizedtype) genericsuperclass). Getactualtypearguments (); System.out.println (Actualtypearguments[0]);} Get Type method[] methods = ReflectGenericityTest.class.getMethods (); for (int i = 0; i < methods.length; i++) {if (methods[ I].getname (). Equals ("Test1")) {parameter[] parameters = Methods[i].getparameters (); System.out.println (Parameters[0]); break;}} Gets the parameterized type of method = ReflectGenericityTest.class.getMethod ("Test2", Vector.class); type[] Genericparametertypes = Method.getgenericparametertypes (); System.out.println (Genericparametertypes[0]);}} Class Reflectgenericitytest<t> {public static <E> e test1 (e e) {return E;} public static VoiD test2 (vector<string> e) {}}class reflectgenericitytest1<t> extends reflectgenericitytest<t> {}
5. Reflected Tool class
public class Reflectutils {public static class Getsuperclass (class clazz, int index) {Type Gentype = Clazz.getgenericsuper Class (); if (! ( Gentype instanceof Parameterizedtype) {return object.class;} Type[] params = ((Parameterizedtype) gentype). Getactualtypearguments (); if (index >= params.length | | Index < 0) {RET Urn Object.class;} if (! ( Params[index] instanceof Class) {return object.class;} Return (Class) Params[index];} public static <T> class<t> Getsupergtype (Class clazz) {return Getsuperclass (clazz, 0);} public static Method Getdecaredmethod (Object obj, String methoname, class<?>[] paraterty) {for (Class clazz = obj.ge Tclass (); Clazz! = Object.class; Clazz.getsuperclass ()) {try {return Clazz.getdeclaredmethod (Methoname, paraterty);} catch (Exception e) { E.printstacktrace ();}} return null;} public static void makeaccsible (Field field) {//Getmodifys gets the various permissions of the overlay integer if (modifier.ispublic (Field.getmodifiers ())) { Field.setaccessible (True);}} public static Field GetdecaredmethOD (Object obj, String fieldname) {for (Class clazz = Obj.getclass (); Clazz! = Object.class; Clazz.getsuperclass ()) {try {R Eturn Clazz.getdeclaredfield (fieldname);} catch (Exception e) {e.printstacktrace ();}} arraylist<object> ArrayList = new arraylist<string> (); return null;} public static object InvokeMethod (Object obj, String methoname, class<?>[] paraterty, object[] parame) {Method Metho D = Getdecaredmethod (obj, Methoname, paraterty); if (method = = null) {throw new Illegalaccesserror ();} if (Modifier.ispublic (Method.getmodifiers ())) {method.setaccessible (true);} try {method.invoke (obj, parame),} catch (Exception e) {e.printstacktrace ();} return null;} public static void SetFieldValue (Object obj, String fieldname, Object param) {Field field = Getdecaredmethod (obj, Fieldnam e); if (field = = null) {throw new Illegalaccesserror ();} makeaccsible (field); try {field.set (obj, param);} catch (Illegalaccessexception e) {e.printstacktrace ();}} public static object Geyfieldvalue (objectObject, String name) {Field field = Getdecaredmethod (object, name); if (Field = = null) {throw new IllegalArgumentException ( "Not find field!");} try {Field.get (object),} catch (Illegalaccessexception e) {e.printstacktrace ();} return null;}}
Getting Started with the reflection basics of Java