Skip to content

Commit

Permalink
Merge ce99d46 into b81196f
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0092 committed Jun 14, 2018
2 parents b81196f + ce99d46 commit 0b1b4d6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0b1b4d6

Please sign in to comment.