Java 反射機制[Field反射]
1. 反射概念及功能
反射就是把Java類中的各種成分映射成相應的Java類。例如一個Java類中用一個Class類的對象來表示。一個類中的組成部分分為成員變量,方法,構造方法,包等等。
Java反射機制主要提供了以下功能: 判斷在運行時任意一個對象所屬的類;在運行時構造任意一個類的對象;判斷在運行時任意一個類所具有的成員變量和方法;在運行時調用任意一個對象的方法;生成動態代理。
2. Field反射
以下代碼將obj對象中的String類型的字段對應的Value中含有i的字符替換為abc。通過這個簡單的案例進行初步分析Java的反射機制。
Java Code
/*
* System Abbrev :
* system Name :
* Component No :
* Component Name:
* File name :ReflectTestMain.java
* Author :Qiuzhenping
* Date :2014-10-25
* Description : <description>
*/
/* Updation record 1:
* Updation date : 2014-10-25
* Updator : Qiuzhenping
* Trace No: <Trace No>
* Updation No: <Updation No>
* Updation Content: <List all contents of updation and all methods updated.>
*/
package com.qiuzhping.reflect.main;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* <Description functions in a word>
* 反射就是把Java類中的各種成分映射成相應的Java類。
* <Detail description>
*
* @author Qiuzhenping
* @version [Version NO, 2014-10-25]
* @see [Related classes/methods]
* @since [product/module version]
*/
public class ReflectTestMain {
/** <default constructor>
*/
public ReflectTestMain() {
// TODO Auto-generated constructor stub
}
/** <Description functions in a word>
* 2014-10-25
* <Detail description>
* @author Qiuzhenping
* @param args [Parameters description]
* @return void [Return type description]
* @exception throws [Exception] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//Constructor[] contructor = Person.class.getConstructors();
//Method[] method = Person.class.getMethods();
Person p = new Person(24, "Qiuzhping", "100001", "Qiuzhping");
// Field [] field = p.getClass().getDeclaredFields();
// for(Field f:field){
// f.setAccessible(true);
// System.out.println(f.getName());
// Object obj = f.get(p);
// System.out.println(obj);
// }
changeStringValue(p);
System.out.println(p.toString());
}
/** <Description functions in a word>
* 將obj對象中的String類型的字段對應的Value中含有i的字符替換為abc<BR>
* 2014-10-26
* <Detail description>
* @author Qiuzhenping
* @param obj [Parameters description]
* @return void [Return type description]
* @exception throws [Exception] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
private static void changeStringValue(Object obj) throws Exception {
Field[] fields = obj.getClass().getDeclaredFields();
for(Field f : fields){
f.setAccessible(true);//暴力反射
if(f.getType() == String.class){//字節碼比較是用 ==
String oldValue = (String) f.get(obj);
String newValue = oldValue.replaceAll("i", "abc");//將所有的i替換為abc
f.set(obj, newValue);
}
}
}
static class Person {
public Person(int age, String name, String id, String pwd) {
super();
this.age = age;
this.name = name;
this.id = id;
this.pwd = pwd;
}
@Override
public String toString() {
return "age = "+age +"\tname = "+name+"\tid = "+id+"\tpwd = "+pwd;
}
private int age;
private String name;
private String id;
private String pwd;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/** <default constructor>
*/
public Person() {
// TODO Auto-generated constructor stub
}
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!