[英]Access path variable in FileInputStream using reflection
我遇到了一些使用反射來獲取FileInputStream中“ path”變量的代碼! 更奇怪的部分是commons.lang.reflect.FieldUtils.readField(is, "path", true)
拋出java.lang.IllegalArgumentException
。 太斷斷續續了……可能是某些特定情況導致的,但我無法弄清楚。 關於什么可能導致反射無法“看到”該字段的任何指針?
該代碼已埋入許多與Mumbo jumbo相關的級聯工作流中,但這是相關的部分-
//This same method is invoked multiple times on same file. If it matters?
method(SourceCall<Object, InputStream> sc) {
InputStream is = sc.getInput();
if (is instanceof FileInputStream) {
FileInputStream fileInputStream = (FileInputStream)is;
//The line below throws IllegalArgumentException - sometimes
String fileName = (String)FieldUtils.readField(fileInputStream , "path", true);
return fileName;
}
}
您應該使用FieldUtils.readDeclaredField(...)
來訪問私有字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.