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());
}
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);
}
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);
}
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);
}
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());
}
Aggregations