Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArC: implement equals/hashCode for TypeVariableImpl and WildcardTypeImpl #34098

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

public class TypeVariableImpl<D extends GenericDeclaration> implements TypeVariable<D> {
Expand Down Expand Up @@ -55,6 +56,32 @@ public AnnotatedType[] getAnnotatedBounds() {
throw new UnsupportedOperationException();
}

@Override
public int hashCode() {
// This implementation is not compatible with JDK/guava,
// but since it's not possible to implement a compatible equals() anyway,
// it does not really matter.
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode(name);
result = prime * result + Objects.hashCode(bounds);
return result;
}

@Override
public boolean equals(Object obj) {
// Note that JDK does not make it possible to implement a compatible equals()
// as it checks a specific implementation class in its equals() method
if (this == obj) {
return true;
}
if (!(obj instanceof TypeVariable)) {
return false;
}
TypeVariable<?> other = (TypeVariable<?>) obj;
return Objects.equals(name, other.getName()) && Arrays.equals(getBounds(), other.getBounds());
}

@Override
public String toString() {
StringJoiner joiner = new StringJoiner(" & ", " extends ", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.Arrays;

/**
* This code was mainly copied from Weld codebase.
Expand Down Expand Up @@ -48,4 +49,25 @@ public Type[] getUpperBounds() {
public Type[] getLowerBounds() {
return lowerBound;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof WildcardType)) {
return false;
}
WildcardType other = (WildcardType) obj;
return Arrays.equals(lowerBound, other.getLowerBounds()) && Arrays.equals(upperBound, other.getUpperBounds());
}

@Override
public int hashCode() {
// We deliberately use the logic from JDK/guava
return Arrays.hashCode(lowerBound) ^ Arrays.hashCode(upperBound);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.quarkus.arc.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.List;

import jakarta.enterprise.util.TypeLiteral;

import org.junit.jupiter.api.Test;

public class ParameterizedTypeImplTest {

@SuppressWarnings("serial")
@Test
public void testEqualsAndHashCode() {
// List<?>
ParameterizedTypeImpl parameterizedType1 = new ParameterizedTypeImpl(List.class, WildcardTypeImpl.defaultInstance());
TypeLiteral<List<?>> literal1 = new TypeLiteral<List<?>>() {
};
assertEquals(parameterizedType1, literal1.getType());
assertEquals(parameterizedType1.hashCode(), literal1.hashCode());
assertEquals(parameterizedType1,
new ParameterizedTypeImpl(List.class, WildcardTypeImpl.defaultInstance()));

// List<String>
ParameterizedTypeImpl parameterizedType2 = new ParameterizedTypeImpl(List.class, String.class);
TypeLiteral<List<String>> literal2 = new TypeLiteral<List<String>>() {
};
assertEquals(parameterizedType2,
new ParameterizedTypeImpl(List.class, String.class));
assertEquals(parameterizedType2, literal2.getType());
assertEquals(parameterizedType2.hashCode(), literal2.getType().hashCode());

// List<? extends Number>
ParameterizedTypeImpl parameterizedType3 = new ParameterizedTypeImpl(List.class,
WildcardTypeImpl.withUpperBound(Number.class));
TypeLiteral<List<? extends Number>> literal3 = new TypeLiteral<List<? extends Number>>() {
};
assertEquals(parameterizedType3, literal3.getType());
assertEquals(parameterizedType3.hashCode(), literal3.getType().hashCode());
assertEquals(parameterizedType3.hashCode(), new ParameterizedTypeImpl(List.class,
WildcardTypeImpl.withUpperBound(Number.class)).hashCode());
assertNotEquals(parameterizedType3, parameterizedType1);
mkouba marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.arc.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Test;

public class TypeVariableImplTest {

@Test
public void testEqualsAndHashCode() {
assertEquals(new TypeVariableImpl<>("T"), new TypeVariableImpl<>("T"));
assertNotEquals(new TypeVariableImpl<>("T"), new TypeVariableImpl<>("T", String.class));
assertEquals(new TypeVariableImpl<>("T").hashCode(), new TypeVariableImpl<>("T").hashCode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.arc.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Test;

public class WildcardTypeImplTest {

@Test
public void testEqualsAndHashCode() {
assertEquals(WildcardTypeImpl.defaultInstance(), WildcardTypeImpl.withUpperBound(Object.class));
assertEquals(WildcardTypeImpl.withLowerBound(String.class), WildcardTypeImpl.withLowerBound(String.class));
assertNotEquals(WildcardTypeImpl.withLowerBound(String.class), WildcardTypeImpl.withLowerBound(Integer.class));
assertEquals(WildcardTypeImpl.defaultInstance().hashCode(), WildcardTypeImpl.withUpperBound(Object.class).hashCode());
assertEquals(WildcardTypeImpl.withLowerBound(String.class).hashCode(),
WildcardTypeImpl.withLowerBound(String.class).hashCode());
}

}