我遇到了一些代碼,它使用反射來獲取FileInputStream中的「路徑」變量! Weirder部分是commons.lang.reflect.FieldUtils.readField(is, "path", true)
拋出java.lang.IllegalArgumentException
。這也是間歇性的......可能是某種特定的情況導致的,但我無法弄清楚。任何關於什麼可能導致反射無法「看見」字段的指針?使用反射在FileInputStream中訪問路徑變量
的代碼被埋葬在很多級聯的工作流程相關的繁文縟節,但這裏是有關的部分 -
//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;
}
}
爲什麼你需要FileInputStream的路徑?根據以下來源,FileInputSteam不包含_path_字段,如果「如果...字段名稱爲空或空或無法找到」,則readField將拋出IllegalArgumentException:http://www.docjar.com/html /api/java/io/FileInputStream.java.html – copeg
我正在盯着java.io.FileInputStream的1.7.0_60_64版本,它有以下內容 - private final String path;看起來fileName是從路徑變量中提取出來的,並在下游使用。繼續研究級聯API部分以弄清楚爲什麼它必須是這種方式! – user2023507
同樣,爲什麼你需要這條路?反思似乎是一個不穩定的方式去做。 – copeg