gpt4 book ai didi

java - 诡异的 Java 反射错误

转载 作者:行者123 更新时间:2023-12-01 13:24:17 28 4
gpt4 key购买 nike

所以,问题来了。我试图在 Java 中使用反射来定义一种“ View ”类型,以便通过有序方法声明输出到 Excel 工作表。 (按数字排序)

从理论上讲,这应该可以正常工作,并且在一定程度上确实如此。

问题在于,反射(reflection)方法有时似乎是随机决定起作用的,而不是其他方法。它不会抛出异常,它会毫无困难地找到方法名称,但是一旦它执行 boolean 检查以检查该方法是否以适当的标记开头,它有时会在 j = 5 处停止。这是代码澄清方法:

public boolean objectWriter(List<Object> input, String sheetName, int startingRow, String tag){
ArrayList<Object> myList = new ArrayList<>();
jxl.write.Number number;
Label label;

//This is just an internal counter since we're using a for-each loop.
int j;
try{
for (int i = 0; i < input.size(); i++){
j = 0;
//we want to iterate over all of the available methods in the given class with reflection
for (Method m: input.get(i).getClass().getMethods()){
//Check to see if the method name has our requested tag, plus the appropriate counter
//tacked on, and ZERO parameters, in our case.
myLog.debug("this is our boolean check: " + m.getName().startsWith((tag + j)));
if (m.getName().startsWith((tag + j))){
myLog.debug("m.getname inside: " + m.getName());
//Invoke the method, give it's return value to r (return)
final Object r = m.invoke(input.get(i));
//Since we defined in the requirements of this class that it must be a string
//those types of methods returned, this works just fine, just case it to
//String (Since String extends object) and call it a day.
if (isNumeric((String)r)){
//if it's a number, make a number object out of it.
number = new jxl.write.Number(j, startingRow + i
, Double.parseDouble((String)r)
, buildNumberFormat((String)r));

myList.add(number);
}else{
label = new Label(j,startingRow + i,(String)r);
myList.add(label);
}
j++;
}
}
}
}catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex){
myLog.error("There was an error working through the point class with reflection.", ex);
return false;
}
boolean successfulWrite = myExcelWriter.writeInformation(myList, sheetName);
myExcelWriter.resizeColumns(18, sheetName);
return successfulWrite;

}

因此,正如您在上面所看到的,它会反射性地在方法名称中查找调用者定义的“标记”以及一个数字。所以如果我有一个类似的方法:

public String get0(){}
public String get1(){}
public String get2(){}

等等。等等,我给这个方法添加了“get”标签,它将按指定的顺序提取所有这三个方法。此外,它是在使用此特定方法的要求中定义的,其返回值全部为字符串,因此我很确定这不是问题。奇怪的是我不明白为什么 if (m.getName().startsWith((tag + j))) 行上的 boolean 检查会在 j=5 时开始失败 有时

无论如何,如果有人有任何想法,我将非常感激。我被困在这里了。

还值得注意的是,对于我提供的数据集,j=5 每次都会成为“else”子句。这对我来说不仅仅是巧合,但我看不出有什么问题。

编辑

如果我在 boolean 检查之前对 'm' 运行操作,它完全按预期工作的概率(提取所有正确枚举的方法等)也会显着增加(接近 99% 的工作),这也是毫无值(value)的。例如我如何在实际语句之前打印出该 boolean 语句?这使得它几乎每次都能工作。但这肯定不是一个解决方案。

编辑#2按照要求,我继续将 println 移至调试日志,这是一个截断的版本,但它本质上重复了 29 次内的模式,也很有趣,似乎如果我将输出发送到记录器而不是控制台,前面提到的成功概率又回到了没有它的情况......奇怪......

09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,881 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet0Label
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet1MD
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet2Easting
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet3Northing
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet4TVD
09:02:45,882 DEBUG [root] this is our boolean check: true
09:02:45,882 DEBUG [root] m.getname inside: xGet5Date
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,882 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,883 DEBUG [root] this is our boolean check: false
09:02:45,884 DEBUG [root] this is our boolean check: false
09:02:45,884 DEBUG [root] this is our boolean check: false
09:02:45,884 DEBUG [root] this is our boolean check: false
09:02:45,884 DEBUG [root] this is our boolean check: false
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet0Label
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet1MD
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet2Easting
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet3Northing
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet4TVD
09:02:45,884 DEBUG [root] this is our boolean check: true
09:02:45,884 DEBUG [root] m.getname inside: xGet5Date

最佳答案

想通了!因此,我处理正在检查的方法的“顺序”的方式存在一个固有的错误。虽然我预计“getMethods()”会以不特定的顺序返回类的方法,但如果其中一个方法以错误的顺序出现,它永远不会被重新检查!只是偶然地,有时按数字顺序排列的第一个方法会出现在后面的方法之前,从而导致正确的行为。大多数情况下,编号方法 0-5 恰好出现在 6-14 之后(这是该数据集中的范围)。

所以,我的解决方案是创建一个被拒绝的方法名称“池”,然后每次检查失败时,迭代拒绝池,并确保其中一个也不符合要求。如果不是这些,那么它就不是我们关心的方法。

这会稍微减慢速度,但在毫秒范围内。

解决方案代码:

public boolean objectWriter(List<Object> input, String sheetName, int startingRow, String tag){
ArrayList<Object> myList = new ArrayList<>();
ArrayList<Method> methodList = new ArrayList<>();
jxl.write.Number number;
Label label;

//This is just an internal counter since we're using a for-each loop.
int j;
try{
for (int i = 0; i < input.size(); i++){
j = 0;
//we want to iterate over all of the available methods in the given class with reflection
for (Method m: input.get(i).getClass().getDeclaredMethods()){
//Check to see if the method name has our requested tag, plus the appropriate counter
//tacked on, and ZERO parameters, in our case.
if (m.getName().startsWith((tag + j))){
//Invoke the method, give it's return value to r (return)
final Object r = m.invoke(input.get(i));
//Since we defined in the requirements of this class that it must be a string
//those types of methods returned, this works just fine, just case it to
//String (Since String extends object) and call it a day.
if (isNumeric((String)r)){
//if it's a number, make a number object out of it.
number = new jxl.write.Number(j, startingRow + i
, Double.parseDouble((String)r)
, buildNumberFormat((String)r));

myList.add(number);
}else{
label = new Label(j,startingRow + i,(String)r);
myList.add(label);
}
j++;
}else{
methodList.add(m);
for (int x = 0; x < methodList.size(); x++){
if (methodList.get(x).getName().startsWith((tag + j))){
//Invoke the method, give it's return value to r (return)
final Object r = methodList.get(x).invoke(input.get(i));
//Since we defined in the requirements of this class that it must be a string
//those types of methods returned, this works just fine, just case it to
//String (Since String extends object) and call it a day.
if (isNumeric((String)r)){
//if it's a number, make a number object out of it.
number = new jxl.write.Number(j, startingRow + i
, Double.parseDouble((String)r)
, buildNumberFormat((String)r));

myList.add(number);
}else{
label = new Label(j,startingRow + i,(String)r);
myList.add(label);
}
j++;
//methodList.remove(x);
break;
}
}
}
}
}
}catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex){
myLog.error("There was an error working through the point class with reflection.", ex);
return false;
}
boolean successfulWrite = myExcelWriter.writeInformation(myList, sheetName);
myExcelWriter.resizeColumns(18, sheetName);
return successfulWrite;

}

这是一个很好的教训,当您尝试对本质上无序的列表强加顺序时,请确保您小心,因为如果您把它搞砸了(像我一样!),它会感觉像是非常未定义的行为。

关于java - 诡异的 Java 反射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com