文章目錄
- 示例
- 5. 通過反射獲得類的private、 protected、 默認訪問修飾符的屬性值。
- 6. 通過反射獲得類的private方法。
- 7. 通過反射實現一個工具BeanUtils, 可以將一個對象屬性相同的值賦值給另一個對象
接上篇:
示例
5. 通過反射獲得類的private、 protected、 默認訪問修飾符的屬性值。
import java.lang.reflect.Field;class Student {private String privateField = "私有屬性值";protected String protectedField = "受保護屬性值";String defaultField = "默認訪問屬性值"; // 包級私有public String publicField = "公有屬性值";
}
public class ReflectionExample5 {public static void main(String[] args) {Student student = new Student();try {// 獲取類的 Class 對象Class<?> clazz = Student.class;// ================== 訪問私有屬性 ==================Field privateField = clazz.getDeclaredField("privateField");privateField.setAccessible(true); // 解除私有訪問限制String privateValue = (String) privateField.get(student);System.out.println("privateField: " + privateValue);// ================== 訪問受保護屬性 ==================Field protectedField = clazz.getDeclaredField("protectedField");protectedField.setAccessible(true); // 無需繼承關系即可訪問String protectedValue = (String) protectedField.get(student);System.out.println("protectedField: " + protectedValue);// ================== 訪問默認(包級私有)屬性 ==================Field defaultField = clazz.getDeclaredField("defaultField");defaultField.setAccessible(true);String defaultValue = (String) defaultField.get(student);System.out.println("defaultField: " + defaultValue);// ================== 訪問公有屬性 ==================// 方式 1:getDeclaredField + setAccessible(強制訪問)Field publicField1 = clazz.getDeclaredField("publicField");publicField1.setAccessible(true); // 即使公有也強制解除限制(非必須)String publicValue1 = (String) publicField1.get(student);System.out.println("publicField(強制訪問): " + publicValue1);// 方式 2:直接通過 getField 獲取(不推薦,僅用于對比)Field publicField2 = clazz.getField("publicField");String publicValue2 = (String) publicField2.get(student);System.out.println("publicField(正常訪問): " + publicValue2);} catch (NoSuchFieldException e) {System.err.println("字段不存在: " + e.getMessage());} catch (IllegalAccessException e) {System.err.println("訪問權限失敗: " + e.getMessage());}}
}
關鍵操作說明
- getDeclaredField() 方法
- 作用:獲取類中聲明的所有字段(包括 private/protected/默認/public)
- 需要手動調用 setAccessible(true) 來突破非 public 屬性的訪問限制。 - setAccessible(true)
- 方法:Field.setAccessible(true)
- 意義:解除 Java 的訪問控制檢查,允許操作非公有字段。
- 安全性警告:此操作會繞過 Java 的封裝機制,僅建議在框架等需要深度操作時使用。 - 字段值與對象實例的關聯
- 對于 實例字段:必須傳入具體對象實例(如 student),通過 field.get(object) 獲取值。
- 對于 靜態字段:可直接傳入 null,如 field.get(null)。
6. 通過反射獲得類的private方法。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;class MyClass3 {private String privateMethod(String param) {return "私有方法被調用,參數: " + param;}
}
public class ReflectionExample6 {public static void main(String[] args) {try {// 創建類的實例MyClass3 myObject = new MyClass3();// 獲取Class對象Class<?> clazz = MyClass3.class;// 獲取私有方法,需指定方法名和參數類型Method method = clazz.getDeclaredMethod("privateMethod", String.class);// 解除訪問限制(關鍵步驟)method.setAccessible(true);// 調用方法,傳入實例及參數String result = (String) method.invoke(myObject, "Hello");// 輸出結果System.out.println("調用結果: " + result);} catch (NoSuchMethodException e) {System.err.println("方法未找到: " + e.getMessage());} catch (IllegalAccessException e) {System.err.println("非法訪問: " + e.getMessage());} catch (InvocationTargetException e) {System.err.println("方法內部錯誤: " + e.getCause());}}
}
7. 通過反射實現一個工具BeanUtils, 可以將一個對象屬性相同的值賦值給另一個對象
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;class BeanUtils {/*** 將源對象的屬性值復制到目標對象(淺拷貝)* @param source 源對象* @param target 目標對象*/public static void copyProperties(Object source, Object target) {if (source == null || target == null) {throw new IllegalArgumentException("源對象和目標對象不能為 null");}// 獲取源對象和目標對象的所有字段(包含父類字段)List<Field> sourceFields = getAllFields(source.getClass());List<Field> targetFields = getAllFields(target.getClass());for (Field sourceField : sourceFields) {// 查找目標對象中與源對象字段同名的字段Field targetField = findField(targetFields, sourceField.getName());if (targetField == null) continue;// 檢查類型是否兼容(支持基本類型和包裝類型)if (!isTypeCompatible(sourceField.getType(), targetField.getType())) continue;try {// 設置字段可訪問性sourceField.setAccessible(true);targetField.setAccessible(true);// 復制值Object value = sourceField.get(source);targetField.set(target, value);} catch (IllegalAccessException e) {// 處理無法訪問的異常System.err.println("字段復制失敗: " + sourceField.getName() + " -> " + targetField.getName());}}}/*** 獲取類及其父類的所有字段(非靜態)*/private static List<Field> getAllFields(Class<?> clazz) {List<Field> fields = new java.util.ArrayList<>();while (clazz != null && clazz != Object.class) {fields.addAll(Arrays.stream(clazz.getDeclaredFields()).filter(f -> !isStatic(f)).collect(Collectors.toList()));clazz = clazz.getSuperclass();}return fields;}/*** 根據字段名從字段列表中查找字段*/private static Field findField(List<Field> fields, String fieldName) {return fields.stream().filter(f -> f.getName().equals(fieldName)).findFirst().orElse(null);}/*** 判斷字段是否為靜態*/private static boolean isStatic(Field field) {return java.lang.reflect.Modifier.isStatic(field.getModifiers());}/*** 判斷源類型與目標類型是否兼容*/private static boolean isTypeCompatible(Class<?> sourceType, Class<?> targetType) {// 處理基本類型與包裝類型的兼容(例如 int -> Integer)if (sourceType.isPrimitive()) {return targetType.isPrimitive() ? sourceType.equals(targetType) : getWrapperType(sourceType).equals(targetType);} else if (targetType.isPrimitive()) {return getWrapperType(targetType).equals(sourceType);} else {return targetType.isAssignableFrom(sourceType);}}/*** 獲取基本類型對應的包裝類型*/private static Class<?> getWrapperType(Class<?> primitiveType) {if (primitiveType == int.class) return Integer.class;if (primitiveType == long.class) return Long.class;if (primitiveType == boolean.class) return Boolean.class;if (primitiveType == byte.class) return Byte.class;if (primitiveType == char.class) return Character.class;if (primitiveType == short.class) return Short.class;if (primitiveType == double.class) return Double.class;if (primitiveType == float.class) return Float.class;return primitiveType;}
}// 父類
class Person {protected String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}// 子類
class Employee extends Person {private double salary;private Integer departmentId;public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Integer getDepartmentId() {return departmentId;}public void setDepartmentId(Integer departmentId) {this.departmentId = departmentId;}
}// 目標類
class EmployeeDTO {private String name;private int age; // 基本類型private Double salary; // 包裝類型private Integer departmentId;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Double getSalary() {return salary;}public void setSalary(Double salary) {this.salary = salary;}public Integer getDepartmentId() {return departmentId;}public void setDepartmentId(Integer departmentId) {this.departmentId = departmentId;}
}public class ReflectionExample7 {public static void main(String[] args) {// 準備數據Employee emp = new Employee();emp.name = "張三";emp.setAge(30);emp.setSalary(15000.5);emp.setDepartmentId(101);// 目標對象EmployeeDTO dto = new EmployeeDTO();// 復制屬性BeanUtils.copyProperties(emp, dto);// 驗證結果System.out.println("DTO.name: " + dto.getName());System.out.println("DTO.age: " + dto.getAge());System.out.println("DTO.salary: " + dto.getSalary());System.out.println("DTO.departmentId: " + dto.getDepartmentId());}
}