簡體   English   中英

方法反映-方法的調用順序

[英]Method Reflect - Call sequence of methods

我正在嘗試學習方法反射,以便可以在Java應用程序中應用。

我創建了兩個POJO類。

願望.java

public class Wishes {

    private String greeting;

    public String getGreeting() {
        this.greeting="Good Afternoon!";
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
}

Day.java

public class Day {

    private Wishes wishes;

    public Wishes getWishes() {
        return wishes;
    }

    public void setWishes(Wishes wishes) {
        this.wishes = wishes;
    }
}

這就是我在主要方法中所做的。 DemoApp.java

public class DemoApp {
    public static void main(String[] args) {
        try {
            Class cls=Wishes.class;
            Method method1=cls.getDeclaredMethod("getGreeting");
            String result1=(String) method1.invoke(cls.newInstance());
            System.out.println(result1);
            Class clazz=Day.class;
            Method method=clazz.getDeclaredMethod("getWishes().getGreeting");
            String result=(String) method.invoke(clazz.newInstance());
            System.out.println(result);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}

我運行該應用程序。 對於第一個,我直接獲得了准確的輸出。 但是第二次我要例外了。 這是控制台輸出和stacktrace。

Good Afternoon!
java.lang.NoSuchMethodException: com.myapp.demo.Day.getWishes().getGreeting()
    at java.lang.Class.getDeclaredMethod(Class.java:2004)
    at com.myapp.demo.DemoApp.main(DemoApp.java:17)

如何將Day類與Method反射一起使用, getWishesgetWishes調用getGreeting方法? 可能嗎? 否則,使用方法反映的最佳方法是什么?

在我的應用程序中,我得到的方法名稱來自一個XML文件。 因此,它可能像上面一樣包含單個方法或方法調用序列。

首先,在一天課中,您應該發起願望

private Wishes wishes = new Wishes();

第二,您需要這樣做:

Method method=clazz.getDeclaredMethod("getWishes");
Object result= method.invoke(clazz.newInstance());
Method method2=result.getClass().getDeclaredMethod("getGreeting");
String result2=(String) method2.invoke(cls.newInstance());
System.out.println(result2);

方法Class#getDeclaredMethod采用方法的名稱及其參數的類型。 您正在將字符串getWishes().getGreeting傳遞給無效的方法名稱。 你想用

Method method = clazz.getDeclaredMethod("getWishes");

為了從您的Day實例中獲取Wishes實例,應該怎么做。 對於收到的實例,您可以然后反射地調用getGreeting方法。 您建議的方法鏈接不適用於反射。 但是,有些庫簡化了反射API,例如用於對鏈式屬性進行Bean訪問。 但是,出於學習目的,您需要手動鏈接反射調用。

反射呼叫不堆疊。 因此,您調用方法getGreeting的方式不起作用。

您可以這樣嘗試:

        Class cls=Wishes.class;
        Method method1=cls.getDeclaredMethod("getGreeting");
        String result1=(String) method1.invoke(cls.newInstance());
        System.out.println(result1);
        Class clazz=Day.class;
        Object ob = clazz.newInstance();
        Method method2=clazz.getDeclaredMethod("setWishes", cls);
        method2.invoke(ob, cls.newInstance());
        Method method=clazz.getDeclaredMethod("getWishes");
        Object day =(Object) method.invoke(ob);
        System.out.println(((Wishes)day).getGreeting());

注意:可以根據您的要求進一步重構此代碼段

在Day類上沒有這樣的方法“ getWishes()。getGreeting”。 您要做的是。

  1. 調用“ Day.getWishes()並獲取輸出
  2. 在上述輸出對象之上,調用getGreeting

在序列上,您必須一個接一個地執行。

順便說一句,我認為值得一看的是JXPath庫作為替代方案。 您可以給一個復雜的對象並進行xpath搜索。

反射調用不會堆疊-Day類中沒有名稱為“ getWishes()。getGreeting()”的方法。

您需要先調用“ Day.getWishes()”,然后在返回的對象上調用“ getGreeting()”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM