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

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 262c578b fix(java): fix wildcard capturer capture NullPointerException 
(#1637)
262c578b is described below

commit 262c578bd29be4d86c5740b8f95768724357188e
Author: Shawn Yang <[email protected]>
AuthorDate: Thu May 16 21:41:49 2024 +0800

    fix(java): fix wildcard capturer capture NullPointerException (#1637)
    
    ## What does this PR do?
    
     fix wildcard capturer capture NullPointerException
    
    ## Related issues
    Closes #1633
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/incubator-fury/issues/new/choose)
    describing the need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../main/java/org/apache/fury/reflect/TypeRef.java | 14 +++++++
 .../main/java/org/apache/fury/reflect/Types.java   | 22 ++++++----
 .../java/org/apache/fury/reflect/TypeRefTest.java  | 47 +++++++++++++++++++++-
 3 files changed, 74 insertions(+), 9 deletions(-)

diff --git a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java 
b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
index b984bb32..3707f15a 100644
--- a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
+++ b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
@@ -900,16 +900,30 @@ public class TypeRef<T> {
             for (; i < upperBoundsLength; i++) {
               combinedUpperBounds[i] = upperBounds[i];
             }
+            int skipCount = 0;
             rootFor:
             for (; i < combinedUpperBounds.length; i++) {
               Type typeParamBound = typeParamBounds[i - upperBoundsLength];
               for (Type upperBound : upperBounds) {
                 if (upperBound.equals(typeParamBound)) {
+                  skipCount++;
                   continue rootFor;
                 }
               }
               combinedUpperBounds[i] = typeParamBound;
             }
+            if (skipCount > 0) {
+              i = upperBoundsLength;
+              while (combinedUpperBounds[i] == null) {
+                if (i == combinedUpperBounds.length - 1) {
+                  break;
+                } else {
+                  combinedUpperBounds[i] = combinedUpperBounds[i++];
+                }
+              }
+              combinedUpperBounds =
+                  Arrays.copyOf(combinedUpperBounds, 
combinedUpperBounds.length - skipCount);
+            }
           }
           return super.captureAsTypeVariable(combinedUpperBounds);
         }
diff --git a/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java 
b/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java
index cbfc7cdb..5bedac0e 100644
--- a/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java
+++ b/java/fury-core/src/main/java/org/apache/fury/reflect/Types.java
@@ -131,17 +131,23 @@ class Types {
 
     @Override
     public String toString() {
-      return "ParameterizedTypeImpl{"
-          + "actualTypeArguments="
-          + Arrays.toString(actualTypeArguments)
-          + ", rawType="
-          + rawType
-          + ", ownerType="
-          + ownerType
-          + '}';
+      StringBuilder builder = new StringBuilder();
+      builder.append(typeName(rawType)).append('<');
+      int i = 0;
+      for (Type typeArgument : actualTypeArguments) {
+        if (i++ != 0) {
+          builder.append(", ");
+        }
+        builder.append(typeName(typeArgument));
+      }
+      return builder.append('>').toString();
     }
   }
 
+  static String typeName(Type type) {
+    return (type instanceof Class) ? ((Class<?>) type).getName() : 
type.toString();
+  }
+
   public static class GenericArrayTypeImpl implements GenericArrayType {
     private final Type genericComponentType;
 
diff --git 
a/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java 
b/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
index a0cc6cf5..6e791342 100644
--- a/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
+++ b/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
@@ -23,10 +23,17 @@ import static org.testng.Assert.*;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.TreeMap;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import org.apache.fury.Fury;
+import org.apache.fury.FuryTestBase;
+import org.apache.fury.collection.Tuple2;
 import org.apache.fury.type.TypeUtils;
+import org.testng.Assert;
 import org.testng.annotations.Test;
 
-public class TypeRefTest {
+public class TypeRefTest extends FuryTestBase {
   static class MapObject extends LinkedHashMap<String, Object> {}
 
   @Test
@@ -39,4 +46,42 @@ public class TypeRefTest {
         TypeUtils.mapOf(Map.class, String.class, Object.class),
         new TypeRef<Map<String, Object>>() {});
   }
+
+  @Data
+  static class MyInternalClass<T> {
+    public int c = 9;
+    public T t;
+  }
+
+  @EqualsAndHashCode(callSuper = true)
+  static class MyInternalBaseClass extends MyInternalClass<String> {
+    public int d = 19;
+  }
+
+  @Data
+  static class MyClass {
+    protected Map<String, MyInternalClass<?>> fields;
+    private transient int r = 13;
+
+    public MyClass() {
+      fields = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+      fields.put("test", new MyInternalBaseClass());
+    }
+  }
+
+  @Test
+  public void testWildcardType() {
+    Tuple2<TypeRef<?>, TypeRef<?>> mapKeyValueType =
+        TypeUtils.getMapKeyValueType(new TypeRef<Map<String, 
MyInternalClass<?>>>() {});
+    Assert.assertEquals(mapKeyValueType.f0.getType(), String.class);
+    Assert.assertEquals(
+        mapKeyValueType.f1.getRawType(), new TypeRef<MyInternalClass<?>>() 
{}.getRawType());
+  }
+
+  @Test(dataProvider = "enableCodegen")
+  public void testWildcardTypeSerialization(boolean enableCodegen) {
+    // see issue https://github.com/apache/incubator-fury/issues/1633
+    Fury fury = builder().withCodegen(enableCodegen).build();
+    serDeCheck(fury, new MyClass());
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to