Search in sources :

Example 36 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_self_referencing_type_definitions.

@Test
public void imports_complex_generic_field_type_with_multiple_nested_actual_type_arguments_with_self_referencing_type_definitions() {
    @SuppressWarnings("unused")
    class GenericFieldType<A, B, C> {
    }
    @SuppressWarnings("unused")
    class SomeClass<FIRST extends String & Serializable, SECOND extends Serializable & Cloneable> {

        GenericFieldType<List<? extends FIRST>, Map<Map.Entry<FIRST, Map.Entry<String, SECOND>>, Map<? extends String, Map<? extends Serializable, List<List<? extends Set<? super Iterable<? super Map<SECOND, ?>>>>>>>>, Comparable<SomeClass<FIRST, SECOND>>> // assigned to GenericFieldType<_,_,C>
        field;
    }
    JavaType genericFieldType = importClassWithOnlyGenericTypeResolution(SomeClass.class).getField("field").getType();
    // @formatter:off
    assertThatType(genericFieldType).as("generic field type").hasActualTypeArguments(// assigned to GenericFieldType<A,_,_>
    parameterizedType(List.class).withWildcardTypeParameterWithUpperBound(typeVariable("FIRST").withUpperBounds(String.class, Serializable.class)), // assigned to GenericFieldType<_,B,_>
    parameterizedType(Map.class).withTypeArguments(parameterizedType(Map.Entry.class).withTypeArguments(typeVariable("FIRST").withUpperBounds(String.class, Serializable.class), parameterizedType(Map.Entry.class).withTypeArguments(concreteClass(String.class), typeVariable("SECOND").withUpperBounds(Serializable.class, Cloneable.class))), parameterizedType(Map.class).withTypeArguments(wildcardType().withUpperBound(String.class), parameterizedType(Map.class).withTypeArguments(wildcardType().withUpperBound(Serializable.class), parameterizedType(List.class).withTypeArguments(parameterizedType(List.class).withTypeArguments(wildcardType().withUpperBound(parameterizedType(Set.class).withTypeArguments(wildcardType().withLowerBound(parameterizedType(Iterable.class).withTypeArguments(wildcardType().withLowerBound(parameterizedType(Map.class).withTypeArguments(typeVariable("SECOND").withUpperBounds(Serializable.class, Cloneable.class), wildcardType()))))))))))), // assigned to GenericFieldType<_,_,C>
    parameterizedType(Comparable.class).withTypeArguments(parameterizedType(SomeClass.class).withTypeArguments(typeVariable("FIRST").withUpperBounds(String.class, Serializable.class), typeVariable("SECOND").withUpperBounds(Serializable.class, Cloneable.class))));
// @formatter:on
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Serializable(java.io.Serializable) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 37 with JavaType

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

the class ClassFileImporterGenericFieldTypesTest method references_outer_type_variable_assigned_to_actual_type_argument_of_generic_field_type_of_inner_class.

@Test
public void references_outer_type_variable_assigned_to_actual_type_argument_of_generic_field_type_of_inner_class() {
    @SuppressWarnings("unused")
    class OuterWithTypeParameter<OUTER extends String> {

        class SomeInner {

            @SuppressWarnings("unused")
            class GenericFieldType<T> {
            }

            class SomeClass {

                GenericFieldType<OUTER> field;
            }
        }
    }
    JavaType genericFieldType = importClassesWithOnlyGenericTypeResolution(OuterWithTypeParameter.SomeInner.SomeClass.class, OuterWithTypeParameter.SomeInner.class, OuterWithTypeParameter.class).get(OuterWithTypeParameter.SomeInner.SomeClass.class).getField("field").getType();
    assertThatType(genericFieldType).as("generic field type").hasActualTypeArguments(typeVariable("OUTER").withUpperBounds(String.class));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 38 with JavaType

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

the class ClassFileImporterGenericInterfacesTest method imports_generic_interface_with_actual_type_arguments_parameterized_with_bounded_wildcards.

@Test
public void imports_generic_interface_with_actual_type_arguments_parameterized_with_bounded_wildcards() {
    class Child implements InterfaceWithTwoTypeParameters<ClassParameterWithSingleTypeParameter<? extends String>, ClassParameterWithSingleTypeParameter<? super File>> {
    }
    JavaType genericInterface = getOnlyElement(importClassWithOnlyGenericTypeResolution(Child.class).getInterfaces());
    assertThatType(genericInterface).as("generic interface").hasActualTypeArguments(parameterizedType(ClassParameterWithSingleTypeParameter.class).withWildcardTypeParameterWithUpperBound(String.class), parameterizedType(ClassParameterWithSingleTypeParameter.class).withWildcardTypeParameterWithLowerBound(File.class));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) File(java.io.File) Test(org.junit.Test)

Example 39 with JavaType

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

the class ClassFileImporterGenericInterfacesTest method imports_raw_generic_superclass_as_JavaClass_instead_of_JavaParameterizedType.

@Test
public void imports_raw_generic_superclass_as_JavaClass_instead_of_JavaParameterizedType() {
    @SuppressWarnings("rawtypes")
    class Child implements InterfaceWithOneTypeParameter {
    }
    JavaType rawGenericInterface = getOnlyElement(importClassWithOnlyGenericTypeResolution(Child.class).getInterfaces());
    assertThatType(rawGenericInterface).as("raw generic interface").matches(InterfaceWithOneTypeParameter.class);
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 40 with JavaType

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

the class ClassFileImporterGenericInterfacesTest method references_outer_type_variable_assigned_to_actual_type_argument_of_generic_interface_of_inner_class.

@Test
public void references_outer_type_variable_assigned_to_actual_type_argument_of_generic_interface_of_inner_class() {
    @SuppressWarnings("unused")
    class OuterWithTypeParameter<OUTER extends String> {

        class SomeInner {

            class Child implements InterfaceWithOneTypeParameter<OUTER> {
            }
        }
    }
    JavaType genericInterface = getOnlyElement(importClassesWithOnlyGenericTypeResolution(OuterWithTypeParameter.SomeInner.Child.class, OuterWithTypeParameter.SomeInner.class, OuterWithTypeParameter.class).get(OuterWithTypeParameter.SomeInner.Child.class).getInterfaces());
    assertThatType(genericInterface).as("generic interface").hasActualTypeArguments(typeVariable("OUTER").withUpperBounds(String.class));
}
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