I want to get reflective access to the private package constructor java.lang.String.
Namely, this one:
String(char[] value, boolean share) {
Creating a Handle method for it is quite simple, and therefore calls it. The same is true for direct use of Reflection.
But I'm curious if the constructor can be called directly through the functional interfaces.
27602758 addresses a somewhat similar issue, but the solutions provided do not seem to work in this case.
The test case below compiles without problems. Everything works, except for the actual interface call.
package test; import java.lang.invoke.CallSite; import java.lang.invoke.LambdaMetafactory; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import java.lang.reflect.Field; public class Test {
In particular, the instruction
shared.create("foo".toCharArray(), true)
produces the following error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method java.lang.String.<init>([CZ)V from class test.Test$$Lambda$2/989110044 at test.Test.main(Test.java:59)
Why is this error still thrown, despite the fact that access is supposedly granted?
Can someone explain why the generated interface does not have access to the method, which all its components have access to?
Is there a solution or a viable alternative that really works for this particular use case without returning to pure Reflection or MethodHandles?
Because I'm at a standstill.