- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,问题来了。我试图在 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/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!