Skip to content

Commit c86e678

Browse files
committed
Add RuntimeHintsAgent Java agent
With the introduction of `RuntimeHints`, we can now contribute reflection, resources and proxies hints that describe the expected runtime behavior of the application. While this can be verified at runtime with smoke tests, managing such tests and compiling to native there is not very efficient. This commit introduces the new `RuntimeHintsAgent`, a Java agent that instruments JDK methods related to `RuntimeHints`. It is different from the GraalVM agent, which aims at collecting all the required hints for the runtime behavior of an application and dump those in the expected format. Here, the `RuntimeHintsAgent` can collect the related invocations only for a delimited scope (typically, a lambda within a test) and later check those against a `RuntimeHints` instance. In the case of testing `RuntimeHintsRegistrar` implementations, the process is reversed: instead of manually checking for registered hints in a `RuntimeHints` instance, tests should exercise the use cases and then check that the recorded behavior is in line with the prepared hints. This first commit adds the agent infrastructure that collects the invocations for all relevant JDK methods. See gh-27981
1 parent 033d9d0 commit c86e678

13 files changed

+2459
-0
lines changed

spring-core-test/spring-core-test.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ description = "Spring Core Test"
22

33
dependencies {
44
api(project(":spring-core"))
5+
api("org.junit.jupiter:junit-jupiter-api")
56
api("org.assertj:assertj-core")
67
api("com.thoughtworks.qdox:qdox")
78
compileOnly("org.junit.jupiter:junit-jupiter")
89
compileOnly("org.junit.platform:junit-platform-engine")
910
compileOnly("org.junit.platform:junit-platform-launcher")
1011
}
12+
13+
jar {
14+
manifest {
15+
attributes(
16+
'Premain-Class': 'org.springframework.aot.agent.RuntimeHintsAgent',
17+
'Can-Redefine-Classes': 'true'
18+
)
19+
}
20+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.agent;
18+
19+
20+
import org.springframework.aot.hint.ClassProxyHint;
21+
import org.springframework.aot.hint.JavaSerializationHint;
22+
import org.springframework.aot.hint.JdkProxyHint;
23+
import org.springframework.aot.hint.ReflectionHints;
24+
import org.springframework.aot.hint.ResourceBundleHint;
25+
import org.springframework.aot.hint.ResourcePatternHint;
26+
27+
/**
28+
* Main types of {@link org.springframework.aot.hint.RuntimeHints}.
29+
* <p>This allows to sort {@link RecordedInvocation recorded invocations}
30+
* into hint categories.
31+
*
32+
* @author Brian Clozel
33+
* @since 6.0
34+
*/
35+
public enum HintType {
36+
37+
/**
38+
* Reflection hint, as described by {@link org.springframework.aot.hint.ReflectionHints}.
39+
*/
40+
REFLECTION(ReflectionHints.class),
41+
42+
/**
43+
* Resource pattern hint, as described by {@link org.springframework.aot.hint.ResourceHints#resourcePatterns()}.
44+
*/
45+
RESOURCE_PATTERN(ResourcePatternHint.class),
46+
47+
/**
48+
* Resource bundle hint, as described by {@link org.springframework.aot.hint.ResourceHints#resourceBundles()}.
49+
*/
50+
RESOURCE_BUNDLE(ResourceBundleHint.class),
51+
52+
/**
53+
* Java serialization hint, as described by {@link org.springframework.aot.hint.JavaSerializationHint}.
54+
*/
55+
JAVA_SERIALIZATION(JavaSerializationHint.class),
56+
57+
/**
58+
* JDK proxies hint, as described by {@link org.springframework.aot.hint.ProxyHints#jdkProxies()}.
59+
*/
60+
JDK_PROXIES(JdkProxyHint.class),
61+
62+
/**
63+
* Class proxies hint, as described by {@link org.springframework.aot.hint.ProxyHints#classProxies()}.
64+
*/
65+
CLASS_PROXIES(ClassProxyHint.class);
66+
67+
private final Class<?> hintClass;
68+
69+
HintType(Class<?> hintClass) {
70+
this.hintClass = hintClass;
71+
}
72+
73+
public String hintClassName() {
74+
return this.hintClass.getSimpleName();
75+
}
76+
}

0 commit comments

Comments
 (0)