This is an automated email from the ASF dual-hosted git repository.

crazyhzm pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new e642e72ce8 Fixed the issue of superclass and filed not being loaded 
automatically in the native serialization config scenario (#12435)
e642e72ce8 is described below

commit e642e72ce80dfbb72db9f48791642507b75c9d3f
Author: huazhongming <[email protected]>
AuthorDate: Thu Jun 1 15:01:20 2023 +0800

    Fixed the issue of superclass and filed not being loaded automatically in 
the native serialization config scenario (#12435)
    
    * Fixed the issue of superclass and filed not being loaded automatically in 
the native serialization config scenario
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * Add license
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * Fix test
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * Fix test
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * add log
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * add log
    
    Signed-off-by: crazyhzm <[email protected]>
    
    * Fix AotUtilsTest
    
    Signed-off-by: crazyhzm <[email protected]>
    
    ---------
    
    Signed-off-by: crazyhzm <[email protected]>
---
 ...eferenceAnnotationWithAotBeanPostProcessor.java |  2 +-
 .../ServiceAnnotationWithAotPostProcessor.java     |  2 +-
 .../dubbo/config/spring6/utils/AotUtils.java       | 37 +++++++++--
 .../dubbo/config/spring6/utils/AotUtilsTest.java   | 76 ++++++++++++++++++++++
 .../dubbo/config/spring6/utils/DemoService.java}   | 21 +-----
 .../dubbo/config/spring6/utils/HelloRequest.java}  | 25 +++----
 .../config/spring6/utils/HelloRequestSuper.java}   | 27 ++++----
 .../dubbo/config/spring6/utils/HelloResponse.java} | 25 +++----
 .../apache/dubbo/config/spring6/utils/Person.java} | 25 +++----
 9 files changed, 152 insertions(+), 88 deletions(-)

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 54d660b14c..37d67bf2de 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 @@ public class ReferenceAnnotationWithAotBeanPostProcessor 
extends AbstractAnnotat
 
             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 ee5b17d001..03210dc72d 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 class ServiceAnnotationWithAotPostProcessor extends 
ServiceAnnotationPost
         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 b6b686aee8..45b24dcf7e 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 0000000000..de14a218d8
--- /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/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
 
b/dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java
similarity index 58%
copy from 
dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
copy to 
dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java
index b6b686aee8..1d8f623bca 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/test/java/org/apache/dubbo/config/spring6/utils/DemoService.java
@@ -14,28 +14,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.dubbo.config.spring6.utils;
 
-import org.springframework.aot.hint.RuntimeHints;
-import org.springframework.aot.hint.TypeReference;
-
-import java.util.Arrays;
-
-
-public class AotUtils {
-
-    private AotUtils(){
 
-    }
+public interface DemoService {
 
-    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));
-            });
+    HelloResponse sayHello(HelloRequest request);
 
-            
hints.serialization().registerType(TypeReference.of(method.getReturnType()));
-        });
-    }
 }
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/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java
similarity index 59%
copy from 
dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
copy to 
dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java
index b6b686aee8..f7dbd3e4ef 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/test/java/org/apache/dubbo/config/spring6/utils/HelloRequest.java
@@ -14,28 +14,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.dubbo.config.spring6.utils;
 
-import org.springframework.aot.hint.RuntimeHints;
-import org.springframework.aot.hint.TypeReference;
-
-import java.util.Arrays;
+import java.io.Serializable;
 
+public class HelloRequest extends HelloRequestSuper implements Serializable {
+    private Person person;
 
-public class AotUtils {
-
-    private AotUtils(){
 
+    public HelloRequest(Person person) {
+        this.person = person;
     }
 
-    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 Person getPerson() {
+        return person;
+    }
 
-            
hints.serialization().registerType(TypeReference.of(method.getReturnType()));
-        });
+    public void setPerson(Person person) {
+        this.person = person;
     }
 }
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/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java
similarity index 59%
copy from 
dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
copy to 
dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java
index b6b686aee8..a6c2e49644 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/test/java/org/apache/dubbo/config/spring6/utils/HelloRequestSuper.java
@@ -14,28 +14,25 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.dubbo.config.spring6.utils;
 
-import org.springframework.aot.hint.RuntimeHints;
-import org.springframework.aot.hint.TypeReference;
-
-import java.util.Arrays;
-
+import java.io.Serializable;
 
-public class AotUtils {
+public class HelloRequestSuper implements Serializable {
+    private String su;
 
-    private AotUtils(){
+    public HelloRequestSuper() {
+    }
 
+    public HelloRequestSuper(String su) {
+        this.su = su;
     }
 
-    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 String getSu() {
+        return su;
+    }
 
-            
hints.serialization().registerType(TypeReference.of(method.getReturnType()));
-        });
+    public void setSu(String su) {
+        this.su = su;
     }
 }
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/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java
similarity index 59%
copy from 
dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
copy to 
dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java
index b6b686aee8..59389e4e61 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/test/java/org/apache/dubbo/config/spring6/utils/HelloResponse.java
@@ -14,28 +14,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.dubbo.config.spring6.utils;
 
-import org.springframework.aot.hint.RuntimeHints;
-import org.springframework.aot.hint.TypeReference;
-
-import java.util.Arrays;
 
+import java.io.Serializable;
 
-public class AotUtils {
-
-    private AotUtils(){
+public class HelloResponse implements Serializable {
+    private String response;
 
+    public HelloResponse(String response) {
+        this.response = response;
     }
 
-    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 String getResponse() {
+        return response;
+    }
 
-            
hints.serialization().registerType(TypeReference.of(method.getReturnType()));
-        });
+    public void setResponse(String response) {
+        this.response = response;
     }
 }
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/test/java/org/apache/dubbo/config/spring6/utils/Person.java
similarity index 59%
copy from 
dubbo-config/dubbo-config-spring6/src/main/java/org/apache/dubbo/config/spring6/utils/AotUtils.java
copy to 
dubbo-config/dubbo-config-spring6/src/test/java/org/apache/dubbo/config/spring6/utils/Person.java
index b6b686aee8..de83f4b8d0 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/test/java/org/apache/dubbo/config/spring6/utils/Person.java
@@ -14,28 +14,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.dubbo.config.spring6.utils;
 
-import org.springframework.aot.hint.RuntimeHints;
-import org.springframework.aot.hint.TypeReference;
-
-import java.util.Arrays;
+import java.io.Serializable;
 
+public class Person implements Serializable {
 
-public class AotUtils {
-
-    private AotUtils(){
+    private String name;
 
+    public Person(String name) {
+        this.name = name;
     }
 
-    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 String getName() {
+        return name;
+    }
 
-            
hints.serialization().registerType(TypeReference.of(method.getReturnType()));
-        });
+    public void setName(String name) {
+        this.name = name;
     }
 }

Reply via email to