Search in sources :

Example 6 with JavaType

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

the class ClassFileImporterGenericSuperclassTest 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 BaseClass<T> {
            }
            class Level3<T3 extends T2> extends BaseClass<T3> {
            }
        }
    }
    Class<?> innermostClass = Class.forName(Level1.class.getName() + "$1Level3");
    JavaType genericSuperclass = importClassesWithOnlyGenericTypeResolution(innermostClass, Level1.class).get(innermostClass).getSuperclass().get();
    assertThatType(genericSuperclass).as("generic superclass").hasActualTypeArguments(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 7 with JavaType

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

the class ClassFileImporterGenericSuperclassTest method creates_new_stub_type_variables_for_type_variables_of_enclosing_classes_that_are_out_of_context_for_generic_superclass_of_inner_class.

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

        class SomeInner {

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

            class Child extends BaseClass<OUTER> {
            }
        }
    }
    JavaType genericSuperclass = resetConfigurationAround(() -> {
        ArchConfiguration.get().setResolveMissingDependenciesFromClassPath(false);
        return importClassWithOnlyGenericTypeResolution(OuterWithTypeParameter.SomeInner.Child.class).getSuperclass().get();
    });
    ;
    assertThatType(genericSuperclass).as("generic superclass").hasActualTypeArguments(typeVariable("OUTER").withoutUpperBounds());
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 8 with JavaType

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

the class ClassFileImporterGenericSuperclassTest method imports_generic_superclass_with_actual_type_argument_parameterized_with_type_variable.

@Test
public void imports_generic_superclass_with_actual_type_argument_parameterized_with_type_variable() {
    @SuppressWarnings("unused")
    class BaseClass<SUPER> {
    }
    class Child<SUB> extends BaseClass<ClassParameterWithSingleTypeParameter<SUB>> {
    }
    JavaType genericSuperclass = importClassWithOnlyGenericTypeResolution(Child.class).getSuperclass().get();
    assertThatType(genericSuperclass).as("generic superclass").hasActualTypeArguments(parameterizedType(ClassParameterWithSingleTypeParameter.class).withTypeArguments(typeVariable("SUB")));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 9 with JavaType

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

the class ClassFileImporterGenericSuperclassTest method imports_generic_superclass_with_single_actual_type_argument_parameterized_with_concrete_class.

@Test
public void imports_generic_superclass_with_single_actual_type_argument_parameterized_with_concrete_class() {
    @SuppressWarnings("unused")
    class BaseClass<T> {
    }
    class Child extends BaseClass<ClassParameterWithSingleTypeParameter<String>> {
    }
    JavaType genericSuperclass = importClassWithOnlyGenericTypeResolution(Child.class).getSuperclass().get();
    assertThatType(genericSuperclass).as("generic superclass").hasActualTypeArguments(parameterizedType(ClassParameterWithSingleTypeParameter.class).withTypeArguments(String.class));
}
Also used : JavaType(com.tngtech.archunit.core.domain.JavaType) Test(org.junit.Test)

Example 10 with JavaType

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

the class ClassFileImporterGenericSuperclassTest method creates_new_stub_type_variables_for_wildcards_bound_by_type_variables_of_enclosing_classes_that_are_out_of_context.

@Test
public void creates_new_stub_type_variables_for_wildcards_bound_by_type_variables_of_enclosing_classes_that_are_out_of_context() {
    @SuppressWarnings("unused")
    class OuterWithTypeParameter<OUTER_ONE extends String, OUTER_TWO extends Serializable> {

        class SomeInner {

            @SuppressWarnings("unused")
            class BaseClass<A, B> {
            }

            class Child extends BaseClass<ClassParameterWithSingleTypeParameter<? extends OUTER_ONE>, ClassParameterWithSingleTypeParameter<? super OUTER_TWO>> {
            }
        }
    }
    JavaType genericSuperclass = resetConfigurationAround(() -> {
        ArchConfiguration.get().setResolveMissingDependenciesFromClassPath(false);
        return importClassesWithOnlyGenericTypeResolution(OuterWithTypeParameter.SomeInner.Child.class, ClassParameterWithSingleTypeParameter.class).get(OuterWithTypeParameter.SomeInner.Child.class).getSuperclass().get();
    });
    assertThatType(genericSuperclass).as("generic superclass").hasActualTypeArguments(parameterizedType(ClassParameterWithSingleTypeParameter.class).withWildcardTypeParameterWithUpperBound(typeVariable("OUTER_ONE").withoutUpperBounds()), parameterizedType(ClassParameterWithSingleTypeParameter.class).withWildcardTypeParameterWithLowerBound(typeVariable("OUTER_TWO").withoutUpperBounds()));
}
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