This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
I'm currently using EHCache to cache my data in Java. The problem is after creating a cache and adding it to the cache manager, I'm not able to use the cache since it has not been initialised. The concept is that the initialise() method in the class Cache initialises the MemoryStore and DiskStore for the cache by calling the methods in class MemoryStore and class DiskStore. The problem is that the specifiers for these methods is default and are not available in my package and hence the IDE says the method is not visible. Its a catch-22 situation sinceI need to initialise my cache because I cannot use it without initialising it. Someone please advice on a possible work around to this problem. For info, the documentation is available on http://ehcache.sourceforge.net. Thanks in advance.
Dept of Electrical and Comp Engg
University of Massachusetts, Amherst
I am not very sure but I think with Java Reflection you can basically call any method no matter private or default or whatever the access modifier is. I know it is ugly idea but maybe it could do the job.
Another solution is also possible. If that EHCache product is free and open source you can just patch the binary distribution by changing these access modifiers in the appropriate source file and then just recompile the source.
Then put the recompiled version of your patched class before the EHCache distribution (probably it's some JAR I guess) in your classpath and that's it. I do not think this is any violation of their licence as you do not redestribute the original EHCache product.
Hope one of these two ideas could help.
Regards. [ March 09, 2006: Message edited by: Peter Petrov ]
I'm pretty sure reflection all by itself doesn't give you any powers that you don't normally have. Reflection will tell you the structure of things known to the class loader, but that doesn't cause the JVM to execute code or access data you aren't allowed to. If this weren't the case, anybody could write an applet to hack any browser just by using reflection to bypass the security manager.
Seems Reid M. Pinchback is right about the reflection.
Maybe in the previous versions of Java (before 1.4) there was such option to call methods which you normally have no access to via Java Reflection, I don't know.
I don't remember why I have left with that impression. Maybe I've had a slightly different scenario.
The second solution though which I offered above should work as I have had the same problem many times before. Just take the source files you want to change, change the access modifiers from default to public, then recompile only those source files and put them somewhere in the classpath before the normal/original library class (jar) files of EHCache.
This should do the job. And yes, I agree this is not a performance question
Regards. [ March 10, 2006: Message edited by: Peter Petrov ]
You can use reflection to gain access to private members, but it will throw a SecurityAccessViolationException or something similar if done in applets i.e.
In general, I would rarely use such a technique to make things work. Either you don't understand how the api is intended to be used, or the developer did a bad job, in which case it might not be a good idea to use it at all.
Asking at sourceforge would be my answer too in this case.