From 100ce9642afb14b806e42ae9277f77171a7bc04c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 10 Jun 2022 15:27:21 +0200 Subject: [PATCH] Use TypeReference consistently in hints writer Closes gh-28606 --- .../aot/nativex/ProxyHintsWriter.java | 4 +-- .../aot/nativex/ReflectionHintsWriter.java | 24 ++++++++++------- .../aot/nativex/ProxyHintsWriterTests.java | 14 ++++++++++ .../nativex/ReflectionHintsWriterTests.java | 26 +++++++++++++++++++ 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsWriter.java b/spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsWriter.java index 2db093c133..b19ce7f5a4 100644 --- a/spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsWriter.java +++ b/spring-core/src/main/java/org/springframework/aot/nativex/ProxyHintsWriter.java @@ -21,7 +21,6 @@ import java.util.Map; import org.springframework.aot.hint.JdkProxyHint; import org.springframework.aot.hint.ProxyHints; -import org.springframework.aot.hint.TypeReference; /** * Write {@link JdkProxyHint}s contained in a {@link ProxyHints} to the JSON @@ -46,8 +45,7 @@ class ProxyHintsWriter { private Map toAttributes(JdkProxyHint hint) { Map attributes = new LinkedHashMap<>(); handleCondition(attributes, hint); - attributes.put("interfaces", hint.getProxiedInterfaces().stream() - .map(TypeReference::getCanonicalName).toList()); + attributes.put("interfaces", hint.getProxiedInterfaces()); return attributes; } 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 ae1041e422..bd22234210 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 @@ -29,7 +29,6 @@ import org.springframework.aot.hint.FieldHint; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.ReflectionHints; import org.springframework.aot.hint.TypeHint; -import org.springframework.aot.hint.TypeReference; import org.springframework.lang.Nullable; /** @@ -96,7 +95,7 @@ class ReflectionHintsWriter { private Map toAttributes(ExecutableHint hint) { Map attributes = new LinkedHashMap<>(); attributes.put("name", hint.getName()); - attributes.put("parameterTypes", hint.getParameterTypes().stream().map(TypeReference::getCanonicalName).toList()); + attributes.put("parameterTypes", hint.getParameterTypes()); return attributes; } @@ -105,14 +104,21 @@ class ReflectionHintsWriter { switch (category) { case PUBLIC_FIELDS -> attributes.put("allPublicFields", true); case DECLARED_FIELDS -> attributes.put("allDeclaredFields", true); - case INTROSPECT_PUBLIC_CONSTRUCTORS -> attributes.put("queryAllPublicConstructors", true); - case INTROSPECT_DECLARED_CONSTRUCTORS -> attributes.put("queryAllDeclaredConstructors", true); - case INVOKE_PUBLIC_CONSTRUCTORS -> attributes.put("allPublicConstructors", true); - case INVOKE_DECLARED_CONSTRUCTORS -> attributes.put("allDeclaredConstructors", true); - case INTROSPECT_PUBLIC_METHODS -> attributes.put("queryAllPublicMethods", true); - case INTROSPECT_DECLARED_METHODS -> attributes.put("queryAllDeclaredMethods", true); + case INTROSPECT_PUBLIC_CONSTRUCTORS -> + attributes.put("queryAllPublicConstructors", true); + case INTROSPECT_DECLARED_CONSTRUCTORS -> + attributes.put("queryAllDeclaredConstructors", true); + case INVOKE_PUBLIC_CONSTRUCTORS -> + attributes.put("allPublicConstructors", true); + case INVOKE_DECLARED_CONSTRUCTORS -> + attributes.put("allDeclaredConstructors", true); + case INTROSPECT_PUBLIC_METHODS -> + attributes.put("queryAllPublicMethods", true); + case INTROSPECT_DECLARED_METHODS -> + attributes.put("queryAllDeclaredMethods", true); case INVOKE_PUBLIC_METHODS -> attributes.put("allPublicMethods", true); - case INVOKE_DECLARED_METHODS -> attributes.put("allDeclaredMethods", true); + case INVOKE_DECLARED_METHODS -> + attributes.put("allDeclaredMethods", true); case PUBLIC_CLASSES -> attributes.put("allPublicClasses", true); case DECLARED_CLASSES -> attributes.put("allDeclaredClasses", true); } diff --git a/spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java b/spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java index 3b03bdd8d5..d4a624f690 100644 --- a/spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java +++ b/spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java @@ -63,6 +63,16 @@ public class ProxyHintsWriterTests { ]""", hints); } + @Test + void shouldWriteInnerClass() throws JSONException { + ProxyHints hints = new ProxyHints(); + hints.registerJdkProxy(Inner.class); + assertEquals(""" + [ + { "interfaces": [ "org.springframework.aot.nativex.ProxyHintsWriterTests$Inner" ] } + ]""", hints); + } + @Test void shouldWriteCondition() throws JSONException { ProxyHints hints = new ProxyHints(); @@ -81,4 +91,8 @@ public class ProxyHintsWriterTests { JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE); } + interface Inner { + + } + } 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 6c51ca6506..6091690872 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 @@ -158,6 +158,27 @@ public class ReflectionHintsWriterTests { """, hints); } + @Test + void methodWithInnerClassParameter() throws JSONException { + ReflectionHints hints = new ReflectionHints(); + hints.registerType(Integer.class, builder -> builder.withMethod("test", List.of(TypeReference.of(Inner.class)), + b -> b.withMode(ExecutableMode.INVOKE))); + + assertEquals(""" + [ + { + "name": "java.lang.Integer", + "methods": [ + { + "name": "test", + "parameterTypes": ["org.springframework.aot.nativex.ReflectionHintsWriterTests$Inner"] + } + ] + } + ] + """, hints); + } + @Test void methodAndQueriedMethods() throws JSONException { ReflectionHints hints = new ReflectionHints(); @@ -194,4 +215,9 @@ public class ReflectionHintsWriterTests { JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE); } + + static class Inner { + + } + }