blackdrag commented on code in PR #3109:
URL: https://github.com/apache/groovy/pull/3109#discussion_r4505584630
##########
src/main/java/groovy/lang/MetaClassRegistry.java:
##########
@@ -161,6 +169,41 @@ private MetaClass createWithCustomLookup(Class theClass,
MetaClassRegistry regis
}
}
+ private static boolean customMetaClassVisible(String customName) {
+ ClassLoader loader = MetaClassRegistry.class.getClassLoader();
+ if (loader == null) {
+ return true;
+ }
+ for (ClassLoader current = loader; current != null; current =
current.getParent()) {
+ if (current instanceof GroovyClassLoader
+ && ((GroovyClassLoader)
current).getClassCacheEntry(customName) != null) {
+ return true;
+ }
+ }
+ return classResourcePresent(loader, customName);
+ }
+
+ /**
+ * True when {@code className} is visible as a class file. A native
image
+ * with exact reachability metadata throws if that resource was not
+ * registered; the class may still be loadable, so the caller falls
+ * through to {@code Class.forName}.
+ */
+ private static boolean classResourcePresent(ClassLoader loader, String
className) {
+ try {
+ return loader.getResource(className.replace('.', '/') +
".class") != null;
+ } catch (Error error) {
+ if (isMissingResourceRegistration(error)) {
Review Comment:
why do we return true here?
##########
src/main/java/groovy/lang/MetaClassRegistry.java:
##########
@@ -143,7 +143,15 @@ public final MetaClass create(Class theClass,
MetaClassRegistry registry) {
private MetaClass createWithCustomLookup(Class theClass,
MetaClassRegistry registry) {
try {
- final Class customMetaClass =
Class.forName("groovy.runtime.metaclass." + theClass.getName() + "MetaClass");
+ final String customName = "groovy.runtime.metaclass." +
theClass.getName() + "MetaClass";
+ // Class.forName records a missing name in every
parallel-capable
+ // loader it asks, and the entry is never removed. The lookup
can
+ // only succeed for a class already defined by this runtime's
+ // loader or present as a classpath resource.
+ if (!customMetaClassVisible(customName)) {
Review Comment:
the comment does not really explain why the check is needed. What if the
custom meta class is available as class, but not as classpath resource?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]