java.lang.reflect.Field
API解释:Field 提供有关类或接口的单个字段的信息,以及对它的动态访问权限。反射的字段可能是一个类(静态)字段或实例字段。
它的使用是从反射开始的,通常是通过Class对象来获取Field实例:
Class.getFields(), Class.getField(String), Class.getDeclaredFields(), Class.getDeclaredField(String)
方法说明:
Field getDeclaredField(String name) 返回具有给定名称的字段的Field对象,该对象在此Class表示的类中声明,注意此返回对象只是声明,
要获取对应值需要调用 field.get(对象),包括private
Field[] getDeclaredFields() 返回一个数组,该数组包含由此类表示的类中声明的所有字段的Field对象,包括private
Field getField(String name) 返回指定name的属性值,此属性值必须是public范围
Field[] getFields() 返回该类中声明的所有字段的Field对象(仅public)
关于Field的常用方法:
boolean equals(Object obj) 将此 Field 与指定对象比较
Object get(Object obj) 返回指定对象上此 Field 表示的字段的值
<T extends Annotation> getAnnotation(Class<T> annotationClass) 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null
boolean getBoolean(Object obj) 获取一个静态或实例 boolean 字段的值
//相似方法
//getByte(Object obj)
//getChar(Object obj)
//getDouble(Object obj)
//getFloat(Object obj)
//getInt(Object obj)
//getLong(Object obj)
//getShort(Object obj)
Annotation[] getDeclaredAnnotations() 返回直接存在于此元素上的所有注释
int getModifiers() 以整数形式返回由此 Field 对象表示的字段的 Java 语言修饰符
String getName() 返回此 Field 对象表示的字段的名称
boolean isEnumConstant() 如果此字段表示枚举类型的元素,则返回 true;否则返回 false
void set(Object obj, Object value) 将指定对象变量上此 Field 对象表示的字段设置为指定的新值
//相似的方法
//setByte(Object obj, byte b)
//setChar(Object obj, char c)
//setDouble(Object obj, double d)
//setFloat(Object obj, float f)
//setInt(Object obj, int i)
//setLong(Object obj, long l)
//setShort(Object obj, short s)
String toGenericString() 返回一个描述此 Field(包括其一般类型)的字符串 eg:public boolean UserTest.sex
还有从类 java.lang.reflect.AccessibleObject 继承的几个方法:
getAnnotations //返回当前这个元素上的所有注释
isAccessible //值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查
isAnnotationPresent //用来判断指定注释类型是存在于此元素上
setAccessible //当Field是private的,要先setAccessible(true)
java.lang.reflect.Method
API解释: Method 提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。所反映的方法可能是类方法或实例方法(包括抽象方法)。
Method 允许在匹配要调用的实参与底层方法的形参时进行扩展转换;但如果要进行收缩转换,则会抛出 IllegalArgumentException。
它的使用是从反射开始的,通常是通过Class对象来获取Field实例:
Class.getMethods(), Class.getMethod(String, Class[]), Class.getDeclaredMethods(), Class.getDeclaredMethod(String, Class[])
方法说明:
Method getDeclaredMethod(String name, Class...<?> parameterTypes)返回一个Method对象,该对象表示与由此类表示的类声明的指定名称和参数类型匹配的方法。
Method[] getDeclaredMethods() 返回一个数组,其中包含Method对象,用于在此类中表示的类中声明的所有方法。
Method getEnclosingMethod() 如果是匿名或本地/自动类,则返回此类的封闭方法,否则为空。
Method getMethod(String name, Class...<?> parameterTypes) 返回一个Method对象,该对象用指定的名称和参数类型表示公共方法。
Method[] getMethods() 返回一个数组,该数组包含此Class所表示的类C的所有公共方法的Method对象。
关于Method的常用方法:
boolean equals(Object obj) 将此 Method 与指定对象进行比较
<T extends Annotation> getAnnotation(Class<T> annotationClass) 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null
Annotation[] getDeclaredAnnotations() 返回直接存在于此元素上的所有注释
Class<?> getDeclaringClass() 返回表示声明由此 Method 对象表示的方法的类或接口的 Class 对象
Object getDefaultValue() 返回由此 Method 实例表示的注释成员的默认值
Type getGenericReturnType() 返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象,其实就是返回值类型
int getModifiers() 以整数形式返回此 Method 对象所表示方法的 Java 语言修饰符,没有什么用
String getName() 以 String 形式返回此 Method 对象表示的方法名称
Annotation[][] getParameterAnnotations() 返回表示按照声明顺序对此 Method 对象所表示方法的形参进行注释的那个数组的数组
Class<?>[] getParameterTypes() 按照声明顺序返回 Class 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型
Class<?> getReturnType() 返回一个 Class 对象,该对象描述了此 Method 对象所表示的方法的正式返回类型
int hashCode() 返回此 Method 的哈希码
Object invoke(Object obj, Object... args) 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法,
参数: obj - 从中调用底层方法的对象 args - 用于方法调用的参数
boolean isVarArgs() 如果将此方法声明为带有可变数量的参数,则返回 true;否则,返回 false
String toGenericString() 返回描述此 Method 的字符串,包括类型参数。
另外Method也有从类 java.lang.reflect.AccessibleObject 继承的方法: getAnnotations, isAccessible, isAnnotationPresent, setAccessible
下来打印一下上面的一些方法:
UserTest类如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MethodTest { //注解
String message();
}
public class UserTest {
@Utils(message = "年龄不能小于0")
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean sex;
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String sayHello(String str){
System.out.println(str);
return str;
}
@MethodTest(message = "方法名为:privTest")
private void privTest(){
System.out.println("privTest");
}
}
main方法中的内容:
public static void main(String[] args) {
UserTest mUserTest = new UserTest();
mUserTest.setAge(10);
mUserTest.setName("zhangsan");
Class<?> clazzClass = mUserTest.getClass();
try {
Field[] files = clazzClass.getFields(); //返回该类中声明的所有字段的Field对象(仅public)
//Field field = clazzClass.getField("age"); //返回该类中age对应的属性(仅public)
Field[] files2 = clazzClass.getDeclaredFields(); //返回该类中声明的所有字段的Field对象(包括private)
Field field2 = clazzClass.getDeclaredField("age");返回该类中age对应的属性(包括private)
field2.setAccessible(true);
Field field_sex = clazzClass.getDeclaredField("sex");
//获取一个静态或实例 boolean 字段的值(不用去判断类型,然后强转),相似用法
//getByte(Object obj)
//getChar(Object obj)
//getDouble(Object obj)
//getFloat(Object obj)
//getInt(Object obj)
//getLong(Object obj)
//getShort(Object obj)
System.out.println("field_sex: "+field_sex.getBoolean(mUserTest));
for (Field subField : files2) {
subField.setAccessible(true);
//返回一个 Class 对象,它标识了此 Field 对象所表示字段的声明类型
System.out.println("getType: "+subField.getType());
//返回一个 Type 对象,它表示此 Field 对象所表示字段的声明类型
System.out.println("getGenericType: "+subField.getGenericType());
System.out.println("name: "+subField.getName());//返回此 Field 对象表示的字段的名称
System.out.println("type_int: "+subField.getModifiers()); //返回指定对象上此 Field 表示的字段的值
if(subField.getType().equals(String.class)){ //将此 Field 与指定对象比较
String pass = (String)subField.get(mUserTest);//返回指定对象上此 Field 表示的字段的值
System.out.println("fields_String: "+pass);
}else if(subField.getType().equals(int.class)){ //将此 Field 与指定对象比较
int age = (int)subField.get(mUserTest);
System.out.println("fields_Integer: "+age);
}else if(subField.getType().equals(boolean.class)){
boolean sex = (boolean)subField.get(mUserTest);
System.out.println("fields_Boolean: "+sex);
}
//如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null
Utils annotation = subField.getAnnotation(Utils.class);
if(annotation !=null){
System.out.println(annotation.message()); //获取注解信息
}
System.out.println("---------------------------------------------------------");
}
//将指定对象变量上此 Field 对象表示的字段设置为指定的新值,即 将mUserTest对象上的field_sex属性值修改为true
//field_sex.set(mUserTest, true);
field_sex.setBoolean(mUserTest, true); //将字段的值设置为指定对象上的一个 boolean 值
//相似的方法
//setByte(Object obj, byte b)
//setChar(Object obj, char c)
//setDouble(Object obj, double d)
//setFloat(Object obj, float f)
//setInt(Object obj, int i)
//setLong(Object obj, long l)
//setShort(Object obj, short s)
System.out.println("reset_field_sex: "+field_sex.getBoolean(mUserTest));
//返回一个描述此 Field(包括其一般类型)的字符串
System.out.println("toGenericString: "+field_sex.toGenericString());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("---------------------Method--------------------------------");
try {
Method[] methods = clazzClass.getMethods(); //该数组包含此Class所表示的类的所有公共方法的Method对象(仅public)
//返回一个Method对象,该对象用指定的名称和参数类型表示公共方法(仅public)
//参数:第一个参数是具体调用该方法的对象 第二个参数是执行该方法的具体参数
Method method = clazzClass.getMethod("sayHello",String.class);
//返回一个数组,其中包含Method对象,用于在此类中表示的类中声明的所有方法。
Method[] methods_all = clazzClass.getDeclaredMethods();
//返回一个Method对象,该对象表示与由此类表示的类声明的指定名称和参数类型匹配的方法,包括private
Method method_priv = clazzClass.getDeclaredMethod("privTest",null);
for (Method method2 : methods_all) {
//返回直接存在于此元素上的所有注释
Annotation[] mAnnotation =method2.getAnnotations();
//如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null
MethodTest methodTest = method2.getAnnotation(MethodTest.class);
if(methodTest != null){
System.out.println("methodTest_message: "+methodTest.message());
}
//返回表示声明由此 Method 对象表示的方法的类或接口的 Class 对象,其实就是类名
Class<?> clazzClass2 = method2.getDeclaringClass();
System.out.println("getDeclaringClass: " + clazzClass2.toString());
//getDefaultValue 返回由此 Method 实例表示的注释成员的默认值
System.out.println("getDefaultValue: "+method2.getDefaultValue());
//按照声明顺序返回 Type 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型的
Type[] type = method2.getGenericParameterTypes();
for (Type type2 : type) {
System.out.println("getGenericParameterTypes: "+type2.toString());
}
// getGenericReturnType 返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象,其实就是返回值类型
System.out.println("getGenericReturnType: "+method2.getGenericReturnType());
// getModifiers 以整数形式返回此 Method 对象所表示方法的 Java 语言修饰符
System.out.println("getModifiers: "+method2.getModifiers());
//getName() 以 String 形式返回此 Method 对象表示的方法名称
System.out.println("getName: "+method2.getName());
//getParameterTypes 按照声明顺序返回 Class 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型
Class<?>[] clazzClasses = method2.getParameterTypes();
for (Class<?> class1 : clazzClasses) {
System.out.println("getParameterTypes: "+class1.toString());
}
//返回一个 Class 对象,该对象描述了此 Method 对象所表示的方法的正式返回类型
Class<?> clazzClasses2 = method2.getReturnType();
System.out.println("getReturnType: "+clazzClasses2.toString());
//对带有指定参数的指定对象调用由此 Method 对象表示的底层方法
//参数: obj - 从中调用底层方法的对象 args - 用于方法调用的参数
method.invoke(mUserTest, "invoke");
//toGenericString 返回描述此 Method 的字符串,包括类型参数。toString 返回描述此 Method 的字符串
System.out.println("toGenericString: "+method2.toGenericString()+" toString"+method2.toString());
System.out.println("------------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
}
}
field_sex: false
getType: class java.lang.String
getGenericType: class java.lang.String
name: name
type_int: 1
fields_String: zhangsan
年龄不能小于0
---------------------------------------------------------
getType: int
getGenericType: int
name: age
type_int: 2
fields_Integer: 10
---------------------------------------------------------
getType: boolean
getGenericType: boolean
name: sex
type_int: 1
fields_Boolean: false
---------------------------------------------------------
reset_field_sex: true
toGenericString: public boolean UserTest.sex
---------------------Method--------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericReturnType: class java.lang.String
getModifiers: 1
getName: getName
getReturnType: class java.lang.String
invoke
toGenericString: public java.lang.String UserTest.getName() toStringpublic java.lang.String UserTest.getName()
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericParameterTypes: class java.lang.String
getGenericReturnType: void
getModifiers: 1
getName: setName
getParameterTypes: class java.lang.String
getReturnType: void
invoke
toGenericString: public void UserTest.setName(java.lang.String) toStringpublic void UserTest.setName(java.lang.String)
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericParameterTypes: int
getGenericReturnType: void
getModifiers: 1
getName: setAge
getParameterTypes: int
getReturnType: void
invoke
toGenericString: public void UserTest.setAge(int) toStringpublic void UserTest.setAge(int)
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericParameterTypes: class java.lang.String
getGenericReturnType: class java.lang.String
getModifiers: 1
getName: sayHello
getParameterTypes: class java.lang.String
getReturnType: class java.lang.String
invoke
toGenericString: public java.lang.String UserTest.sayHello(java.lang.String)
toStringpublic java.lang.String UserTest.sayHello(java.lang.String)
------------------------------------
methodTest_message: 方法名为:privTest
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericReturnType: void
getModifiers: 2
getName: privTest
getReturnType: void
invoke
toGenericString: private void UserTest.privTest() toStringprivate void UserTest.privTest()
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericParameterTypes: boolean
getGenericReturnType: void
getModifiers: 1
getName: setSex
getParameterTypes: boolean
getReturnType: void
invoke
toGenericString: public void UserTest.setSex(boolean) toStringpublic void UserTest.setSex(boolean)
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericReturnType: int
getModifiers: 1
getName: getAge
getReturnType: int
invoke
toGenericString: public int UserTest.getAge() toStringpublic int UserTest.getAge()
------------------------------------
getDeclaringClass: class UserTest
getDefaultValue: null
getGenericReturnType: boolean
getModifiers: 1
getName: isSex
getReturnType: boolean
invoke
toGenericString: public boolean UserTest.isSex() toStringpublic boolean UserTest.isSex()
------------------------------------