diff --git a/spring-core-test/spring-core-test.gradle b/spring-core-test/spring-core-test.gradle
index 04b6953d4d7..264bc8f7ea8 100644
--- a/spring-core-test/spring-core-test.gradle
+++ b/spring-core-test/spring-core-test.gradle
@@ -2,9 +2,19 @@ description = "Spring Core Test"
dependencies {
api(project(":spring-core"))
+ api("org.junit.jupiter:junit-jupiter-api")
api("org.assertj:assertj-core")
api("com.thoughtworks.qdox:qdox")
compileOnly("org.junit.jupiter:junit-jupiter")
compileOnly("org.junit.platform:junit-platform-engine")
compileOnly("org.junit.platform:junit-platform-launcher")
}
+
+jar {
+ manifest {
+ attributes(
+ 'Premain-Class': 'org.springframework.aot.agent.RuntimeHintsAgent',
+ 'Can-Redefine-Classes': 'true'
+ )
+ }
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/HintType.java b/spring-core-test/src/main/java/org/springframework/aot/agent/HintType.java
new file mode 100644
index 00000000000..09df5b4753f
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/HintType.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+
+import org.springframework.aot.hint.ClassProxyHint;
+import org.springframework.aot.hint.JavaSerializationHint;
+import org.springframework.aot.hint.JdkProxyHint;
+import org.springframework.aot.hint.ReflectionHints;
+import org.springframework.aot.hint.ResourceBundleHint;
+import org.springframework.aot.hint.ResourcePatternHint;
+
+/**
+ * Main types of {@link org.springframework.aot.hint.RuntimeHints}.
+ *
This allows to sort {@link RecordedInvocation recorded invocations}
+ * into hint categories.
+ *
+ * @author Brian Clozel
+ * @since 6.0
+ */
+public enum HintType {
+
+ /**
+ * Reflection hint, as described by {@link org.springframework.aot.hint.ReflectionHints}.
+ */
+ REFLECTION(ReflectionHints.class),
+
+ /**
+ * Resource pattern hint, as described by {@link org.springframework.aot.hint.ResourceHints#resourcePatterns()}.
+ */
+ RESOURCE_PATTERN(ResourcePatternHint.class),
+
+ /**
+ * Resource bundle hint, as described by {@link org.springframework.aot.hint.ResourceHints#resourceBundles()}.
+ */
+ RESOURCE_BUNDLE(ResourceBundleHint.class),
+
+ /**
+ * Java serialization hint, as described by {@link org.springframework.aot.hint.JavaSerializationHint}.
+ */
+ JAVA_SERIALIZATION(JavaSerializationHint.class),
+
+ /**
+ * JDK proxies hint, as described by {@link org.springframework.aot.hint.ProxyHints#jdkProxies()}.
+ */
+ JDK_PROXIES(JdkProxyHint.class),
+
+ /**
+ * Class proxies hint, as described by {@link org.springframework.aot.hint.ProxyHints#classProxies()}.
+ */
+ CLASS_PROXIES(ClassProxyHint.class);
+
+ private final Class> hintClass;
+
+ HintType(Class> hintClass) {
+ this.hintClass = hintClass;
+ }
+
+ public String hintClassName() {
+ return this.hintClass.getSimpleName();
+ }
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedBridgeMethods.java b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedBridgeMethods.java
new file mode 100644
index 00000000000..89c6168157a
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedBridgeMethods.java
@@ -0,0 +1,527 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Proxy;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.stream.Stream;
+
+import org.springframework.lang.Nullable;
+
+
+/**
+ * Instrumented version of JDK methods to be used by bytecode rewritten by the {@link RuntimeHintsAgent}.
+ *
Methods implemented here follow a specific naming pattern "lowercase type name + bridged method name",
+ * so that the agent can consistently rewrite calls to instrumented methods.
+ * For example {@code Class#forName(String)} will be here names {@code classforName(String)}.
+ *
+ * @author Brian Clozel
+ * @see InstrumentedMethod
+ * @deprecated This class should only be used by the runtime-hints agent when instrumenting bytecode
+ * and is not considered public API.
+ */
+@Deprecated
+public abstract class InstrumentedBridgeMethods {
+
+ private InstrumentedBridgeMethods() {
+
+ }
+
+ /*
+ * Bridge methods for java.lang.Class
+ */
+
+ public static Class> classforName(String className) throws ClassNotFoundException {
+ Class> result = null;
+ try {
+ result = Class.forName(className);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_FORNAME).withArguments(className).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Class> classforName(String className, boolean initialize, ClassLoader loader) throws ClassNotFoundException {
+ Class> result = null;
+ try {
+ result = Class.forName(className, initialize, loader);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_FORNAME).withArguments(className, initialize, loader).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Constructor>[] classgetConstructors(Class> clazz) throws SecurityException {
+ Constructor>[] result = null;
+ try {
+ result = clazz.getConstructors();
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(RecordedInvocation.of(InstrumentedMethod.CLASS_GETCONSTRUCTORS).onInstance(clazz).returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static Constructor> classgetConstructor(Class> clazz, Class>[] parameterTypes) throws NoSuchMethodException {
+ Constructor> result = null;
+ try {
+ result = clazz.getConstructor(parameterTypes);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETCONSTRUCTOR)
+ .onInstance(clazz).withArgument(parameterTypes).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Constructor>[] classgetDeclaredConstructors(Class> clazz) throws SecurityException {
+ Constructor>[] result = null;
+ try {
+ result = clazz.getDeclaredConstructors();
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS).onInstance(clazz).returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static Constructor> classgetDeclaredConstructor(Class> clazz, Class>[] parameterTypes) throws NoSuchMethodException {
+ Constructor> result = null;
+ try {
+ result = clazz.getDeclaredConstructor(parameterTypes);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR)
+ .onInstance(clazz).withArgument(parameterTypes).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Method[] classgetMethods(Class> clazz) throws SecurityException {
+ Method[] result = null;
+ try {
+ result = clazz.getMethods();
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS)
+ .onInstance(clazz).returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static Method classgetMethod(Class> clazz, String name, Class>... parameterTypes) throws NoSuchMethodException, SecurityException {
+ Method result = null;
+ try {
+ result = clazz.getMethod(name, parameterTypes);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHOD)
+ .onInstance(clazz).withArguments(name, parameterTypes).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Method classgetDeclaredMethod(Class> clazz, String name, Class>... params)
+ throws SecurityException, NoSuchMethodException {
+ Method result = null;
+ try {
+ result = clazz.getDeclaredMethod(name, params);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDMETHOD)
+ .onInstance(clazz).withArguments(name, params).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Method[] classgetDeclaredMethods(Class> clazz) {
+ Method[] result = clazz.getDeclaredMethods();
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDMETHODS)
+ .onInstance(clazz).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Class>[] classgetDeclaredClasses(Class> clazz) {
+ Class>[] result = clazz.getDeclaredClasses();
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDCLASSES)
+ .onInstance(clazz).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Class>[] classgetClasses(Class> clazz) {
+ Class>[] result = clazz.getClasses();
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETCLASSES)
+ .onInstance(clazz).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Field[] classgetDeclaredFields(Class> clazz) {
+ Field[] result = clazz.getDeclaredFields();
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELDS)
+ .onInstance(clazz).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Field classgetDeclaredField(Class> clazz, String name) throws NoSuchFieldException {
+ Field result = null;
+ try {
+ result = clazz.getDeclaredField(name);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELD)
+ .onInstance(clazz).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ public static Field[] classgetFields(Class> clazz) {
+ Field[] result = clazz.getFields();
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELDS)
+ .onInstance(clazz).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Field classgetField(Class> clazz, String name) throws NoSuchFieldException {
+ Field result = null;
+ try {
+ result = clazz.getField(name);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD)
+ .onInstance(clazz).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ @Nullable
+ public static URL classgetResource(Class> clazz, String name) {
+ URL result = clazz.getResource(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCE)
+ .onInstance(clazz).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ @Nullable
+ public static InputStream classgetResourceAsStream(Class> clazz, String name) {
+ InputStream result = clazz.getResourceAsStream(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETRESOURCEASSTREAM)
+ .onInstance(clazz).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ /*
+ * Bridge methods for java.lang.ClassLoader
+ */
+
+ public static Class> classloaderloadClass(ClassLoader classLoader, String name) throws ClassNotFoundException {
+ Class> result = null;
+ try {
+ result = classLoader.loadClass(name);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_LOADCLASS)
+ .onInstance(classLoader).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ @Nullable
+ public static URL classloadergetResource(ClassLoader classLoader, String name) {
+ URL result = classLoader.getResource(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_GETRESOURCE)
+ .onInstance(classLoader).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ @Nullable
+ public static InputStream classloadergetResourceAsStream(ClassLoader classLoader, String name) {
+ InputStream result = classLoader.getResourceAsStream(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_GETRESOURCEASSTREAM)
+ .onInstance(classLoader).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Stream classloaderresources(ClassLoader classLoader, String name) {
+ Stream result = classLoader.resources(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_RESOURCES)
+ .onInstance(classLoader).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ public static Enumeration classloadergetResources(ClassLoader classLoader, String name) throws IOException {
+ Enumeration result = null;
+ try {
+ result = classLoader.getResources(name);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_GETRESOURCES)
+ .onInstance(classLoader).withArgument(name).returnValue(result).build());
+ }
+ return result;
+ }
+
+ /*
+ * Bridge methods for java.lang.Constructor
+ */
+ public static Object constructornewInstance(Constructor> constructor, Object... arguments) throws InvocationTargetException, InstantiationException, IllegalAccessException {
+ Object result = null;
+ boolean accessibilityChanged = false;
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE)
+ .onInstance(constructor).withArguments(arguments);
+ try {
+ if (!Modifier.isPublic(constructor.getModifiers()) ||
+ !Modifier.isPublic(constructor.getDeclaringClass().getModifiers()) || !constructor.canAccess(null)) {
+ constructor.setAccessible(true);
+ accessibilityChanged = true;
+ }
+ result = constructor.newInstance(arguments);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ if (accessibilityChanged) {
+ constructor.setAccessible(false);
+ }
+ }
+ return result;
+ }
+
+ /*
+ * Bridge methods for java.lang.reflect.Method
+ */
+
+ public static Object methodinvoke(Method method, Object object, Object... arguments) throws InvocationTargetException, IllegalAccessException {
+ Object result = null;
+ try {
+ result = method.invoke(object, arguments);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
+ .onInstance(method).withArguments(object, arguments).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ }
+ return result;
+ }
+
+ /*
+ * Bridge methods for java.lang.reflect.Field
+ */
+
+ public static Object fieldget(Field field, Object object) throws IllegalArgumentException, IllegalAccessException {
+ Object result = null;
+ boolean accessibilityChanged = false;
+ try {
+ if ((!Modifier.isPublic(field.getModifiers()) ||
+ !Modifier.isPublic(field.getDeclaringClass().getModifiers())) && !field.canAccess(object)) {
+ field.setAccessible(true);
+ accessibilityChanged = true;
+ }
+ result = field.get(object);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.FIELD_GET)
+ .onInstance(field).withArguments(object).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ if (accessibilityChanged) {
+ field.setAccessible(false);
+ }
+ }
+ return result;
+ }
+
+ public static void fieldset(Field field, Object object, Object value) throws IllegalArgumentException, IllegalAccessException {
+ boolean accessibilityChanged = false;
+ try {
+ if ((!Modifier.isPublic(field.getModifiers()) ||
+ !Modifier.isPublic(field.getDeclaringClass().getModifiers())) && !field.canAccess(object)) {
+ field.setAccessible(true);
+ accessibilityChanged = true;
+ }
+ field.set(object, value);
+ }
+ finally {
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.FIELD_SET)
+ .onInstance(field).withArguments(object, value).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ if (accessibilityChanged) {
+ field.setAccessible(false);
+ }
+ }
+ }
+
+
+ /*
+ * Bridge methods for java.lang.Module
+ */
+
+ public static InputStream modulegetResourceAsStream(Module module, String name) throws IOException {
+ InputStream result = module.getResourceAsStream(name);
+ RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.MODULE_GETRESOURCEASSTREAM)
+ .onInstance(module).withArgument(name).returnValue(result).build();
+ RecordedInvocationsPublisher.publish(invocation);
+ return result;
+ }
+
+ /*
+ * Bridge methods for java.util.ResourceBundle
+ */
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArgument(baseName);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, ResourceBundle.Control control) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, control);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, control);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, Locale locale) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, locale);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, locale);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, Module module) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, module);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, module);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, Locale targetLocale, Module module) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, targetLocale, module);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, targetLocale, module);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle( String baseName, Locale targetLocale, ResourceBundle.Control control) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, targetLocale, control);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, targetLocale, control);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, Locale locale, ClassLoader loader) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, locale, loader);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, locale, loader);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ public static ResourceBundle resourcebundlegetBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.RESOURCEBUNDLE_GETBUNDLE).withArguments(baseName, targetLocale, loader, control);
+ ResourceBundle result = null;
+ try {
+ result = ResourceBundle.getBundle(baseName, targetLocale, loader, control);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+ /*
+ * Bridge methods for JDK Proxies
+ */
+
+ public static Object proxynewProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) {
+ RecordedInvocation.Builder builder = RecordedInvocation.of(InstrumentedMethod.PROXY_NEWPROXYINSTANCE)
+ .withArguments(loader, interfaces, h);
+ Object result = null;
+ try {
+ result = Proxy.newProxyInstance(loader, interfaces, h);
+ }
+ finally {
+ RecordedInvocationsPublisher.publish(builder.returnValue(result).build());
+ }
+ return result;
+ }
+
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java
new file mode 100644
index 00000000000..f61ef5286f9
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java
@@ -0,0 +1,397 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Proxy;
+import java.util.ResourceBundle;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+import org.springframework.aot.hint.MemberCategory;
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.RuntimeHintsPredicates;
+import org.springframework.aot.hint.TypeReference;
+
+/**
+ * Java method that is instrumented by the {@link RuntimeHintsAgent}.
+ *
A runtime invocation for reflection, resources, etc. can be backed by different hints.
+ * For example, {@code MyClass.class.getMethod("sample", null)} can be backed by a reflection
+ * hint on this method only, or a reflection hint on all public/declared methods of the class.
+ * @param invocation the current invocation of the instrumented method
+ */
+ Predicate matcher(RecordedInvocation invocation) {
+ return this.hintsMatcherGenerator.apply(invocation);
+ }
+
+ private static Predicate hasReturnValue(RecordedInvocation invocation) {
+ return runtimeHints -> invocation.getReturnValue() != null;
+ }
+
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassTransformer.java b/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassTransformer.java
new file mode 100644
index 00000000000..c49f7456a0d
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassTransformer.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.security.ProtectionDomain;
+import java.util.Arrays;
+
+import org.springframework.asm.ClassReader;
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+
+/**
+ * ASM {@link ClassFileTransformer} that delegates bytecode transformations
+ * to a {@link InvocationsRecorderClassVisitor class visitor} if and only
+ * if the class is in the list of packages considered for instrumentation.
+ *
+ * @author Brian Clozel
+ * @see InvocationsRecorderClassVisitor
+ */
+class InvocationsRecorderClassTransformer implements ClassFileTransformer {
+
+ private static final String AGENT_PACKAGE = InvocationsRecorderClassTransformer.class.getPackageName().replace('.', '/');
+
+ private static final String AOT_DYNAMIC_CLASSLOADER = "org/springframework/aot/test/generator/compile/DynamicClassLoader";
+
+ private final String[] instrumentedPackages;
+
+ private final String[] ignoredPackages;
+
+ public InvocationsRecorderClassTransformer(String[] instrumentedPackages, String[] ignoredPackages) {
+ Assert.notNull(instrumentedPackages, "instrumentedPackages should not be null");
+ Assert.notNull(ignoredPackages, "ignoredPackages should not be null");
+ this.instrumentedPackages = rewriteToAsmFormat(instrumentedPackages);
+ this.ignoredPackages = rewriteToAsmFormat(ignoredPackages);
+ }
+
+ private String[] rewriteToAsmFormat(String[] packages) {
+ return Arrays.stream(packages).map(pack -> pack.replace('.', '/'))
+ .toArray(String[]::new);
+ }
+
+ @Override
+ public byte[] transform(@Nullable ClassLoader classLoader, String className, Class> classBeingRedefined,
+ ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
+
+ if (isTransformationCandidate(classLoader, className)) {
+ return attemptClassTransformation(classfileBuffer);
+ }
+ return classfileBuffer;
+ }
+
+ private boolean isTransformationCandidate(@Nullable ClassLoader classLoader, String className) {
+ // Ignore system classes
+ if (classLoader == null) {
+ return false;
+ }
+ // Ignore agent classes and spring-core-test DynamicClassLoader
+ else if (className.startsWith(AGENT_PACKAGE) || className.equals(AOT_DYNAMIC_CLASSLOADER)) {
+ return false;
+ }
+ // Do not instrument CGlib classes
+ else if (className.contains("$$")) {
+ return false;
+ }
+ // Only some packages are instrumented
+ else {
+ for (String ignoredPackage : this.ignoredPackages) {
+ if (className.startsWith(ignoredPackage)) {
+ return false;
+ }
+ }
+ for (String instrumentedPackage : this.instrumentedPackages) {
+ if (className.startsWith(instrumentedPackage)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private byte[] attemptClassTransformation(byte[] classfileBuffer) {
+ ClassReader fileReader = new ClassReader(classfileBuffer);
+ InvocationsRecorderClassVisitor classVisitor = new InvocationsRecorderClassVisitor();
+ try {
+ fileReader.accept(classVisitor, 0);
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ return classfileBuffer;
+ }
+ if (classVisitor.isTransformed()) {
+ return classVisitor.getTransformedClassBuffer();
+ }
+ return classfileBuffer;
+ }
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassVisitor.java b/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassVisitor.java
new file mode 100644
index 00000000000..32cb0ddc0a4
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/InvocationsRecorderClassVisitor.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.springframework.asm.ClassVisitor;
+import org.springframework.asm.ClassWriter;
+import org.springframework.asm.Handle;
+import org.springframework.asm.MethodVisitor;
+import org.springframework.asm.Opcodes;
+import org.springframework.asm.SpringAsmInfo;
+
+/**
+ * ASM {@link ClassVisitor} that rewrites a known set of method invocations
+ * to call instrumented bridge methods for {@link RecordedInvocationsPublisher recording purposes}.
+ *
The bridge methods are located in the {@link InstrumentedBridgeMethods} class.
+ *
+ * @author Brian Clozel
+ * @see InstrumentedMethod
+ */
+class InvocationsRecorderClassVisitor extends ClassVisitor implements Opcodes {
+
+ private boolean isTransformed;
+
+ private final ClassWriter classWriter;
+
+ public InvocationsRecorderClassVisitor() {
+ this(new ClassWriter(ClassWriter.COMPUTE_MAXS));
+ }
+
+ private InvocationsRecorderClassVisitor(ClassWriter classWriter) {
+ super(SpringAsmInfo.ASM_VERSION, classWriter);
+ this.classWriter = classWriter;
+ }
+
+ public boolean isTransformed() {
+ return this.isTransformed;
+ }
+
+ public byte[] getTransformedClassBuffer() {
+ return this.classWriter.toByteArray();
+ }
+
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
+ MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
+ return new InvocationsRecorderMethodVisitor(mv);
+ }
+
+ @SuppressWarnings("deprecation")
+ class InvocationsRecorderMethodVisitor extends MethodVisitor implements Opcodes {
+
+ private static final String INSTRUMENTED_CLASS = InstrumentedBridgeMethods.class.getName().replace('.', '/');
+
+ private static final Set instrumentedMethods = new HashSet<>();
+
+ static {
+ for (InstrumentedMethod method : InstrumentedMethod.values()) {
+ MethodReference methodReference = method.methodReference();
+ instrumentedMethods.add(methodReference.getClassName().replace('.', '/')
+ + "#" + methodReference.getMethodName());
+ }
+ }
+
+ public InvocationsRecorderMethodVisitor(MethodVisitor mv) {
+ super(SpringAsmInfo.ASM_VERSION, mv);
+ }
+
+
+ @Override
+ public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
+ if (isOpcodeSupported(opcode) && shouldRecordMethodCall(owner, name)) {
+ String instrumentedMethodName = rewriteMethodName(owner, name);
+ mv.visitMethodInsn(INVOKESTATIC, INSTRUMENTED_CLASS, instrumentedMethodName,
+ rewriteDescriptor(opcode, owner, name, descriptor), false);
+ isTransformed = true;
+ }
+ else {
+ super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
+ }
+ }
+
+ private boolean isOpcodeSupported(int opcode) {
+ return Opcodes.INVOKEVIRTUAL == opcode || Opcodes.INVOKESTATIC == opcode;
+ }
+
+ @Override
+ public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
+ for (int i = 0; i < bootstrapMethodArguments.length; i++) {
+ if (bootstrapMethodArguments[i] instanceof Handle argumentHandle) {
+ if (shouldRecordMethodCall(argumentHandle.getOwner(), argumentHandle.getName())) {
+ String instrumentedMethodName = rewriteMethodName(argumentHandle.getOwner(), argumentHandle.getName());
+ String newDescriptor = rewriteDescriptor(argumentHandle.getTag(), argumentHandle.getOwner(), argumentHandle.getName(), argumentHandle.getDesc());
+ bootstrapMethodArguments[i] = new Handle(H_INVOKESTATIC, INSTRUMENTED_CLASS, instrumentedMethodName, newDescriptor, false);
+ isTransformed = true;
+ }
+ }
+ }
+ super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
+ }
+
+
+ private boolean shouldRecordMethodCall(String owner, String method) {
+ String methodReference = owner + "#" + method;
+ return instrumentedMethods.contains(methodReference);
+ }
+
+ private String rewriteMethodName(String owner, String methodName) {
+ int classIndex = owner.lastIndexOf('/');
+ return owner.substring(classIndex + 1).toLowerCase() + methodName;
+ }
+
+ private String rewriteDescriptor(int opcode, String owner, String name, String descriptor) {
+ return (opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.H_INVOKESTATIC) ? descriptor : "(L" + owner + ";" + descriptor.substring(1);
+ }
+
+ }
+
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/MethodReference.java b/spring-core-test/src/main/java/org/springframework/aot/agent/MethodReference.java
new file mode 100644
index 00000000000..d1d790d71ce
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/MethodReference.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.util.Objects;
+
+/**
+ * Reference to a Java method, identified by its owner class and the method name.
+ *
+ *
This implementation is ignoring parameters on purpose, as the goal here is
+ * to inform developers on invocations requiring additional
+ * {@link org.springframework.aot.hint.RuntimeHints} configuration, not
+ * precisely identifying a method.
+ *
+ * @author Brian Clozel
+ * @since 6.0
+ */
+public final class MethodReference {
+
+ private final String className;
+
+ private final String methodName;
+
+ private MethodReference(String className, String methodName) {
+ this.className = className;
+ this.methodName = methodName;
+ }
+
+ public static MethodReference of(Class> klass, String methodName) {
+ return new MethodReference(klass.getCanonicalName(), methodName);
+ }
+
+ /**
+ * Return the declaring class for this method.
+ * @return the declaring class name
+ */
+ public String getClassName() {
+ return this.className;
+ }
+
+ /**
+ * Return the name of the method.
+ * @return the method name
+ */
+ public String getMethodName() {
+ return this.methodName;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ MethodReference that = (MethodReference) o;
+ return this.className.equals(that.className) && this.methodName.equals(that.methodName);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.className, this.methodName);
+ }
+
+ @Override
+ public String toString() {
+ return this.className + '#' + this.methodName;
+ }
+}
diff --git a/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java b/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java
new file mode 100644
index 00000000000..cf31f53fc0d
--- /dev/null
+++ b/spring-core-test/src/main/java/org/springframework/aot/agent/RecordedInvocation.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright 2002-2022 the original author or authors.
+ *
+ * Licensed 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.springframework.aot.agent;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.TypeReference;
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+
+/**
+ * Record of an invocation of a method relevant to {@link org.springframework.aot.hint.RuntimeHints}.
+ *
The {@link RuntimeHintsAgent} instruments bytecode and intercepts invocations of
+ * {@link InstrumentedMethod specific methods}; invocations are recorded during test execution
+ * to match them against an existing {@link org.springframework.aot.hint.RuntimeHints} configuration.
+ *
+ * @author Brian Clozel
+ * @since 6.0
+ */
+public final class RecordedInvocation {
+
+ @Nullable
+ private final Object instance;
+
+ private final InstrumentedMethod instrumentedMethod;
+
+ private final Object[] arguments;
+
+ @Nullable
+ private final Object returnValue;
+
+ private final List stackFrames;
+
+ private RecordedInvocation(InstrumentedMethod instrumentedMethod, @Nullable Object instance,
+ Object[] arguments, @Nullable Object returnValue, List stackFrames) {
+ this.instance = instance;
+ this.instrumentedMethod = instrumentedMethod;
+ this.arguments = arguments;
+ this.returnValue = returnValue;
+ this.stackFrames = stackFrames;
+ }
+
+ /**
+ * Initialize a builder for the given {@link InstrumentedMethod}.
+ * @param instrumentedMethod the instrumented method
+ * @return a builder
+ */
+ public static Builder of(InstrumentedMethod instrumentedMethod) {
+ Assert.notNull(instrumentedMethod, "InstrumentedMethod must not be null");
+ return new Builder(instrumentedMethod);
+ }
+
+ /**
+ * Return the category of {@link RuntimeHints} this invocation relates to.
+ * @return the hint type
+ */
+ public HintType getHintType() {
+ return this.instrumentedMethod.getHintType();
+ }
+
+ /**
+ * Return a simple representation of the method invoked here.
+ * @return the method reference
+ */
+ public MethodReference getMethodReference() {
+ return this.instrumentedMethod.methodReference();
+ }
+
+ /**
+ * Return the stack trace of the current invocation.
+ * @return the stack frames
+ */
+ public Stream getStackFrames() {
+ return this.stackFrames.stream();
+ }
+
+ /**
+ * Return the instance of the object being invoked.
+ * @return the object instance
+ * @throws IllegalStateException in case of static invocations (there is no {@code this})
+ */
+ @SuppressWarnings("unchecked")
+ public T getInstance() {
+ Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations");
+ return (T) this.instance;
+ }
+
+ /**
+ * Return the Type reference of the object being invoked.
+ * @return the instance type reference, or {@code null}
+ * @throws IllegalStateException in case of static invocations (there is no {@code this})
+ */
+ public TypeReference getInstanceTypeReference() {
+ Assert.notNull(this.instance, "Cannot resolve 'this' for static invocations");
+ if (this.instance instanceof Class>) {
+ return TypeReference.of((Class>) this.instance);
+ }
+ return TypeReference.of(this.instance.getClass());
+ }
+
+ /**
+ * Return the argument values used for the current reflection invocation.
+ * @return the invocation arguments
+ */
+ public List