org.springframework.util.ReflectionUtils


獲取方法

// 在類中查找指定方法
Method getName = ReflectionUtils.findMethod(Hello2.class, "getName");
System.out.println(getName);//public java.lang.String com.xc.xcspringboot.model.Hello2.getName()
// 同上,額外提供方法參數類型作查找條件
Method getName1 = ReflectionUtils.findMethod(Hello2.class, "getName", String.class);
System.out.println(getName1);//public java.lang.String com.xc.xcspringboot.model.Hello2.getName(java.lang.String)

// 獲得類中所有方法,包括繼承而來的
Method[] allDeclaredMethods = ReflectionUtils.getAllDeclaredMethods(Hello2.class);
// System.out.println(ArrayUtils.toString(allDeclaredMethods));

// 在類中查找指定構造方法
Constructor<Hello2> hello2Constructor = ReflectionUtils.accessibleConstructor(Hello2.class);
System.out.println(hello2Constructor);//public com.xc.xcspringboot.model.Hello2()
Constructor<Hello2> hello2Constructor2 = ReflectionUtils.accessibleConstructor(Hello2.class, Integer.class, String.class);
System.out.println(hello2Constructor2);//public com.xc.xcspringboot.model.Hello2(java.lang.Integer,java.lang.String)

// 是否是 equals() 方法
Method equals_method = ReflectionUtils.findMethod(Hello2.class, "equals", Object.class);
boolean equalsMethod = ReflectionUtils.isEqualsMethod(equals_method);
System.out.println(equalsMethod);//true
for (Method method : allDeclaredMethods) {
    boolean equalsMethod2 = ReflectionUtils.isEqualsMethod(method);
    // System.out.println(equalsMethod2);
}
// 是否是 hashCode() 方法
Method hashCode_method = ReflectionUtils.findMethod(Hello2.class, "hashCode");
boolean hashCodeMethod = ReflectionUtils.isHashCodeMethod(hashCode_method);
System.out.println(hashCodeMethod);//true
// 是否是 toString() 方法
Method toString = ReflectionUtils.findMethod(Hello2.class, "toString");
boolean toStringMethod = ReflectionUtils.isToStringMethod(toString);
System.out.println(toStringMethod);//true

// 是否是從 Object 類繼承而來的方法
for (Method method : allDeclaredMethods) {
    boolean equalsMethod2 = ReflectionUtils.isObjectMethod(method);
    // System.out.println(equalsMethod2);
}

// 檢查一個方法是否聲明拋出指定異常
boolean declaresException = ReflectionUtils.declaresException(getName1, XcException.class);
System.out.println(declaresException);//true

 

執行方法

// 執行方法
Hello2 hello2 = new Hello2(1, "aaa");
Object invokeMethod = ReflectionUtils.invokeMethod(getName, hello2);
System.out.println(invokeMethod);//aaa
// 同上,提供方法參數
Object invokeMethod2 = ReflectionUtils.invokeMethod(getName1, hello2, "bbb");
System.out.println(invokeMethod2);//bbb
// 取消 Java 權限檢查。以便后續執行該私有方法
Method getName2 = ReflectionUtils.findMethod(Hello2.class, "getName2");
ReflectionUtils.makeAccessible(getName2);
Object invokeMethod3 = ReflectionUtils.invokeMethod(getName2, hello2);
System.out.println(invokeMethod3);//aaa
// 取消 Java 權限檢查。以便后續執行私有構造方法
Constructor<Hello2> hello2Constructor3 = ReflectionUtils.accessibleConstructor(Hello2.class, Integer.class, String.class, String.class);
ReflectionUtils.makeAccessible(hello2Constructor3);
Hello2 hello3 = hello2Constructor3.newInstance(1, "aaa", "176");
System.out.println(hello3);//Hello2(id=1, name=aaa, age=null, phone=176)

 

獲取字段

// 在類中查找指定屬性
Field name = ReflectionUtils.findField(Hello2.class, "name");
System.out.println(name);//private java.lang.String com.xc.xcspringboot.test.org.springframework.util.ReflectionUtilsTest.Hello2.name
// 同上,多提供了屬性的類型
Field name3 = ReflectionUtils.findField(Hello2.class, "name3", String.class);
System.out.println(name3);//private java.lang.String com.xc.xcspringboot.test.org.springframework.util.ReflectionUtilsTest.Hello2.name
// 是否為一個 "public static final" 屬性
boolean publicStaticFinal = ReflectionUtils.isPublicStaticFinal(name3);
System.out.println(publicStaticFinal);//true

 

設置字段

// 取消 Java 的權限控制檢查。以便后續讀寫該私有屬性
ReflectionUtils.makeAccessible(name_field);
// 設置 target 對象的 field 屬性值,值為 value
ReflectionUtils.setField(name_field, hello3, "aac");
// 獲取 target 對象的 field 屬性值
Object field = ReflectionUtils.getField(name_field, hello3);
System.out.println(field);//aac
// 同類對象屬性對等賦值
ReflectionUtils.shallowCopyFieldState(hello3, hello2);
System.out.println(hello2);//Hello2(id=1, name=aac, age=null, phone=176)

   

附:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hello2 {

    public static final String name3 = "name3";

    @ApiModelProperty(value = "主鍵")
    @NotNull(message = "修改角色必須有id", groups = UpdateValid.class)
    @ApiComment(value = "主鍵", sample = "1")
    private Integer id;

    @ApiModelProperty(value = "名稱")
    @NotNull(message = "添加角色必須有name", groups = AddValid.class)
    @Length(min = 1, max = 10, message = "名稱不合法", groups = {AddValid.class, UpdateValid.class})
    @Size(min = 1, max = 5, message = "name 長度小於1或大於5")
    @ApiComment(value = "名稱", sample = "xc")
    private String name;

    @ApiModelProperty(value = "年齡")
    @NotNull(message = "age 為空")
    @ApiComment(value = "年齡", sample = "18")
    private Integer age;

    @ApiModelProperty(value = "手機號")
    @NotNull(message = "手機號不能為空")
    @PhoneValid
    private String phone;

    public String getName(String aaa) throws XcException {
        return aaa;
    }

    public Hello2(@NotNull(message = "修改角色必須有id", groups = UpdateValid.class) Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    private Hello2(@NotNull(message = "修改角色必須有id", groups = UpdateValid.class) Integer id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    private String getName2() {
        return name;
    }
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM