For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class which provides methods to examine the runtime properties of the object including its members and type information. Class also provides the ability to create new classes and objects. Most importantly, it is the entry point for all of the Reflection APIs.
} catch (ClassNotFoundException e) { e.printStackTrace(); } // 3、通过实例对象的getClass方法 ClasspeopleClazz3=newPeople().getClass(); // 这个是String实例对象的getClass方法 ClassstringClazz3="HellString".getClass(); byte[] bytes = newbyte[1024]; ClassbytesClazz3= bytes.getClass(); //Returns the Class corresponding to the enumeration type E. ClassenumObjValue= EnumObj.A.getClass();
Set<String> hashSet3 = newHashSet<String>(); //Returns the Class corresponding to java.util.HashSet. ClasshashSetClazz3= hashSet3.getClass(); }
Class : java.util.HashMap Class Modifiers : public Parameters : K V Implemented Interfaces : java.util.Map<K, V> interface java.lang.Cloneable interface java.io.Serializable Inheritance Path : java.util.AbstractMap java.lang.Object -- No Annotations --%n%n
++Reflection defines an interface java.lang.reflect.Member which is implemented by java.lang.reflect.Field, java.lang.reflect.Method, and java.lang.reflect.Constructor .++
private static void testSetFiled() { People people = new People(10001, "王大明", "王小明", 18);
System.out.println(people.toString());
Class peopleClazz = people.getClass();
try { Field publicFiledName = peopleClazz.getField("name"); //注意获取private变量时,需要用getDeclaredField Field privateFiledAge = peopleClazz.getDeclaredField("age");
//反射获取名字, 年龄 String name = (String) publicFiledName.get(people);
// 这个地方要 // 变量是private,Java运行时会进行访问权限检查,private类型的变量无法进行直接访问, // 刚刚进行的反射操作并没有打破这种封装,所以我们依然没有权限对private属性进行直接访问。 // 如果不加的话,会报下面的异常: //java.lang.IllegalAccessException: class com.frewen.reflect.ReflectTest // cannot access a member of class com.frewen.reflect.People with modifiers "private" //at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361) //at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591) // at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075) // at java.base/java.lang.reflect.Field.getInt(Field.java:592) // at com.frewen.reflect.ReflectTest.testSetFiled(ReflectTest.java:44) // at com.frewen.reflect.ReflectTest.main(ReflectTest.java:21) privateFiledAge.setAccessible(true); int age = privateFiledAge.getInt(people); System.out.println("反射获取:People name:" + name + ",age:" + age);
People constructor called with id = 10001,name = 王大明,oldName = 王小明,name = 18 People id:10001,name:王大明,oldName:王小明,age:18 反射获取:People name:王大明,age:18 反射设置名字之后:People name:王大明反射名字,age:23
++The java.lang.reflect.Method class provides APIs to access information about a method’s modifiers, return type, parameters, annotations, and thrown exceptions. It also be used to invoke methods.++
//调用无参方法 Method toString = peopleClass.getDeclaredMethod("toString"); toString.invoke(people); //调用定项参数方法 Method setAge = peopleClass.getDeclaredMethod("setAge", int.class); setAge.invoke(people, 23); // 这个地方注意一下: 注意:如果方法是private的,可以使用method.setAccessible(true)方法绕过权限检查 // 否则,会报异常: // java.lang.IllegalAccessException: class com.frewen.reflect.ReflectTest // cannot access a member of class com.frewen.reflect.People with modifiers "private" // at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361) // at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591) // at java.base/java.lang.reflect.Method.invoke(Method.java:558) // at com.frewen.reflect.ReflectTest.testMethod(ReflectTest.java:44) // at com.frewen.reflect.ReflectTest.main(ReflectTest.java:23) Method getAge = peopleClass.getDeclaredMethod("getAge"); getAge.setAccessible(true); System.out.println(getAge.invoke(people));