diff --git a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ReferenceAnnotationWithAotBeanPostProcessor.java b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ReferenceAnnotationWithAotBeanPostProcessor.java index 54d660b14c3..37d67bf2de4 100644 --- a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ReferenceAnnotationWithAotBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ReferenceAnnotationWithAotBeanPostProcessor.java @@ -713,7 +713,7 @@ private CodeBlock generateMethodStatementForElement(ClassName targetClassName, try { Class c = referenceElement.getInjectedType(); - AotUtils.registerSerializationHint(c, hints); + AotUtils.registerSerializationForService(c, hints); hints.reflection().registerType(TypeReference.of(c), MemberCategory.INVOKE_PUBLIC_METHODS); hints.proxies().registerJdkProxy(c, EchoService.class, Destroyable.class); hints.proxies().registerJdkProxy(c, EchoService.class, Destroyable.class, SpringProxy.class, Advised.class, DecoratingProxy.class); diff --git a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ServiceAnnotationWithAotPostProcessor.java b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ServiceAnnotationWithAotPostProcessor.java index ee5b17d001d..03210dc72d9 100644 --- a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ServiceAnnotationWithAotPostProcessor.java +++ b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/beans/factory/annotation/ServiceAnnotationWithAotPostProcessor.java @@ -88,7 +88,7 @@ public DubboServiceBeanRegistrationAotContribution(Class cl) { public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) { generationContext.getRuntimeHints().reflection().registerType(TypeReference.of(cl), MemberCategory.INVOKE_PUBLIC_METHODS); - AotUtils.registerSerializationHint(cl, generationContext.getRuntimeHints()); + AotUtils.registerSerializationForService(cl, generationContext.getRuntimeHints()); } } diff --git a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java index b6b686aee80..45b24dcf7e0 100644 --- a/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java +++ b/dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java @@ -17,25 +17,48 @@ package org.apache.dubbo.config.spring6.utils; +import org.apache.dubbo.common.compiler.support.ClassUtils; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.TypeReference; +import java.io.Serializable; import java.util.Arrays; +import java.util.Date; public class AotUtils { - private AotUtils(){ + private AotUtils() { } - public static void registerSerializationHint(Class injectType, RuntimeHints hints) { - Arrays.stream(injectType.getMethods()).forEach((method) -> { - Arrays.stream(method.getParameterTypes()).forEach((cl) -> { - hints.serialization().registerType(TypeReference.of(cl)); - }); + public static void registerSerializationForService(Class serviceType, RuntimeHints hints) { + Arrays.stream(serviceType.getMethods()).forEach((method) -> { + Arrays.stream(method.getParameterTypes()).forEach((parameterType) -> registerSerializationType(parameterType, hints)); - hints.serialization().registerType(TypeReference.of(method.getReturnType())); + registerSerializationType(method.getReturnType(), hints); }); } + + private static void registerSerializationType(Class registerType, RuntimeHints hints) { + if (isPrimitive(registerType)) { + hints.serialization().registerType(TypeReference.of(ClassUtils.getBoxedClass(registerType))); + } else { + if (Serializable.class.isAssignableFrom(registerType)) { + hints.serialization().registerType(TypeReference.of(registerType)); + + Arrays.stream(registerType.getDeclaredFields()).forEach((field -> registerSerializationType(field.getType(), hints))); + + registerSerializationType(registerType.getSuperclass(), hints); + } + } + + } + + private static boolean isPrimitive(Class cls) { + return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class + || cls == Character.class || cls == Short.class || cls == Integer.class + || cls == Long.class || cls == Float.class || cls == Double.class + || cls == String.class || cls == Date.class || cls == Class.class; + } } diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/AotUtilsTest.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/AotUtilsTest.java new file mode 100644 index 00000000000..de14a218d8d --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/AotUtilsTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; + +import java.util.concurrent.atomic.AtomicBoolean; + +public class AotUtilsTest { + + @Test + void registerSerializationForServiceTest() { + + RuntimeHints runtimeHints = new RuntimeHints(); + AotUtils.registerSerializationForService(DemoService.class, runtimeHints); + + AtomicBoolean containHelloRequest = new AtomicBoolean(false); + runtimeHints.serialization().javaSerializationHints().forEach(s -> { + if (s.getType().getName().equals(HelloRequest.class.getName())) { + containHelloRequest.set(true); + } + }); + + AtomicBoolean containPerson = new AtomicBoolean(false); + runtimeHints.serialization().javaSerializationHints().forEach(s -> { + if (s.getType().getName().equals(HelloRequest.class.getName())) { + containPerson.set(true); + } + }); + + AtomicBoolean containString = new AtomicBoolean(false); + runtimeHints.serialization().javaSerializationHints().forEach(s -> { + if (s.getType().getName().equals(HelloRequest.class.getName())) { + containString.set(true); + } + }); + + AtomicBoolean containHelloRequestSuper = new AtomicBoolean(false); + runtimeHints.serialization().javaSerializationHints().forEach(s -> { + if (s.getType().getName().equals(HelloRequest.class.getName())) { + containHelloRequestSuper.set(true); + } + }); + + AtomicBoolean containHelloResponse = new AtomicBoolean(false); + runtimeHints.serialization().javaSerializationHints().forEach(s -> { + if (s.getType().getName().equals(HelloRequest.class.getName())) { + containHelloResponse.set(true); + } + }); + + Assertions.assertTrue(containHelloRequest.get()); + Assertions.assertTrue(containPerson.get()); + Assertions.assertTrue(containString.get()); + Assertions.assertTrue(containHelloRequestSuper.get()); + Assertions.assertTrue(containHelloResponse.get()); + } + +} diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java new file mode 100644 index 00000000000..1d8f623bcaf --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + + +public interface DemoService { + + HelloResponse sayHello(HelloRequest request); + +} diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java new file mode 100644 index 00000000000..f7dbd3e4ef1 --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + +import java.io.Serializable; + +public class HelloRequest extends HelloRequestSuper implements Serializable { + private Person person; + + + public HelloRequest(Person person) { + this.person = person; + } + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } +} diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java new file mode 100644 index 00000000000..a6c2e496441 --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + +import java.io.Serializable; + +public class HelloRequestSuper implements Serializable { + private String su; + + public HelloRequestSuper() { + } + + public HelloRequestSuper(String su) { + this.su = su; + } + + public String getSu() { + return su; + } + + public void setSu(String su) { + this.su = su; + } +} diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java new file mode 100644 index 00000000000..59389e4e617 --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + + +import java.io.Serializable; + +public class HelloResponse implements Serializable { + private String response; + + public HelloResponse(String response) { + this.response = response; + } + + public String getResponse() { + return response; + } + + public void setResponse(String response) { + this.response = response; + } +} diff --git a/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/Person.java b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/Person.java new file mode 100644 index 00000000000..de83f4b8d08 --- /dev/null +++ b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/Person.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.config.spring6.utils; + +import java.io.Serializable; + +public class Person implements Serializable { + + private String name; + + public Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}