From 95e87f4791218c45ddb9b8eed0a4073873432034 Mon Sep 17 00:00:00 2001 From: yaohaishi Date: Thu, 14 Jun 2018 13:05:23 +0800 Subject: [PATCH] [SCB-669] add extra hashcode into CtTypeJavaType to avoid deserialization problem --- .../common/javassist/CtTypeJavaType.java | 20 ++++++++- .../common/javassist/TestCtTypeJavaType.java | 41 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) 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 1ef4cec8e7e..74c083cb08f 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 @@ public class CtTypeJavaType extends SimpleType { 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 46213a31682..9ec4ea96c5f 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;", 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); + } }