On 20/01/2018 00:07, Jeremy Manson wrote:
:
The other place I saw that this came up is the cglib code generation
library, which now uses Unsafe to access ClassLoader.defineClass (for
any ClassLoader). This is useful for generating classes for
dependency injection / mocking / that kind of thing. It is a
violation of encapsulation, but it is done with a very specific
software engineering goal in mind. It was never a terribly good
approach, but it a library that is now depended upon by a very large
amount of code in the wild.
DI and other cases with frameworks doing code injection should be
looking at Lookup.defineClass. Frameworks can use this API to inject a
class into an existing runtime package once they have a Lookup object
with the appropriate access. it's not all use-cases but it's the only
supported API in the platform for doing this kind of thing.
-Alan