Search in sources :

Example 6 with JavaConstructor

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

the class ClassFileImporterAccessesTest method method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks.

@Test
public void method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks() {
    JavaClasses classes = new ClassFileImporter().importClasses(ClassWithComplexTryCatchBlocks.class, ClassHoldingMethods.class);
    JavaClass classHoldingMethods = classes.get(ClassHoldingMethods.class);
    JavaClass classWithTryCatchBlocks = classes.get(ClassWithComplexTryCatchBlocks.class);
    JavaMethod setSomeInt = classHoldingMethods.getMethod("setSomeInt", int.class);
    JavaMethod setSomeString = classHoldingMethods.getMethod("setSomeString", String.class);
    JavaMethod doSomething = classHoldingMethods.getMethod("doSomething");
    JavaConstructor constructor = classWithTryCatchBlocks.getConstructor();
    assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(12)).isNotWrappedInTryCatch();
    assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(14)).isWrappedWithTryCatchFor(Exception.class);
    assertThatCall(methodCallTo(doSomething).from(constructor).inLineNumber(17)).isWrappedWithTryCatchFor(Exception.class).isWrappedWithTryCatchFor(IllegalArgumentException.class).isWrappedWithTryCatchFor(UnsupportedOperationException.class);
    assertThatCall(methodCallTo(setSomeString).from(constructor).inLineNumber(26)).isWrappedWithTryCatchFor(Throwable.class);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 7 with JavaConstructor

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

the class ClassFileImporterAccessesTest method constructors_know_callers.

@Test
public void constructors_know_callers() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/dependents"));
    JavaClass classHoldingDependencies = classes.get(ClassHoldingDependencies.class);
    JavaClass firstClassWithDependency = classes.get(FirstClassWithDependency.class);
    JavaClass secondClassWithDependency = classes.get(SecondClassWithDependency.class);
    JavaConstructor targetConstructur = classHoldingDependencies.getConstructor();
    Set<JavaConstructorCall> calls = targetConstructur.getCallsOfSelf();
    Set<JavaConstructorCall> expected = ImmutableSet.<JavaConstructorCall>builder().addAll(getByTarget(classHoldingDependencies.getConstructorCallsFromSelf(), targetConstructur)).addAll(getByTarget(firstClassWithDependency.getConstructorCallsFromSelf(), targetConstructur)).addAll(getByTarget(secondClassWithDependency.getConstructorCallsFromSelf(), targetConstructur)).build();
    assertThat(calls).as("Default Constructor calls to ClassWithDependents").isEqualTo(expected);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 8 with JavaConstructor

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

the class ClassFileImporterAnnotationsTest method imports_parameter_annotations_correctly_for_synthetic_constructor_parameter.

@Test
public void imports_parameter_annotations_correctly_for_synthetic_constructor_parameter() {
    @SuppressWarnings("unused")
    class LocalClassThusSyntheticFirstConstructorParameter {

        LocalClassThusSyntheticFirstConstructorParameter(Object notAnnotated, @SimpleAnnotation("test") List<String> annotated) {
        }
    }
    JavaConstructor constructor = getOnlyElement(new ClassFileImporter().importClasses(LocalClassThusSyntheticFirstConstructorParameter.class, String.class).get(LocalClassThusSyntheticFirstConstructorParameter.class).getConstructors());
    List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = constructor.getParameterAnnotations();
    assertThat(parameterAnnotations).as("parameter annotations").hasSize(2);
    assertThatAnnotations(parameterAnnotations.get(0)).isEmpty();
    assertThatAnnotations(parameterAnnotations.get(1)).isNotEmpty();
    List<JavaParameter> parameters = constructor.getParameters();
    for (int i = 0; i < parameterAnnotations.size(); i++) {
        assertThat(parameterAnnotations.get(i)).isEqualTo(parameters.get(i).getAnnotations());
        assertThatAnnotations(parameterAnnotations.get(i)).match(ImmutableSet.copyOf(constructor.reflect().getParameterAnnotations()[i]));
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SimpleAnnotation(com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) List(java.util.List) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 9 with JavaConstructor

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_parameter_types.

@Test
public void automatically_resolves_constructor_parameter_types() {
    @SuppressWarnings("unused")
    class ConstructorParameterTypesWithoutAnyFurtherReference {

        ConstructorParameterTypesWithoutAnyFurtherReference(FileSystem constructorParam1, Buffer constructorParam2) {
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME).importClass(ConstructorParameterTypesWithoutAnyFurtherReference.class);
    JavaConstructor constructor = javaClass.getConstructor(getClass(), FileSystem.class, Buffer.class);
    assertThat(constructor.getRawParameterTypes().get(0)).as("constructor parameter type").isFullyImported(true);
    assertThat(constructor.getRawParameterTypes().get(1)).as("constructor parameter type").isFullyImported(true);
}
Also used : Buffer(java.nio.Buffer) JavaClass(com.tngtech.archunit.core.domain.JavaClass) FileSystem(java.nio.file.FileSystem) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 10 with JavaConstructor

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

the class ClassFileImporterAutomaticResolutionTest method importFirstTypeArgumentConstructorParameterBound.

private static JavaType importFirstTypeArgumentConstructorParameterBound(Class<?> clazz) {
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_PROPERTY_NAME, MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_DEFAULT_VALUE).importClass(clazz);
    JavaConstructor constructor = getOnlyElement(javaClass.getConstructors());
    return getFirstTypeArgumentUpperBound(constructor.getParameterTypes().get(0));
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor)

Aggregations

JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)13 Test (org.junit.Test)11 JavaClass (com.tngtech.archunit.core.domain.JavaClass)7 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)2 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)2 ArchCondition (com.tngtech.archunit.lang.ArchCondition)2 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)2 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)1 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 JavaParameter (com.tngtech.archunit.core.domain.JavaParameter)1 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)1 SimpleAnnotation (com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.SimpleAnnotation)1 ClassWithAnnotatedMethods (com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods)1 ClassWithComplexTryCatchBlocks (com.tngtech.archunit.core.importer.testexamples.trycatch.ClassWithComplexTryCatchBlocks)1