use of com.tngtech.archunit.core.domain.JavaCodeUnit in project git-machete-intellij-plugin by VirtusLab.
the class ClassStructureTestSuite method actions_overriding_onUpdate_should_call_super_onUpdate.
@Test
public void actions_overriding_onUpdate_should_call_super_onUpdate() {
classes().that().areAssignableTo(com.virtuslab.gitmachete.frontend.actions.base.BaseProjectDependentAction.class).and().areNotAssignableFrom(com.virtuslab.gitmachete.frontend.actions.base.BaseProjectDependentAction.class).and(new DescribedPredicate<JavaClass>("override onUpdate method") {
@Override
public boolean apply(JavaClass input) {
return input.getMethods().stream().anyMatch(method -> method.getName().equals("onUpdate"));
}
}).should().callMethodWhere(new DescribedPredicate<JavaMethodCall>("name is onUpdate and owner is the direct superclass") {
@Override
public boolean apply(JavaMethodCall input) {
// where is the method called from?
JavaCodeUnit origin = input.getOrigin();
// where is the method declared?
AccessTarget.MethodCallTarget target = input.getTarget();
if (origin.getName().equals("onUpdate") && target.getName().equals("onUpdate")) {
return target.getOwner().equals(origin.getOwner().getSuperclass().orElse(null));
}
return false;
}
}).check(importedClasses);
}
use of com.tngtech.archunit.core.domain.JavaCodeUnit in project ArchUnit by TNG.
the class ClassGraphCreator method createEnclosingCodeUnit.
@Override
public Optional<JavaCodeUnit> createEnclosingCodeUnit(JavaClass owner) {
Optional<CodeUnit> enclosingCodeUnit = importRecord.getEnclosingCodeUnitFor(owner.getName());
if (!enclosingCodeUnit.isPresent()) {
return Optional.empty();
}
CodeUnit codeUnit = enclosingCodeUnit.get();
JavaClass enclosingClass = classes.getOrResolve(codeUnit.getDeclaringClassName());
return enclosingClass.tryGetCodeUnitWithParameterTypeNames(codeUnit.getName(), codeUnit.getRawParameterTypeNames());
}
use of com.tngtech.archunit.core.domain.JavaCodeUnit 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.JavaCodeUnit in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_origin_and_target_of_call_from_bridge_method_correctly.
@Test
public void imports_origin_and_target_of_call_from_bridge_method_correctly() {
class Parent {
@SuppressWarnings("unused")
Object covariantlyOverriddenCausingBridgeMethod() {
return null;
}
}
class Child extends Parent {
@Override
String covariantlyOverriddenCausingBridgeMethod() {
return null;
}
}
JavaMethodCall callFromBridgeMethodToOverriddenOne = getOnlyElement(new ClassFileImporter().importClasses(Parent.class, Child.class).get(Child.class).getMethodCallsFromSelf());
JavaCodeUnit bridgeMethod = callFromBridgeMethodToOverriddenOne.getOrigin();
assertThat(bridgeMethod.getName()).isEqualTo("covariantlyOverriddenCausingBridgeMethod");
assertThatType(bridgeMethod.getRawReturnType()).as("Return type of bridge method").matches(Object.class);
assertThat(bridgeMethod.getModifiers()).as("modifiers of bridge method").contains(BRIDGE, SYNTHETIC);
}
use of com.tngtech.archunit.core.domain.JavaCodeUnit 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);
}
Aggregations