反射操作使用案例分析(三)
操作 Field 成员变量类
通过Class类对象获取对应类的Field成员变量类对象
Field[ ] getFields ( ) ;
获取类内所有非私有化成员变量数组
Field[ ] getDeclaredFields ( ) ;
【暴力反射】
获取类内所有成员变量数组,包括私有化成员变量
Field getField ( String fieldName) ;
根据成员变量名字获取对应的成员变量对象,要求当前成员变量非私有化
例如:
public int test;
cls. getField ( "test" ) ;
Field getDeclaredField ( String fieldName) ;
【暴力反射】
获取类内指定名字的成员变量对象,包括私有化成员变量
例如:
private String name;
private int id;
cls. getDeclaredField ( "name" ) ;
cls. getDeclaredField ( "id" ) ;
操作Field类对象赋值取值成员变量
void set ( Object obj, Object value) ;
赋值Field类对象对应成员变量
Object obj 明确当前赋值的成员变量是哪一个类对象的。
Object value 给予当前成员变量赋值使用数据
Before:
Person p = new Person ( ) ;
p. setName ( "小红" ) ;
Now:
name. set ( p, "小红" ) ;
Object get ( Object obj) ;
取值Field类对象对应成员变量
Object obj 明确获取的成员变量对应数据是哪一个类对象
Before
Person p = new Person ( ) ;
p. getName ( ) ; == > 小红
name. get ( p) ; == > 小红
package com. qfedu. a_reflect;
import java. lang. reflect. Field;
import java. lang. reflect. InvocationTargetException;
public class GetFieldObject {
public static void main ( String[ ] args)
throws ClassNotFoundException, NoSuchFieldException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException {
Class cls = Class. forName ( "com.qfedu.a_reflect.Person" ) ;
Field[ ] fields = cls. getFields ( ) ;
for ( Field field : fields) {
System. out. println ( field) ;
}
System. out. println ( ) ;
Field[ ] declaredFields = cls. getDeclaredFields ( ) ;
for ( Field field : declaredFields) {
System. out. println ( field) ;
}
System. out. println ( ) ;
Field test = cls. getField ( "test" ) ;
System. out. println ( test) ;
System. out. println ( ) ;
Field id = cls. getDeclaredField ( "id" ) ;
Field name = cls. getDeclaredField ( "name" ) ;
System. out. println ( id) ;
System. out. println ( name) ;
System. out. println ( ) ;
Object obj = cls. getConstructor ( ) . newInstance ( ) ;
System. out. println ( obj) ;
test. set ( obj, 100 ) ;
System. out. println ( obj) ;
System. out. println ( test. get ( obj) ) ;
id. setAccessible ( true ) ;
name. setAccessible ( true ) ;
id. set ( obj, 10 ) ;
name. set ( obj, "百合绿豆" ) ;
System. out. println ( obj) ;
System. out. println ( id. get ( obj) ) ;
System. out. println ( name. get ( obj) ) ;
System. out. println ( ) ;
System. out. println ( id. getType ( ) ) ;
System. out. println ( name. getType ( ) ) ;
}
}
暴力反射授权
class AccessibleObject 类内方法
public static void setAccessible ( AccessibleObject[ ] array, boolean flag) ;
通过类名调用的静态工具方式,给予AccessibleObject类对象或者其子类对象数组,赋值操作权限。
子类对象包括: Field Method Constructor
public void setAccessible ( boolean flag) ;
通过AccessibleObject类对象调用,单一权限授权,Field Method Constructor都可以使用。
案例操作
需要使用
1. String方法
2. IO流 推荐字符流操作
3. 反射
4. 自行了解 ==> String 转其他类型方法 百度 parse系列方法
文件名:
studentInfo.txt
文件内容:
className=com.qfedu.a_reflect.Student
name=刘某
age=15
gender=false
javaScore=100
webScore=99
dbScore=88
目标
文件内容转Student类对象
package com. qfedu. a_reflect;
import java. io. BufferedReader;
import java. io. FileReader;
import java. io. IOException;
import java. lang. reflect. Field;
import java. lang. reflect. InvocationTargetException;
import java. util. Arrays;
@SuppressWarnings ( "all" )
public class ReflectDemo {
public static void main ( String[ ] args)
throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, NoSuchFieldException {
BufferedReader br = new BufferedReader ( new FileReader ( "./data/studentInfo.txt" ) ) ;
String classInfo = br. readLine ( ) ;
String className = classInfo. substring ( classInfo. indexOf ( "=" ) + 1 ) ;
Class cls = Class. forName ( className) ;
Object obj = cls. getConstructor ( ) . newInstance ( ) ;
String info = null;
Object value = null;
while ( ( info = br. readLine ( ) ) != null) {
String[ ] split = info. split ( "=" ) ;
System. out. println ( Arrays. toString ( split) ) ;
Field field = cls. getDeclaredField ( split[ 0 ] ) ;
field. setAccessible ( true ) ;
Class type = field. getType ( ) ;
if ( type. equals ( String. class ) ) {
value = split[ 1 ] ;
} else if ( type. equals ( int . class ) ) {
value = Integer. parseInt ( split[ 1 ] ) ;
} else if ( type. equals ( boolean . class ) ) {
value = Boolean. parseBoolean ( split[ 1 ] ) ;
}
field. set ( obj, value) ;
}
System. out. println ( obj) ;
br. close ( ) ;
}
}