You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/06/14 11:38:41 UTC

[GitHub] liubao68 closed pull request #768: [SCB-669] add extra hashcode into CtTypeJavaType to avoid deserialization problem

liubao68 closed pull request #768: [SCB-669] add extra hashcode into CtTypeJavaType to avoid deserialization problem
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/768
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/CtTypeJavaType.java b/common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/CtTypeJavaType.java
index 1ef4cec8e..74c083cb0 100644
--- a/common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/CtTypeJavaType.java
+++ b/common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/CtTypeJavaType.java
@@ -16,7 +16,10 @@
  */
 package org.apache.servicecomb.common.javassist;
 
+import java.util.Objects;
+
 import com.fasterxml.jackson.databind.type.SimpleType;
+import com.fasterxml.jackson.databind.type.TypeBindings;
 
 /**
  * just a wrapper for CtType
@@ -28,7 +31,9 @@
   private CtType type;
 
   public CtTypeJavaType(CtType type) {
-    super(CtTypeJavaType.class);
+    super(CtTypeJavaType.class, TypeBindings.emptyBindings(), null, null,
+        type == null ? 0 : type.getGenericSignature().hashCode(),
+        null, null, false);
     this.type = type;
   }
 
@@ -46,9 +51,20 @@ public String getGenericSignature() {
     return type.getGenericSignature();
   }
 
-
   @Override
   public StringBuilder getGenericSignature(StringBuilder sb) {
     return sb.append(type.getGenericSignature());
   }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || !this.getClass().isAssignableFrom(o.getClass())) {
+      return false;
+    }
+    CtTypeJavaType that = (CtTypeJavaType) o;
+    return Objects.equals(this.getGenericSignature(), that.getGenericSignature());
+  }
 }
diff --git a/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestCtTypeJavaType.java b/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestCtTypeJavaType.java
index 46213a316..9ec4ea96c 100644
--- a/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestCtTypeJavaType.java
+++ b/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestCtTypeJavaType.java
@@ -45,4 +45,45 @@ public void getGenericSignature() {
     Assert.assertEquals("Ljava/util/List<Lorg/apache/servicecomb/common/javassist/TestCtTypeJavaType;>;",
         listJavaType.getGenericSignature());
   }
+
+  /**
+   * The {@link CtTypeJavaType} with different CtType should holds different hash code.
+   */
+  @Test
+  public void testHashCode() {
+    JavaType newJavaType = TypeFactory.defaultInstance().constructType(String.class);
+    CtType newCtType = new CtType(newJavaType);
+    CtTypeJavaType newCtTypeJavaType = new CtTypeJavaType(newCtType);
+    Assert.assertNotEquals(ctTypeJavaType.hashCode(), newCtTypeJavaType.hashCode());
+
+    newJavaType = TypeFactory.defaultInstance().constructType(cls);
+    newCtType = new CtType(newJavaType);
+    newCtTypeJavaType = new CtTypeJavaType(newCtType);
+    Assert.assertEquals(ctTypeJavaType.hashCode(), newCtTypeJavaType.hashCode());
+  }
+
+  /**
+   * The {@link CtTypeJavaType}s holding different type information should not equal to each others.
+   * While those holding the same type information should be equal.
+   */
+  @Test
+  public void testEquals() {
+    JavaType newJavaType = TypeFactory.defaultInstance().constructType(String.class);
+    CtType newCtType = new CtType(newJavaType);
+    CtTypeJavaType newCtTypeJavaType = new CtTypeJavaType(newCtType);
+    Assert.assertNotEquals(ctTypeJavaType, newCtTypeJavaType);
+
+    newJavaType = TypeFactory.defaultInstance().constructType(cls);
+    newCtType = new CtType(newJavaType);
+    newCtTypeJavaType = new CtTypeJavaType(newCtType);
+    Assert.assertEquals(ctTypeJavaType, newCtTypeJavaType);
+
+    // test subClass of CtTypeJavaType
+    newJavaType = TypeFactory.defaultInstance().constructType(cls);
+    newCtType = new CtType(newJavaType);
+    newCtTypeJavaType = new CtTypeJavaType(newCtType) {
+      private static final long serialVersionUID = 1876189050753964880L;
+    };
+    Assert.assertEquals(ctTypeJavaType, newCtTypeJavaType);
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services