1 import net.dsmxx.PersonFk; 2 import org.apache.poi.ss.formula.functions.T; 3 4 import java.lang.annotation.Annotation; 5 import java.lang.reflect.Constructor; 6 import java.lang.reflect.Field; 7 import java.lang.reflect.Method; 8 import java.lang.reflect.Modifier; 9 10 /** 11 * Created with IDEA 12 * author:foreign 13 * Date:2019/9/29 14 * Time:20:02 15 */ 16 public class ReflectionFk { . 17 public static void main (String [] args) { 18 is // 1. Get the object of the compiler . 19 Class clazz = PersonFk. class ; 20 is System.out.println ( "Object:" + clazz); 21 is the try { 22 // 1.1 Get operation of the object 23 is Class of clazz1 the Class.forName = ( "net.dsmxx.PersonFk" ); 24 System.out.println ( "Object:" + of clazz1); 25 } the catch (a ClassNotFoundException E) { 26 is E .printStackTrace (); 27 } 28 // 2. Get the full class name (including the package name) 29 String name = clazz.getName (); 30 System.out.println ( "full class name:" + name); 31 is // 2.1 Get Type 32 String = SimpleName clazz.getSimpleName (); 33 is System.out.println ( "class name:" + SimpleName); 34 is // modifier acquired class 3 35 int modifiers = clazz.getModifiers (); 36 System.out.println ( "modifier:" + modifiers); 37 [ // 3.1 Analyzing these are public, Final, Native, State, the synchronized, transient 38 is Boolean aPublic = Modifier.isPublic(modifiers); 39 System.out.println("是否public:" + aPublic); 40 boolean aFinal = Modifier.isFinal(modifiers); 41 System.out.println("是否final:" + aFinal); 42 boolean aNative = Modifier.isNative(modifiers); 43 System.out.println("是否aNative: " + aNative); 44 boolean aStatic = Modifier.isStatic(modifiers); 45 System.out.println("是否static:" + aStatic); 46 boolean aSynchronized = Modifier.isSynchronized(modifiers); 47 System.out.println ( "whether or not the synchronized:" + Asynchronized); 48 Boolean aTransient = Modifier.isTransient (modifiers); 49 System.out.println ( "whether transient:" + aTransient); 50 // 3.2 Analyzing such method what modification 51 is method, [] = methods clazz.getMethods (); 52 is for (method, method: methods) { 53 is int modifiers1 = method.getModifiers (); 54 is Boolean aPublic1 = Modifier.isPrivate (modifiers1); 55 the System .out.println ( "method whether Private:" + aPublic1); 56 is } 57 // 4 acquires package information 58 the Package aPackage = clazz.getPackage (); 59 System.out.println ( "package name:" + aPackage); 60 // . 5 Get parent 61 is Class = the superclass clazz.getSuperclass () ; 62 is System.out.println ( "parent:" + the superclass); 63 is // . 6 acquires the current class implements the interface, not the parent class implementation includes an interface 64 class [] = the interfaces clazz.getInterfaces (); 65 for ( Inter class: the interfaces) { 66 System.out.println ( "Interface:" + Inter); 67 } 68 // 7 acquires constructor 69 the Constructor [] = Constructors clazz.getConstructors (); 70 for (CON the Constructor: Constructors) { 71 is System.out.println ( "constructor:" + CON); 72 } 73 is // . 8 Get method 74 method, [] = Methods1 clazz.getMethods (); 75 for (method, method: Methods1) { 76 System.out.println ( "method:" + method); 77 } 78 // . 9 acquires public variable 79 Field, [] = Fields clazz.getFields (); 80 for(Field, Field: Fields) { 81 System.out.println ( "Variable:" + Field); 82 } 83 // 10 acquires annotation 84 the Annotation [] = Annotations clazz.getAnnotations (); 85 for (the Annotation Annotation: Annotations) { 86 System.out.println ( "comment:" + annotation); 87 } 88 } 89 }