简体   繁体   中英

Method Reflect - Call sequence of methods

I am trying to learn Method Reflect so I can apply in my Java application.

I created two POJO classes.

Wishes.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;
    }
}

This is what I do in my main method. 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();
        }
    }
}

I run the application. For the first one I am getting exact output as it's straight forward. But for the second I am getting exception. Here is the console output and 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)

How to call the getGreeting method from getWishes from using Day class with Method reflect? Is it possible? Otherwise what is the best way to do that with method reflect?

In my application, the method name I am getting is from one XML file. So it may contain single method or sequence of method calls like the above.

first of all in Day class you should initiate wishes

private Wishes wishes = new Wishes();

second you need to this:

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);

The method Class#getDeclaredMethod takes the name of a method and the types of its parameters. You are handing the string getWishes().getGreeting what is not a valid method name. You want to use

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

what should work in order to get the instance of Wishes from your Day instance. For the received instance, you can then call the getGreeting method reflectively. Method chaining as you suggest it does not work with reflection. There are however libraries easing the reflection API as for example for bean access of chained properties. For your learning purposes, you however need to chain the reflective calls manually.

Reflective calls are not stacked. So the way you are calling the method getGreeting doesn't work.

You can try this way instead:

        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());

Note: This snippet can further be refactored to suit your requirements

There is no such method "getWishes().getGreeting" on the Day class. what you have to do is.

  1. invoke "Day.getWishes() and get the output
  2. on top of the above output object invoke getGreeting

On sequences you have to execute one by one.

By the way, I think it is worth having a look at JXPath library as an alternative. you can give a complex object and do a xpath search.

Reflection calls don't stack - there is no method with the name "getWishes().getGreeting()" in class Day.

You need to first call "Day.getWishes()" and then call "getGreeting()" on the returned object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM