I'm getting strange reflection or access errors.

An example of this kind of error is:

java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor1 cannot access its superclass sun.reflect.ConstructorAccessorImpl (Caused by java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor1 cannot access its superclass sun.reflect.ConstructorAccessorImpl)
	at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
	at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:370)
	at us.blanshard.stubout.tests.TestClassReplacement$TestLogging.test(TestClassReplacement.java:131)
	at us.blanshard.stubout.StubbingTestCase.runBareTest(StubbingTestCase.java:30)
	at us.blanshard.stubout.Stubber.reloadAndRun(Stubber.java:305)
	at us.blanshard.stubout.StubbingTestCase.runBare(StubbingTestCase.java:25)
	at sun.misc.Unsafe.defineClass(Native Method)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
	... 23 more
                   

To solve these errors, add calls to Stubber.excludePackage to your beforeReloading method. In this case, the easiest way to solve it is to exclude the "sun" package. See the end of TestClassReplacement (the TestLogging nested class) for an example.

[top]

If the "sun" package is problematic, why not automatically exclude it from stubbing?

This is a tough call. StubOut does not require this package to be excluded from stubbing; many stubbing unit tests run just fine without excluding it. On the other hand, this particular bug does bite people fairly often and it would help them not to even have to know about it.

I've decided not to automatically exclude this package for now because it would violate my instinct to keep such restrictions to a minimum. The bug that StubOut sometimes gets caught by is an implementation bug in Sun's JVM. Ideally, they will fix it and it will stop being an issue.

Furthermore, the "sun" package is not the only thing you may find yourself needing to exclude. Code that manipulates class loaders, for example the Apache Commons Logging library, also may need to be excluded from stubbing.

However, I am ambivalent about this decision and may revisit it in a later release.

[top]