Search in sources :

Example 1 with JavaConstructorCall

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

the class ClassFileImporterLambdaAccessesTest method imports_constructor_call_from_lambda_without_parameter.

@Test
public void imports_constructor_call_from_lambda_without_parameter() {
    class Target {
    }
    @SuppressWarnings({ "unused", "Convert2MethodRef" })
    class Caller {

        Runnable call() {
            return () -> new Target();
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    JavaConstructorCall call = getOnlyElement(filterOriginByName(classes.get(Caller.class).getConstructorCallsFromSelf(), "call"));
    assertThatCall(call).isFrom("call").isTo(Target.class, CONSTRUCTOR_NAME, getClass());
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 2 with JavaConstructorCall

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

the class ClassFileImporterAccessesTest method imports_constructor_calls_on_self.

@Test
public void imports_constructor_calls_on_self() {
    JavaClass classThatCallsOwnConstructor = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnConstructor.class);
    JavaCodeUnit caller = classThatCallsOwnConstructor.getCodeUnitWithParameterTypes("copy");
    Set<JavaConstructorCall> calls = classThatCallsOwnConstructor.getConstructorCallsFromSelf();
    assertThatCall(getOnlyByCaller(calls, caller)).isFrom(caller).isTo(classThatCallsOwnConstructor.getConstructor(String.class)).inLineNumber(8);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 3 with JavaConstructorCall

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_call_target_owners.

@Test
public void automatically_resolves_constructor_call_target_owners() {
    class Target {
    }
    @SuppressWarnings("unused")
    class Origin {

        void resolvesConstructorCallTargetOwner() {
            new Target();
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
    JavaConstructorCall call = getOnlyElement(javaClass.getMethod("resolvesConstructorCallTargetOwner").getConstructorCallsFromSelf());
    assertThat(call.getTargetOwner()).isFullyImported(true);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 4 with JavaConstructorCall

use of com.tngtech.archunit.core.domain.JavaConstructorCall 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 5 with JavaConstructorCall

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

the class ClassFileImporterAccessesTest method imports_constructor_calls_on_external_class.

@Test
public void imports_constructor_calls_on_external_class() throws Exception {
    JavaClass classThatCallsOwnConstructor = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnConstructor.class);
    JavaCodeUnit constructorCallingObjectInit = classThatCallsOwnConstructor.getConstructor(String.class);
    JavaConstructorCall objectInitCall = getOnlyElement(constructorCallingObjectInit.getConstructorCallsFromSelf());
    assertThatCall(objectInitCall).isFrom(constructorCallingObjectInit).inLineNumber(4);
    ConstructorCallTarget target = objectInitCall.getTarget();
    assertThat(target.getFullName()).isEqualTo(Object.class.getName() + ".<init>()");
    assertThat(reflect(target)).isEqualTo(Object.class.getConstructor());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Aggregations

JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)10 Test (org.junit.Test)8 JavaClass (com.tngtech.archunit.core.domain.JavaClass)6 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)5 ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)3 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)3 ImmutableSet (com.google.common.collect.ImmutableSet)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 JavaConstructorCallBuilder (com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorCallBuilder)1 Supplier (java.util.function.Supplier)1