jdaugherty commented on code in PR #16180: URL: https://github.com/apache/grails-core/pull/16180#discussion_r4364847262
########## 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: Checked what `as` compiles to in a dynamically compiled class on Groovy 6.0.0. The receiver's static type matters as much as the class literal: the call site's descriptor says `Closure` only when the compiler knows the receiver is one. ``` { int o -> o * 2 } as IntUnaryOperator ldc IntUnaryOperator; invokedynamic invoke:(Lgroovy/lang/Closure;Ljava/lang/Class;) found Closure c = { ... }; c as IntUnaryOperator the same found def c = { ... }; c as IntUnaryOperator ldc IntUnaryOperator; invokedynamic invoke:(Ljava/lang/Object;Ljava/lang/Class;) not found make() as IntUnaryOperator the same, an Object receiver not found ``` The last two still make a JDK proxy at run time, and this sentence, the guide bullet at line 118 of `deploymentAot.adoc` ("wherever dynamically compiled code names the interface with `as`") and the description all read as if naming the interface is enough. Either way of closing that works for me: - say the closure has to be a closure literal or a `Closure`-typed expression, here and in the guide bullet; or - accept an `Object` receiver for `asType` as well. Groovy proxies a `Map` coerced to an interface the same way, so that case is right too; the cost is a proxy hint for an `as Set`, `as List` or `as Map` in dynamic code, which the image never uses. Not blocking: the framework's own `as BeanRegistrar` coercions are closure literals, which `DynamicCoercionFixture` covers. -- 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]
