Fix named instantiator leaking memory
This commit reworks the named object instantiator so that it doesn't
leak memory, by locating the cache on the `VisibleURLClassLoader`
instance whenever possible. Previously, it retained strong references
to classes, meaning that any class loaded via a script classloader
would be strongly referenced and would prevent the loader from being
collected. With this change, the cache is now located on the loader
itself.
If, for some reason, the loader is not a known classloader, we will
still use a global cache, but this shouldn't leak memory anymore
since in this case it's likely the affected classes come from Gradle
core itself.
Fixes #8142