codeconsole commented on code in PR #16180:
URL: https://github.com/apache/grails-core/pull/16180#discussion_r4367554306


##########
grails-core/src/main/groovy/org/apache/grails/core/aot/BytecodeRuntimeHints.java:
##########
@@ -0,0 +1,492 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.grails.core.aot;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import groovy.lang.GroovyObject;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jspecify.annotations.Nullable;
+
+import org.springframework.aot.hint.ExecutableMode;
+import org.springframework.aot.hint.MemberCategory;
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.RuntimeHintsRegistrar;
+import org.springframework.aot.hint.TypeReference;
+import org.springframework.asm.AnnotationVisitor;
+import org.springframework.asm.ClassReader;
+import org.springframework.asm.ClassVisitor;
+import org.springframework.asm.FieldVisitor;
+import org.springframework.asm.Handle;
+import org.springframework.asm.Label;
+import org.springframework.asm.MethodVisitor;
+import org.springframework.asm.SpringAsmInfo;
+import org.springframework.asm.Type;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.io.support.ResourcePatternResolver;
+import org.springframework.util.ClassUtils;
+
+import org.apache.grails.common.aot.RegistrableTypes;
+
+/**
+ * Registers the reflection the framework's own compiled classes imply, read 
from their bytecode.
+ *
+ * <p>Some of what a class does at run time is answered by reflection however 
it was compiled, and an image
+ * answers none of it unless it was asked to:</p>
+ *
+ * <ul>
+ * <li>A Groovy enum creates its constants by choosing a constructor through 
Groovy rather than by calling
+ * one, so its constructors have to be visible. {@code 
grails.util.Environment} is one, and it is the first
+ * thing an application touches: without this an image fails before Spring has 
started, with "Could not
+ * find matching constructor".</li>
+ * <li>A closure turned into an interface -- passed where a method takes one, 
such as {@code removeIf},
+ * {@code forEach} or {@code computeIfAbsent}, or coerced with {@code as}, as 
a plugin's
+ * {@code BeanRegistrar} often is -- becomes it through a JDK proxy created as 
the call is made. An image
+ * creates a proxy only for an interface it was told about, and refuses the 
rest with
+ * {@code MissingReflectionRegistrationError}. Only a coercion that names the 
interface is found: any in
+ * statically compiled code, and an {@code as} with a class literal in 
dynamically compiled code. A closure

Review Comment:
   I went with the second option, one step wider. On Groovy 6.0.0 I compiled 
each receiver shape in a dynamic and a `@CompileStatic` class. Every `as` there 
is an indy call to `asType`, since statically compiled code doesn't cast for 
`as` either, and the receiver in the descriptor is whatever the compiler knows:
   
   | `as` on | dynamic | static | JDK proxy at run time |
   |---|---|---|---|
   | closure literal, `Closure` variable | `(Closure, Class)` | `(Closure, 
Class)` | yes |
   | `def` variable, method result | `(Object, Class)` | `(Closure, Class)` | 
yes |
   | map literal | `(Map, Class)` | `(Map, Class)` | yes |
   
   Accepting `Object` alone would still miss a map literal, whose receiver is 
`Map` in both. The framework has one: `ConfigProperties.propertyNames()` 
coerces a map of closures `as Enumeration`, and that had no proxy hint.
   
   4395d67e6d accepts any receiver for an `asType` call right after a class 
literal naming an interface. The spec is now data-driven over the seven forms 
above. Each row runs its fixture, asserts the result is a JDK proxy, and 
asserts the hint. The four rows the old visitor missed (`def`, method result, 
both map literals) fail against it, as does a new feature for 
`ConfigProperties.propertyNames()`.
   
   The cost, measured on native-i18n's runtime classpath: the wider rule adds 7 
interfaces. Only `Enumeration` is a real proxy. The other six are never proxied:
   
   - `Collection`, `List`, `Map` and `Set`, from collection conversions;
   - `Promise`, from a `Future` that `CachedThreadPoolPromiseFactory` coerces;
   - `Runnable`, from `EmbeddedDatabaseShutdownHook`. In 
`ShutdownOperations.addOperation { … } as Runnable`, the `as` binds to the void 
call's result, and the closure is a `Runnable` already.
   
   Accepting any receiver also finds `value as SomeSealedInterface`. The JDK 
refuses to proxy a sealed interface, so bb2940f68e skips those, with a fixture 
that fails without the check.
   
   The javadoc and the guide bullet at line 118 now say a closure or a map is 
found wherever any code names the interface with `as`, whatever it declares the 
value as. The javadoc also says an `as` that converts without a proxy costs an 
unused hint. The description says the same.
   



-- 
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]

Reply via email to