IllegalAccessError when loading a class generated with a NamingStrategy
See original GitHub issueI have a simple scenario where I receive an IllegalAccessError when loading a generated class that uses a NamingStrategy.
java.lang.IllegalAccessError: class FooBar.Foo cannot access its superclass org.modelmapper.FooTest$Foo
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at net.bytebuddy.dynamic.loading.ByteArrayClassLoader.findClass(ByteArrayClassLoader.java:48)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at net.bytebuddy.dynamic.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:59)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:1132)
at org.modelmapper.FooTest.proxyClassFor(FooTest.java:35)
at org.modelmapper.FooTest.test(FooTest.java:40)
Here’s the complete test:
@Test
public class FooTest {
static class Foo {
}
public void test() {
new ByteBuddy().withNamingStrategy(new NamingStrategy() {
public String getName(UnnamedType unnamedType) {
return "FooBar." + unnamedType.getSuperClass().getSimpleName();
}
})
.subclass(Foo.class)
.method(MethodMatchers.any())
.intercept(InvocationHandlerAdapter.of(new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
}))
.make()
.load(FooTest.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
}
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Two java files. Getting IllegalAccessError when running class ...
IllegalAccessError : class TapeDeckTestDrive tried to access method TestDrive.playTape()V (TapeDeckTestDrive is in unnamed module of loader com.
Read more >Strange IllegalAccessError with JBoss + Hibernate
Hi guys, I'm getting the following error when loading up my application on JBoss 4.0.5: java.lang.IllegalAccessError: tried to access method ...
Read more >IllegalAccessError in Java - Baeldung
An IllegalAccessError is thrown when an application attempts to access a field or invoke a method that is inaccessible.
Read more >[wicket-ioc] LazyInitProxyFactory CGLIB proxies naming ...
A CGLib proxy created by LazyInitProxyFactory may cause java.lang.IllegalAccessError when the target class has "package-private" method.
Read more >IllegalAccessError @ buildSessionFactory using Annotations
IllegalAccessError when I use an AnnotatedConfiguration instead of a Configuration which ... This class is generated by eclipse-rcp wizard.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Java is a quite tricky language when it comes to package-private access. Package-private access is also rather an invention for being used by compilers rather then by users since they have strange semantics.
You might have heard before that two classes are only consider equal by the Java run time if they were loaded with the same class loader even if they otherwise represent an identical class. The same is true for Java packages. If a class is loaded with a different class loader, it cannot get private-package access to any classes (or class members) that belong to another class loader.
The injection strategy uses reflection to load a class into a given class loader. The wrapper class loader on the other hand, creates a new child class loader for loading a given class. The former strategy is more efficient since a class loader is a rather expensive object to Java (takes about 70 nanoseconds to create it in my benchmarks). However, it does not need to call internal methods. Also, the wrapper class loader allows the unloading of the generated class if none of its instances and its class and class loader are not longer reachable. This is handy when creating classes that are only needed for a limited amount of time such as in bootstrap procedures. One specific use case of this strategy would be mocking for example as for creating type safe DSLs. This allows to keep the perm gen requirements of an application down as the injector strategy often targets the system class loader what makes loaded classes invincible for the application’s life time.
The injector strategy is equivalent to Javassist and cglib’s approaches.
I thought about predefining such a strategy for Byte Buddy 0.2. However, this would require you to resolve any invokable method of a class in order to check if any of those methods is package-private relatively to the generated class in order to determine its necessity. Such reflective look-ups are unfortunately expensive. I might still just add the strategy and add an explicit warning of its costs.