Skip to content
Open
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 @@ -866,6 +866,24 @@ public Class<?>[] resolveGenerics(Class<?> fallback) {
return getGeneric(indexes).resolve();
}

/**
* Convenience method that will {@link #getGeneric(int...) get} and
* {@link #resolve() resolve} a specific generic parameter.
* @param indexes the indexes that refer to the generic parameter
* (can be omitted to return the first generic)
* @return a resolved {@link Class}
* @throws IllegalStateException if no generic was available
* @see #resolveGeneric(int...)
* @see #getGeneric(int...)
* @see #resolve()
*/
@SuppressWarnings("unchecked")
public <T> Class<T> resolveRequiredGeneric(int... indexes) {
Class<?> generic = getGeneric(indexes).resolve();
Assert.state(generic != null, "The generic must not be null");
return (Class<T>) generic;
}

/**
* Resolve this type to a {@link java.lang.Class}, returning {@code null}
* if the type cannot be resolved. This method will consider bounds of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -1601,6 +1602,14 @@ void gh34541() throws Exception {
assertThat(typeWithGenerics.isAssignableFrom(PaymentCreator.class)).isTrue();
}

@Test
void resolveRequiredGeneric() {
assertThat(new AbstractRepository<String>() {}.entityClass).isEqualTo(String.class);
assertThat(new AbstractRepository<Long>() {}.entityClass).isEqualTo(Long.class);
assertThat(new AbstractRepository<>() {}.entityClass).isEqualTo(Object.class);
assertThatIllegalStateException().isThrownBy(() -> new AbstractRepository() {});
}


private ResolvableType testSerialization(ResolvableType type) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -2013,6 +2022,14 @@ static class PaymentCreatorParameter<T extends Payment> {
abstract static class Payment {
}

abstract class AbstractRepository<T> {

final Class<T> entityClass;

AbstractRepository() {
entityClass = ResolvableType.forClass(getClass()).as(AbstractRepository.class).resolveRequiredGeneric();
}
}

private static class ResolvableTypeAssert extends AbstractAssert<ResolvableTypeAssert, ResolvableType>{

Expand Down
Loading