Skip to content

Commit 13df905

Browse files
committed
Introduce "unsafeAllocated" flag in TypeHint
This metadata information is required for supporting libraries using `sun.misc.Unsafe#allocateInstance(Class<?>)`, even though Spring Framework is not using this feature. Closes gh-34055
1 parent aee52b5 commit 13df905

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -124,6 +124,14 @@ public enum MemberCategory {
124124
* reflection for inner classes but rather makes sure they are available
125125
* via a call to {@link Class#getDeclaredClasses}.
126126
*/
127-
DECLARED_CLASSES
127+
DECLARED_CLASSES,
128+
129+
/**
130+
* A category that represents the need for
131+
* {@link sun.misc.Unsafe#allocateInstance(Class) unsafe allocation}
132+
* for this type.
133+
* @since 6.2.1
134+
*/
135+
UNSAFE_ALLOCATED
128136

129137
}

spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private void handleCategories(Map<String, Object> attributes, Set<MemberCategory
124124
attributes.put("allDeclaredMethods", true);
125125
case PUBLIC_CLASSES -> attributes.put("allPublicClasses", true);
126126
case DECLARED_CLASSES -> attributes.put("allDeclaredClasses", true);
127+
case UNSAFE_ALLOCATED -> attributes.put("unsafeAllocated", true);
127128
}
128129
}
129130
);

spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ void one() throws JSONException {
5959
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
6060
MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS,
6161
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS,
62-
MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES)
62+
MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES, MemberCategory.UNSAFE_ALLOCATED)
6363
.withField("DEFAULT_CHARSET")
6464
.withField("defaultCharset")
6565
.withField("aScore")
@@ -83,6 +83,7 @@ void one() throws JSONException {
8383
"allDeclaredMethods": true,
8484
"allPublicClasses": true,
8585
"allDeclaredClasses": true,
86+
"unsafeAllocated": true,
8687
"fields": [
8788
{ "name": "aScore" },
8889
{ "name": "DEFAULT_CHARSET" },

0 commit comments

Comments
 (0)