Search in sources :

Example 91 with JavaType

use of com.tngtech.archunit.core.domain.JavaType in project ArchUnit by TNG.

the class ClassFileImporterGenericFieldTypesTest method imports_generic_field_type_with_parameterized_array_bounds.

@Test
public void imports_generic_field_type_with_parameterized_array_bounds() {
    @SuppressWarnings("unused")
    class GenericFieldType<A, B, C> {
    }
    @SuppressWarnings("unused")
    class SomeClass {

        GenericFieldType<List<String>[], List<String[]>[][], List<String[][]>[][][]> field;
    }
    JavaType genericFieldType = importClassWithOnlyGenericTypeResolution(SomeClass.class).getField("field").getType();
    assertThatType(genericFieldType).hasActualTypeArguments(genericArray(parameterizedTypeArrayName(List.class, String.class, 1)).withComponentType(parameterizedType(List.class).withTypeArguments(String.class)), genericArray(parameterizedTypeArrayName(List.class, String[].class, 2)).withComponentType(genericArray(parameterizedTypeArrayName(List.class, String[].class, 1)).withComponentType(parameterizedType(List.class).withTypeArguments(String[].class))), genericArray(parameterizedTypeArrayName(List.class, String[][].class, 3)).withComponentType(genericArray(parameterizedTypeArrayName(List.class, String[][].class, 2)).withComponentType(genericArray(parameterizedTypeArrayName(List.class, String[][].class, 1)).withComponentType(parameterizedType(List.class).withTypeArguments(String[][].class)))));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) List(java.util.List) Test(org.junit.Test)

Example 92 with JavaType

use of com.tngtech.archunit.core.domain.JavaType in project ArchUnit by TNG.

the class ClassFileImporterGenericFieldTypesTest method imports_generic_field_type_with_array_type_argument.

@Test
public void imports_generic_field_type_with_array_type_argument() {
    @SuppressWarnings("unused")
    class GenericFieldType<T> {
    }
    @SuppressWarnings("unused")
    class SomeClass {

        GenericFieldType<String[]> field;
    }
    JavaType genericFieldType = importClassWithOnlyGenericTypeResolution(SomeClass.class).getField("field").getType();
    assertThatType(genericFieldType).as("generic field type").hasErasure(GenericFieldType.class).hasActualTypeArguments(String[].class);
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 93 with JavaType

use of com.tngtech.archunit.core.domain.JavaType in project ArchUnit by TNG.

the class ClassFileImporterGenericFieldTypesTest method considers_hierarchy_of_methods_and_classes_for_type_parameter_context.

@Test
public void considers_hierarchy_of_methods_and_classes_for_type_parameter_context() throws ClassNotFoundException {
    @SuppressWarnings("unused")
    class Level1<T1 extends String> {

        <T2 extends T1> void level2() {
            class Level3<T3 extends T2> {

                T3 field;
            }
        }
    }
    Class<?> innermostClass = Class.forName(Level1.class.getName() + "$1Level3");
    JavaType genericFieldType = importClassesWithOnlyGenericTypeResolution(innermostClass, Level1.class).get(innermostClass).getField("field").getType();
    assertThatType(genericFieldType).as("generic field type").matches(typeVariable("T3").withUpperBounds(typeVariable("T2").withUpperBounds(typeVariable("T1").withUpperBounds(String.class))));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 94 with JavaType

use of com.tngtech.archunit.core.domain.JavaType in project ArchUnit by TNG.

the class ClassFileImporterGenericFieldTypesTest method imports_complex_generic_field_type_with_multiple_nested_actual_type_arguments_with_generic_array_bounds.

@Test
public void imports_complex_generic_field_type_with_multiple_nested_actual_type_arguments_with_generic_array_bounds() {
    @SuppressWarnings("unused")
    class GenericFieldType<A, B, C> {
    }
    @SuppressWarnings("unused")
    class SomeClass<X extends Serializable, Y extends String> {

        GenericFieldType<List<X[]>, List<? extends X[][]>, Map<? super Y[], Map<Map<? super Y[][][], ?>, X[][]>>> field;
    }
    JavaType genericFieldType = importClassWithOnlyGenericTypeResolution(SomeClass.class).getField("field").getType();
    assertThatType(genericFieldType).hasActualTypeArguments(parameterizedType(List.class).withTypeArguments(genericArray(typeVariableArrayName("X", 1)).withComponentType(typeVariable("X").withUpperBounds(Serializable.class))), parameterizedType(List.class).withWildcardTypeParameterWithUpperBound(genericArray(typeVariableArrayName("X", 2)).withComponentType(genericArray(typeVariableArrayName("X", 1)).withComponentType(typeVariable("X").withUpperBounds(Serializable.class)))), parameterizedType(Map.class).withTypeArguments(wildcardType().withLowerBound(genericArray(typeVariableArrayName("Y", 1)).withComponentType(typeVariable("Y").withUpperBounds(String.class))), parameterizedType(Map.class).withTypeArguments(parameterizedType(Map.class).withTypeArguments(wildcardType().withLowerBound(genericArray(typeVariableArrayName("Y", 3)).withComponentType(genericArray(typeVariableArrayName("Y", 2)).withComponentType(genericArray(typeVariableArrayName("Y", 1)).withComponentType(typeVariable("Y").withUpperBounds(String.class))))), wildcardType()), genericArray(typeVariableArrayName("X", 2)).withComponentType(genericArray(typeVariableArrayName("X", 1)).withComponentType(typeVariable("X").withUpperBounds(Serializable.class))))));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Serializable(java.io.Serializable) Map(java.util.Map) Test(org.junit.Test)

Example 95 with JavaType

use of com.tngtech.archunit.core.domain.JavaType in project ArchUnit by TNG.

the class ClassFileImporterGenericFieldTypesTest method imports_generic_field_type_with_single_unbound_wildcard.

@Test
public void imports_generic_field_type_with_single_unbound_wildcard() {
    @SuppressWarnings("unused")
    class GenericFieldType<T> {
    }
    @SuppressWarnings("unused")
    class SomeClass {

        GenericFieldType<?> field;
    }
    JavaType genericFieldType = importClassWithOnlyGenericTypeResolution(SomeClass.class).getField("field").getType();
    assertThatType(genericFieldType).as("generic field type").hasActualTypeArguments(wildcardType());
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Aggregations

JavaType (com.tngtech.archunit.core.domain.JavaType)133 Test (org.junit.Test)132 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)31 Serializable (java.io.Serializable)29 Map (java.util.Map)23 File (java.io.File)17 List (java.util.List)13 Reference (java.lang.ref.Reference)5 JavaClass (com.tngtech.archunit.core.domain.JavaClass)3 JavaTypeVariable (com.tngtech.archunit.core.domain.JavaTypeVariable)3 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)1 SomeDeeplyNestedInterface (com.tngtech.archunit.core.importer.ClassFileImporterGenericInterfacesTest.Outer.SomeNestedInterface.SomeDeeplyNestedInterface)1 Path (java.nio.file.Path)1 Set (java.util.Set)1