blackdrag commented on PR #3109: URL: https://github.com/apache/groovy/pull/3109#issuecomment-6834104147
The main concern I have with this approach is that it still infers that a class cannot be loaded from the absence of a class resource and the absence of an entry in a `GroovyClassLoader` cache. Neither establishes that a class loader cannot provide the class. `ClassLoader.loadClass` is not restricted to classes backed by resources. A parent loader may already have a class defined, generate it on demand, or otherwise make it available without exposing a `.class` resource. This is particularly relevant here because Groovy supports custom class loaders and has existing code paths that deliberately use `loadClass` to resolve classes that exist only in memory. There are two places where this assumption affects behavior: * **`GroovyClassLoader`:** `throwIfParentWouldOnlyRecordAMiss` short-circuits parent delegation for names containing `$` (except the special `java.lang.$` case) when no resource or cached class is found. This can turn a successful parent lookup into a `ClassNotFoundException`. The fact that the name resembles an inner class does not establish that it is a generated name that the parent cannot resolve. * **`MetaClassRegistry`:** `customMetaClassVisible` similarly skips `Class.forName` when no ancestor `GroovyClassLoader` cache entry or class resource is found. This can prevent a custom MetaClass supplied by a resource-less class loader from being discovered. The added test covers a MetaClass defined directly in the registry loader, but does not exercise this case through a parent loader. I think the optimization should be based on **positive knowledge about the name being probed**, rather than an inference that the name cannot be resolved. In particular, we should avoid delegation only for a generated companion name that it knows belongs to a script or template and that cannot be supplied by the relevant parent lookup path. All other names should continue through the existing class-loading mechanism. The same principle should apply to the custom MetaClass lookup: identify the specific generated-script companion probes that are safe to skip, rather than treating a missing resource as proof that a custom MetaClass is unavailable. This would also avoid having the optimization depend on increasingly elaborate approximations of what arbitrary class loaders can load. The class loader's own resolution mechanism should remain authoritative unless Groovy has specific knowledge that a particular generated lookup is unnecessary. I would add regression tests for both cases: - A parent class loader that can provide a class without exposing a `.class` resource, where the requested name contains `$`. - A custom MetaClass available through a parent class loader but without a corresponding `.class` resource. The existing tests demonstrate the retention problem and cover several ordinary lookup cases, but they do not establish that these short-circuits preserve the existing class-loader contract. -- 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]
