use of com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget in project ArchUnit by TNG.
the class AccessTargetNewerJavaVersionTest method function_resolve_member.
@Test
public void function_resolve_member() {
@SuppressWarnings("unused")
class Origin {
String access() {
Data_function_resolve_member.Target target = new Data_function_resolve_member.Target();
Supplier<Data_function_resolve_member.Target> supplier = Data_function_resolve_member.Target::new;
Consumer<Data_function_resolve_member.Target> consumer = Data_function_resolve_member.Target::method;
target.method();
return target.field;
}
}
JavaClass targetClass = new ClassFileImporter().importClasses(Origin.class, Data_function_resolve_member.Target.class).get(Data_function_resolve_member.Target.class);
MethodCallTarget methodCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodCallTarget.class);
assertThat(AccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
assertThat(AccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
assertThat(CodeUnitAccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
assertThat(CodeUnitAccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
assertThat(MethodCallTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
assertThat(MethodCallTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
MethodReferenceTarget methodReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodReferenceTarget.class);
assertThat(MethodReferenceTarget.Functions.RESOLVE_MEMBER.apply(methodReferenceTarget)).contains(methodReferenceTarget.resolveMember().get());
ConstructorCallTarget constructorCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorCallTarget.class);
assertThat(ConstructorCallTarget.Functions.RESOLVE_MEMBER.apply(constructorCallTarget)).contains(constructorCallTarget.resolveMember().get());
assertThat(ConstructorCallTarget.Functions.RESOLVE.apply(constructorCallTarget)).isEqualTo(ImmutableSet.of(constructorCallTarget.resolveMember().get()));
ConstructorReferenceTarget constructorReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorReferenceTarget.class);
assertThat(ConstructorReferenceTarget.Functions.RESOLVE_MEMBER.apply(constructorReferenceTarget)).contains(constructorReferenceTarget.resolveMember().get());
FieldAccessTarget fieldAccessTarget = findTargetWithType(targetClass.getAccessesToSelf(), FieldAccessTarget.class);
assertThat(FieldAccessTarget.Functions.RESOLVE_MEMBER.apply(fieldAccessTarget)).contains(fieldAccessTarget.resolveMember().get());
assertThat(FieldAccessTarget.Functions.RESOLVE.apply(fieldAccessTarget)).isEqualTo(ImmutableSet.of(fieldAccessTarget.resolveMember().get()));
}
use of com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_on_overridden_external_class.
@Test
public void imports_method_calls_on_overridden_external_class() {
JavaClass classThatCallsExternalMethod = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(ExternalOverriddenMethodCall.class);
JavaMethodCall call = getOnlyElement(classThatCallsExternalMethod.getMethodCallsFromSelf());
assertThatCall(call).isFrom(classThatCallsExternalMethod.getCodeUnitWithParameterTypes("call")).inLineNumber(9);
MethodCallTarget target = call.getTarget();
assertThat(target.getFullName()).isEqualTo(ChildClass.class.getName() + ".overrideMe()");
assertThat(resolve(target).getFullName()).isEqualTo(ChildClass.class.getName() + ".overrideMe()");
assertThat(reflect(target)).isEqualTo(method(ChildClass.class, "overrideMe"));
}
use of com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_on_external_class.
@Test
public void imports_method_calls_on_external_class() {
JavaClass classThatCallsExternalMethod = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsExternalMethod.class);
JavaMethodCall call = getOnlyElement(classThatCallsExternalMethod.getMethodCallsFromSelf());
assertThatCall(call).isFrom(classThatCallsExternalMethod.getCodeUnitWithParameterTypes("getString")).inLineNumber(7);
MethodCallTarget target = call.getTarget();
assertThat(target.getOwner().reflect()).isEqualTo(ArrayList.class);
assertThat(target.getFullName()).isEqualTo(ArrayList.class.getName() + ".toString()");
}
use of com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_shadowed_and_superclass_method_calls.
@Test
public void imports_shadowed_and_superclass_method_calls() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/hierarchicalmethodcall"));
JavaClass classThatCallsMethodOfSuperclass = classes.get(CallOfSuperAndSubclassMethod.class);
JavaClass superclassWithCalledMethod = classes.get(SuperclassWithCalledMethod.class);
JavaClass subClassWithCalledMethod = classes.get(SubclassWithCalledMethod.class);
Set<JavaMethodCall> calls = classThatCallsMethodOfSuperclass.getMethodCallsFromSelf();
assertThat(calls).hasSize(2);
JavaCodeUnit callSuperclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSuperclassMethod);
JavaMethod expectedSuperclassMethod = superclassWithCalledMethod.getMethod(SuperclassWithCalledMethod.method);
MethodCallTarget expectedSuperclassCall = newMethodCallTargetBuilder().withOwner(subClassWithCalledMethod).withName(expectedSuperclassMethod.getName()).withParameters(expectedSuperclassMethod.getRawParameterTypes()).withReturnType(expectedSuperclassMethod.getRawReturnType()).withMember(() -> Optional.of(expectedSuperclassMethod)).build();
assertThatCall(getOnlyByCaller(calls, callSuperclassMethod)).isFrom(callSuperclassMethod).isTo(expectedSuperclassCall).inLineNumber(CallOfSuperAndSubclassMethod.callSuperclassLineNumber);
JavaCodeUnit callSubclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSubclassMethod);
assertThatCall(getOnlyByCaller(calls, callSubclassMethod)).isFrom(callSubclassMethod).isTo(subClassWithCalledMethod.getMethod(SubclassWithCalledMethod.maskedMethod)).inLineNumber(CallOfSuperAndSubclassMethod.callSubclassLineNumber);
}
use of com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_method_calls_that_return_Arrays.
@Test
public void imports_method_calls_that_return_Arrays() {
JavaClass classThatCallsMethodReturningArray = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsMethodReturningArray.class);
MethodCallTarget target = getOnlyElement(classThatCallsMethodReturningArray.getMethodCallsFromSelf()).getTarget();
assertThatType(target.getOwner()).matches(CallsMethodReturningArray.SomeEnum.class);
assertThatType(target.getRawReturnType()).matches(CallsMethodReturningArray.SomeEnum[].class);
}
Aggregations