I came across this class java.io.FileSystem and noticed it has many methods that I currently need in my project. However the class is package-private, and so I am accessing the needed methods using reflection.
Questions:
- Is there any particular reason why this class is marked package private?
- Are there any dangers of accessing it via reflection? (Other than performance hit, that is.)
This class is package private because SUN (and by extension, Oracle) believe that the methods of this platform-dependent class are likely to undergo significant change in the future, and therefore must not be accessible directly. All implementations of this abstract class are in native code; Java programmers should not be able to create their own.
The biggest danger of using a hidden class through reflection is not the performance, but a very real possibility that its methods or even the entire class would disappear in the next upgrade of the JDK, no matter how minor. Non-public APIs are, well, non-public; changing them is fair game even in a maintenance release, so you have only yourself to blame if your program stops working after what seemed like a routine JDK update.