如何正确使用反射(特殊情况)JAVA

huangapple go评论68阅读模式
英文:

how to use reflection properly (special case) JAVA

问题

我有一个POJO类,其中有许多getter方法。现在我正在使用来自java.lang.reflect包的Method类,以获取该类中的所有getter方法。我想调用这些getter,但我不知道该如何做。arraylistFilter是一个包含我的请求结果的Arraylist<pojo>,Option是一个对象字段数据类型,实际上有大约100个getter。我如何在不需要逐个调用的情况下获取它们?我在我的.中应该放什么?那里是我想要能够调用我的getter的地方。

try {
    Class<? extends Option> testObject = new Option().getClass();
    Method[] methods = testObject.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (method.getName().startsWith("get") &&
            method.getParameterCount() == 0) {
            for (int i = 0; i < arrayListFilter.size(); i++) {
                arrayListFilter.get(i).getOptions().invokeMethod(method.getName());
            }
        }
    }
} catch (Exception e) {
    // 在异常情况下做一些处理
}
英文:

I hava a POJO class which has a lot of getters. Now I’m using the class Method from the java.lang.reflect package to get all the get methods from that class. I want to invoke those getters but I don’t have idea how. arraylistFilter is an Arraylist&lt;pojo&gt; that contains the result from my request. Option is an Object Field data type which is the one that actually has like 100 getters. How can I get each of them without needed to call 1 by 1. what goes in my .??? there is where I want to be able to invoke my getters.

try {
    Class&lt;? extends Options&gt; testObject = new Options().getClass();
    Method[] methods = testObject.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (method.getName().startsWith(&quot;get&quot;) &amp;&amp;
            method.getGenericParameterTypes().length == 0) {
            for (int i = 0; i &lt; arrayListFilter.size(); i++) {
                arrayListFilter.get(i).getOptions().???;
            }
        }
    }
} catch (Exception e) {
    // do something with the exceptions
}

答案1

得分: 1

尝试 {
    Method[] methods = Options.class.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (method.getName().startsWith("get") &&
            method.getParameterTypes().length == 0) {
            for (int i = 0; i < arrayListFilter.size(); i++) {
                Object value = method.invoke(arrayListFilter.get(i).getOptions());
                // 'value'的实际类型将取决于getter的形式返回类型和实际返回类型。
                // 当返回类型是基本类型时,它会映射到相应的包装类型。
            }
        }
    }
} catch (Exception e) {
    // 处理异常的代码
}
英文:

It will be something like this:

try {
    Method[] methods = Options.class.getMethods();
    for (Method method : methods) {
        String name = method.getName();
        if (method.getName().startsWith(&quot;get&quot;) &amp;&amp;
            method.getParameterTypes().length == 0) {
            for (int i = 0; i &lt; arrayListFilter.size(); i++) {
                Object value = method.invoke(arrayListFilter.get(i).getOptions());
                // The actual type of &#39;value&#39; will depend on the getter&#39;s
                // formal return type AND the actual type it returns.
                // When the return type is a primitive, it is mapped to
                // the corresponding wrapper.  
            }
        }
    }
} catch (Exception e) {
    // do something with the exceptions
}

We have simplified / fixed the code that gets the Method objects, and have changed getGenericParameterTypes() to getParameterTypes(). Generic type parameters are not relevant here, but you do need to filter out getters that require arguments, because there is no way to supply sensible argument values.

huangapple
  • 本文由 发表于 2020年5月4日 06:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定