use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterCodeUnitReferencesTest method creates_correct_description_for_references.
@Test
public void creates_correct_description_for_references() {
JavaClasses javaClasses = new ClassFileImporter().importClasses(Origin.class, Target.class);
JavaClass originClass = javaClasses.get(Origin.class);
JavaClass targetClass = javaClasses.get(Target.class);
JavaMethod constructorReferenceOrigin = originClass.getMethod("referencesConstructor");
JavaConstructor targetConstructor = targetClass.getConstructor();
JavaMethod methodReferenceOrigin = originClass.getMethod("referencesMethod");
JavaMethod targetMethod = targetClass.getMethod("call");
JavaConstructorReference constructorReference = getOnlyElement(constructorReferenceOrigin.getConstructorReferencesFromSelf());
assertThat(constructorReference.getDescription()).isEqualTo(String.format("Method <%s> references constructor <%s> in (%s.java:%d)", constructorReferenceOrigin.getFullName(), targetConstructor.getFullName(), Origin.class.getSimpleName(), 9));
JavaMethodReference methodReference = getOnlyElement(originClass.getMethod("referencesMethod").getMethodReferencesFromSelf());
assertThat(methodReference.getDescription()).isEqualTo(String.format("Method <%s> references method <%s> in (%s.java:%d)", methodReferenceOrigin.getFullName(), targetMethod.getFullName(), Origin.class.getSimpleName(), 13));
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesNewerJavaVersionTest method resolves_method_call_targets.
@Test
@UseDataProvider("method_resolution_scenarios")
public void resolves_method_call_targets(Class<?> scenario) throws NoSuchMethodException {
JavaMethod origin = new ClassFileImporter().importPackagesOf(scenario).get(scenario).getMethod("scenario");
MethodCallTarget target = getOnlyElement(origin.getMethodCallsFromSelf()).getTarget();
Method methodWithAnnotation = findMethodWithAnnotation(scenario, ExpectedMethod.class);
// Sanity check that the expected method really is the one that would be found via Reflection, too
assertThat(target.getOwner().reflect().getMethod(target.getName())).as("Method resolved via Reflection").isEqualTo(methodWithAnnotation);
assertThat(target.resolveMember().get()).isEquivalentTo(methodWithAnnotation);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterTest method imports_enclosing_classes.
@Test
public void imports_enclosing_classes() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/innerclassimport"));
JavaClass classWithInnerClass = classes.get(ClassWithInnerClass.class);
JavaClass innerClass = classes.get(ClassWithInnerClass.Inner.class);
JavaClass anonymousClass = classes.get(ClassWithInnerClass.class.getName() + "$1");
JavaClass localClass = classes.get(ClassWithInnerClass.class.getName() + "$1LocalCaller");
JavaMethod calledTarget = getOnlyElement(classes.get(CalledClass.class).getMethods());
assertThat(innerClass.getEnclosingClass()).contains(classWithInnerClass);
assertThat(anonymousClass.getEnclosingClass()).contains(classWithInnerClass);
assertThat(localClass.getEnclosingClass()).contains(classWithInnerClass);
JavaMethodCall call = getOnlyElement(innerClass.getCodeUnitWithParameterTypes("call").getMethodCallsFromSelf());
assertThatCall(call).isFrom("call").isTo(calledTarget).inLineNumber(31);
call = getOnlyElement(anonymousClass.getCodeUnitWithParameterTypes("call").getMethodCallsFromSelf());
assertThatCall(call).isFrom("call").isTo(calledTarget).inLineNumber(11);
call = getOnlyElement(localClass.getCodeUnitWithParameterTypes("call").getMethodCallsFromSelf());
assertThatCall(call).isFrom("call").isTo(calledTarget).inLineNumber(21);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterTest method handles_synthetic_modifiers.
@Test
public void handles_synthetic_modifiers() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/syntheticimport"));
JavaField syntheticField = getOnlyElement(classes.get(ClassWithSynthetics.ClassWithSyntheticField.class).getFields());
assertThat(syntheticField.getModifiers()).as("modifiers of field in ClassWithSynthetics.ClassWithSyntheticField").contains(SYNTHETIC);
JavaMethod syntheticMethod = getMethodWithReturnType(classes.get(ClassWithSynthetics.ClassWithSyntheticMethod.class), Object.class);
assertThat(syntheticMethod.getModifiers()).as("modifiers of method in ClassWithSynthetics.ClassWithSyntheticMethod").contains(SYNTHETIC);
JavaMethod compareMethod = classes.get(ClassWithSynthetics.class).getMethod("compare", Object.class, Object.class);
assertThat(compareMethod.getModifiers()).as("modifiers of bridge method in ClassWithSynthetics").contains(BRIDGE, SYNTHETIC);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ArchConditionsTest method never_call_method_where_target_owner_is_assignable_to.
@Test
public void never_call_method_where_target_owner_is_assignable_to() {
JavaClass callingClass = importClassWithContext(CallingClass.class);
AccessesSimulator simulateCall = simulateCall();
JavaClass someClass = importClassWithContext(SomeClass.class);
JavaMethod doNotCallMe = someClass.getMethod("doNotCallMe");
JavaMethodCall callTodoNotCallMe = simulateCall.from(callingClass.getMethod("call"), 0).to(doNotCallMe);
ArchCondition<JavaClass> condition = never(callMethodWhere(target(name("doNotCallMe")).and(target(owner(assignableTo(SomeSuperclass.class))))));
assertThat(condition).checking(callingClass).containViolations(callTodoNotCallMe.getDescription());
condition = never(callMethodWhere(target(name("doNotCallMe")).and(target(owner(type(SomeSuperclass.class))))));
assertThat(condition).checking(callingClass).containNoViolation();
}
Aggregations