From 13df9058a48c7cd79d5f144e64564fa87e5b0d10 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 9 Dec 2024 10:50:44 +0100 Subject: [PATCH] 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 --- .../org/springframework/aot/hint/MemberCategory.java | 12 ++++++++++-- .../aot/nativex/ReflectionHintsWriter.java | 1 + .../aot/nativex/ReflectionHintsWriterTests.java | 5 +++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java b/spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java index 2d09bb0e45..697431698a 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -124,6 +124,14 @@ public enum MemberCategory { * reflection for inner classes but rather makes sure they are available * via a call to {@link Class#getDeclaredClasses}. */ - DECLARED_CLASSES + DECLARED_CLASSES, + + /** + * A category that represents the need for + * {@link sun.misc.Unsafe#allocateInstance(Class) unsafe allocation} + * for this type. + * @since 6.2.1 + */ + UNSAFE_ALLOCATED } diff --git a/spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java b/spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java index 3d678b0d85..df360b73b1 100644 --- a/spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java +++ b/spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java @@ -124,6 +124,7 @@ class ReflectionHintsWriter { attributes.put("allDeclaredMethods", true); case PUBLIC_CLASSES -> attributes.put("allPublicClasses", true); case DECLARED_CLASSES -> attributes.put("allDeclaredClasses", true); + case UNSAFE_ALLOCATED -> attributes.put("unsafeAllocated", true); } } ); diff --git a/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java b/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java index c9fb6901d7..0e797e3909 100644 --- a/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java +++ b/spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -59,7 +59,7 @@ class ReflectionHintsWriterTests { MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS, - MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES) + MemberCategory.PUBLIC_CLASSES, MemberCategory.DECLARED_CLASSES, MemberCategory.UNSAFE_ALLOCATED) .withField("DEFAULT_CHARSET") .withField("defaultCharset") .withField("aScore") @@ -83,6 +83,7 @@ class ReflectionHintsWriterTests { "allDeclaredMethods": true, "allPublicClasses": true, "allDeclaredClasses": true, + "unsafeAllocated": true, "fields": [ { "name": "aScore" }, { "name": "DEFAULT_CHARSET" },