简体   繁体   English

方法反映-方法的调用顺序

[英]Method Reflect - Call sequence of methods

I am trying to learn Method Reflect so I can apply in my Java application. 我正在尝试学习方法反射,以便可以在Java应用程序中应用。

I created two POJO classes. 我创建了两个POJO类。

Wishes.java 愿望.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 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 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. 这是控制台输出和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? 如何将Day类与Method反射一起使用, getWishesgetWishes调用getGreeting方法? 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. 在我的应用程序中,我得到的方法名称来自一个XML文件。 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. 方法Class#getDeclaredMethod采用方法的名称及其参数的类型。 You are handing the string getWishes().getGreeting what is not a valid method name. 您正在将字符串getWishes().getGreeting传递给无效的方法名称。 You want to use 你想用

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

what should work in order to get the instance of Wishes from your Day instance. 为了从您的Day实例中获取Wishes实例,应该怎么做。 For the received instance, you can then call the getGreeting method reflectively. 对于收到的实例,您可以然后反射地调用getGreeting方法。 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. 但是,有些库简化了反射API,例如用于对链式属性进行Bean访问。 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. 因此,您调用方法getGreeting的方式不起作用。

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. 在Day类上没有这样的方法“ getWishes()。getGreeting”。 what you have to do is. 您要做的是。

  1. invoke "Day.getWishes() and get the output 调用“ Day.getWishes()并获取输出
  2. on top of the above output object invoke getGreeting 在上述输出对象之上,调用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. 顺便说一句,我认为值得一看的是JXPath库作为替代方案。 you can give a complex object and do a xpath search. 您可以给一个复杂的对象并进行xpath搜索。

Reflection calls don't stack - there is no method with the name "getWishes().getGreeting()" in class Day. 反射调用不会堆叠-Day类中没有名称为“ getWishes()。getGreeting()”的方法。

You need to first call "Day.getWishes()" and then call "getGreeting()" on the returned object. 您需要先调用“ Day.getWishes()”,然后在返回的对象上调用“ getGreeting()”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 顺序调用方法 - Call Methods in Sequence 如何锁定一系列方法,以便其他线程调用那些相同的方法应该等待? - How to lock a sequence of methods, such that other thread call those same method should be waiting? 将方法调用转换为该方法的 java.lang.reflect.Method object - Converting method call to the java.lang.reflect.Method object of that method dalvik Java如何使用反射调用超类方法? - dalvik Java How to call super class method using reflect? 为什么你能在Java和.Net中反映和调用(不是那么)私有方法 - Why can you reflect and call a (not so) private method in Java and .Net Java设计问题:强制执行方法调用序列 - Java Design Issue: Enforce method call sequence 给定一个java.lang.reflect.Method对象,有一种简单的方法来获取其覆盖的方法的层次结构 - Given a java.lang.reflect.Method object, is there an easy way to obtain a hierachry of the methods it overrides 为什么Javascript(在Rhino中运行)无法访问java.lang.reflect.Method实例上的方法? - Why can't Javascript (running in Rhino) access methods on a java.lang.reflect.Method instance? 通过Java中的方法中的参数调用2个方法 - Call 2 methods through an argument in method in java 使用方法引用调用静态方法 - Using method reference to call static methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM