|
6 | 6 | import build.codemodel.foundation.usage.GenericTypeUsage; |
7 | 7 | import build.codemodel.foundation.usage.NamedTypeUsage; |
8 | 8 | import build.codemodel.foundation.usage.WildcardTypeUsage; |
| 9 | +import build.codemodel.foundation.usage.TypeVariableUsage; |
| 10 | +import build.codemodel.jdk.example.BoundedContainer; |
9 | 11 | import build.codemodel.jdk.example.WildcardContainer; |
10 | 12 | import build.codemodel.hierarchical.descriptor.HierarchicalTypeDescriptor; |
11 | 13 | import build.codemodel.jdk.descriptor.JDKTypeDescriptor; |
|
20 | 22 | import build.codemodel.objectoriented.descriptor.FieldDescriptor; |
21 | 23 | import build.codemodel.objectoriented.descriptor.ImplementsTypeDescriptor; |
22 | 24 | import build.codemodel.objectoriented.descriptor.MethodDescriptor; |
| 25 | +import build.codemodel.objectoriented.descriptor.ParameterizedTypeDescriptor; |
23 | 26 | import org.junit.jupiter.api.Test; |
24 | 27 |
|
25 | 28 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -409,4 +412,37 @@ void shouldDiscoverWildcardBoundsViaReflection() { |
409 | 412 | assertThat(unboundedWildcard.upperBound()).isEmpty(); |
410 | 413 | assertThat(unboundedWildcard.lowerBound()).isEmpty(); |
411 | 414 | } |
| 415 | + |
| 416 | + @Test |
| 417 | + void shouldDiscoverTypeParameterDeclarationViaReflection() { |
| 418 | + final var codeModel = createCodeModel(); |
| 419 | + final var descriptor = codeModel.getJDKTypeDescriptor(Container.class).orElseThrow(); |
| 420 | + |
| 421 | + assertThat(descriptor.getTrait(ParameterizedTypeDescriptor.class)).isPresent(); |
| 422 | + |
| 423 | + final var typeVars = descriptor.getTrait(ParameterizedTypeDescriptor.class) |
| 424 | + .orElseThrow() |
| 425 | + .typeVariables() |
| 426 | + .toList(); |
| 427 | + assertThat(typeVars).hasSize(1); |
| 428 | + assertThat(typeVars.getFirst().typeName().name().toString()).isEqualTo("T"); |
| 429 | + } |
| 430 | + |
| 431 | + @Test |
| 432 | + void shouldDiscoverBoundedTypeParameterDeclarationViaReflection() { |
| 433 | + final var codeModel = createCodeModel(); |
| 434 | + final var descriptor = codeModel.getJDKTypeDescriptor(BoundedContainer.class).orElseThrow(); |
| 435 | + |
| 436 | + assertThat(descriptor.getTrait(ParameterizedTypeDescriptor.class)).isPresent(); |
| 437 | + |
| 438 | + final var typeVar = descriptor.getTrait(ParameterizedTypeDescriptor.class) |
| 439 | + .orElseThrow() |
| 440 | + .typeVariables() |
| 441 | + .findFirst() |
| 442 | + .orElseThrow(); |
| 443 | + assertThat(typeVar).isInstanceOf(TypeVariableUsage.class); |
| 444 | + assertThat(typeVar.typeName().name().toString()).isEqualTo("T"); |
| 445 | + assertThat(typeVar.upperBound()).isPresent(); |
| 446 | + assertThat(((NamedTypeUsage) typeVar.upperBound().get()).typeName().toString()).contains("Number"); |
| 447 | + } |
412 | 448 | } |
0 commit comments