英文:
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<pojo>
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<? extends Options> testObject = new Options().getClass();
Method[] methods = testObject.getMethods();
for (Method method : methods) {
String name = method.getName();
if (method.getName().startsWith("get") &&
method.getGenericParameterTypes().length == 0) {
for (int i = 0; i < 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("get") &&
method.getParameterTypes().length == 0) {
for (int i = 0; i < arrayListFilter.size(); i++) {
Object value = method.invoke(arrayListFilter.get(i).getOptions());
// The actual type of 'value' will depend on the getter'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论