The problem is not that reflection "doesn't work"; it is that reflection is finally enforcing more of the accessibility model that the compiler and runtime have always enforced. URLClassLoader::addUrl
is intended only for subclasses; it is not intended to be accessed from outside the implementation, which is what you're doing. Over time, starting with Java 9 and continuing in later versions (including 17), access restrictions are increasingly recognized by reflection, with warnings, to give broken code a chance to migrate to something supportable.
The code in question only really ever worked accidentally; it depended on being able to break into an unsupported interface. Using setAccessible
should be a clue. Sure, you can get into locked houses by breaking windows, but if you have to break a window (and it’s not your house), you should be aware of where the problem lies.
Look at it as glass-half-full; this accidentally-working code worked for a long time. But the bill has come due; it is time to fix your code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…