简体   繁体   English

使用反射访问FileInputStream中的路径变量

[英]Access path variable in FileInputStream using reflection

I came across some code that uses reflection to get to the "path" variable in the FileInputStream!! 我遇到了一些使用反射来获取FileInputStream中“ path”变量的代码! Weirder part is that commons.lang.reflect.FieldUtils.readField(is, "path", true) throws a java.lang.IllegalArgumentException . 更奇怪的部分是commons.lang.reflect.FieldUtils.readField(is, "path", true)抛出java.lang.IllegalArgumentException That too intermittently... May be some specific scenario leads to that, but I am not able to figure out. 太断断续续了……可能是某些特定情况导致的,但我无法弄清楚。 Any pointers as to what could cause reflection to fail to "see" the Field? 关于什么可能导致反射无法“看到”该字段的任何指针?

The code is buried in lot of Cascading workflows related mumbo jumbo but here is the relevant part - 该代码已埋入许多与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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM