Permalink
Browse files

fix: two generated ParameterizedTypes aren't equals

Adds an equals method to the inner implementation of ParameterizedType.
  • Loading branch information...
a-peyrard committed Apr 30, 2015
1 parent ceb7a5b commit 51982bb4d103fc72b079d514528707726bef6da9
@@ -14,7 +14,9 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
/**
* Date: 23/10/13
@@ -37,7 +39,25 @@ public Type getRawType() {
public Type getOwnerType() {
return rawType.getEnclosingClass();
}
};
@Override
public int hashCode() {
return Objects.hashCode(getOwnerType())
^ Objects.hashCode(rawType)
^ Arrays.hashCode(arguments);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) obj;
return Objects.equals(type.getOwnerType(), getOwnerType())
&& Objects.equals(type.getRawType(), rawType)
&& Arrays.equals(type.getActualTypeArguments(), arguments);
}
return false;
}
};
}
/**
@@ -9,6 +9,7 @@
import org.junit.Rule;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.ArrayList;
@@ -24,6 +25,38 @@
@Rule
public JUnitSoftAssertions softly = new JUnitSoftAssertions();
@Test
public void newParameterizedType_should_create_equals_parameters_types() {
Type parameterizedType1 = Types.newParameterizedType(List.class, String.class);
Type parameterizedType2 = Types.newParameterizedType(List.class, String.class);
softly.assertThat(parameterizedType1.equals(parameterizedType2)).isTrue();
Type typeReference = new TypeReference<List<String>>() {}.getType();
softly.assertThat(typeReference.equals(parameterizedType1)).isTrue();
}
static class GenericHolder<T> {
final T value;
GenericHolder(T value) {
this.value = value;
}
public T getValue() {
return value;
}
}
@Test
public void newParameterizedType_should_create_equals_inner_parameters_types() {
Type genericHolder1 = Types.newParameterizedType(GenericHolder.class, String.class);
Type genericHolder2 = Types.newParameterizedType(GenericHolder.class, String.class);
softly.assertThat(genericHolder1.equals(genericHolder2)).isTrue();
Type typeReference = new TypeReference<GenericHolder<String>>() {}.getType();
softly.assertThat(typeReference.equals(genericHolder1)).isTrue();
}
@Test
public void getRawType_should_return_the_class_of_non_parametrized_types() {
softly.assertThat(Types.getRawType(Integer.class)).isEqualTo(Integer.class);

0 comments on commit 51982bb

Please sign in to comment.