黑马程序员-Reflect(2)

来源:互联网 发布:王国风云2 mac 编辑:程序博客网 时间:2024/06/07 02:05

---------------------android培训、java培训、期待与您交流! -------------------------

Fields

Field: class instance field, static field, enum constant.

获取Field Object

相关API如下:
FieldgetField(String name)
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object
Field[]getFields()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object
FieldgetDeclaredField(String name)
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
Field[]getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Notd:getField[s]获取当前类或继承类中的public字段
getDeclaredField[s]只在当前类中查找, 不受修饰符的影响。
找不到,throw NoSuchFieldException

获取Field Type

Class c = c.forName("...");//获取Class Object , 具体请查看Reflect(1)Field f = c.getField("fieldName")Class fc = f.getType();Type t = f.getGenericType();/*public Type getGenericType() {        if (getGenericSignature() != null)            return getGenericInfo().getGenericType();        else            return getType();}*/

获取Field Modifiers

Field f = c.getField("...");f.getModifiers();

获取或设置Field Values

//通用Field设置函数Field f = c.getField("...");f.setXXX(instanceObj, ...);//IllegalArgumentException//public Integer val;Field f = c.getField("val");f.setInt(instanceObj, 3);//throw excption.//关于类型的兼容性通过: Class.isAssignableFrom()测试//Integer.class.isAssignableFrom(int.class)//output: false//NoSuchFieldException for Non-Public Fields//通过:Class.getDeclaredField()解决//IllegalAccessException when Modifying final fields//solution: AccessibleObject.setAccessible()->f.setAccessible(true)//Bean PropertyPropertyDescriptor pd = new PropertyDescriptor("age", UserBean.class);Method mw = pd.getWriteMethod();mw.invoke(u, 25);

Methods

Get Method API

MethodgetMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
Method[]getMethods()
Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces
MethodgetDeclaredMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object
Method[]getDeclaredMethods()
Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods
//获取某个具体的Method对象: Method m = c.get[Declared]Method(...)//获取Method对象集合:       Method[] mset = c.get[Declared]Methods(....)//获取参数个数为0的Method ObjectMethod m = c.getMethod("methodName", new Class<?>[0])//获取参数个数不为0的Method Object, pnType代表形参n的类型m = c.getMethod("methodName",                  new Class<?>[] {p1Type.class, p2Type.class, .., pnType.class});//Note:当形参为泛型时,如果为T传入Object.class, 如果为参数化类型,例如 HashMap, 则传入HashMap.class

Get Method Type Info

Method m = c.getMethod("...");//Get method object//Return Typem.getReturnType();m.getGenericReturnType();//ParameterTypesm.getParameterTypes();m.getGenericParameterTypes()

Get Method Modifiers

m.getModifiers()

Invoking Methods

//调用形参个数为0的Method Objectm.invoke(objInstance, new Object[0]);//调用形参个数不为0的Method Objectm.invoke(objInstance, new Object[] {arg1, arg2, .., argn});//如果调用的Method Object 所代表的方法修饰符包括static , 则objInstance可为null//当参数的类型为可变参数时, 分两种情况//1. 可变参数的类型为原始类型时, 调用形式如上//2. 可变参数的类型为引用类型时,调用形式要采用如下的方式//m.invoke(objInstance, (Object)new argType[]{....}) 或者//m.invoke(objInstance, new Object[] { new argType[] {....}})

Constructors

Constructor<T>getConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object
Constructor<?>[]getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object
Constructor<T>getDeclaredConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
Constructor<?>[]getDeclaredConstructors()
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object
Constructor Object 获取信息的方法与Method Object 一致

Create New Class Instances

//Constructor.newInstance() VS Class.newInstance()//建议使用Constructor.newInstance, 提供更好的异常处理//调用newInstance注意事项参见上面的Method.invoke

---------------------android培训、java培训、期待与您交流! -------------------------

0 0
原创粉丝点击