Search in sources :

Example 1 with JavaCodeUnit

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);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) Test(org.junit.Test)

Example 2 with JavaCodeUnit

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());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) CodeUnit(com.tngtech.archunit.core.importer.RawAccessRecord.CodeUnit)

Example 3 with JavaCodeUnit

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);
}
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 4 with JavaCodeUnit

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);
}
Also used : JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 5 with JavaCodeUnit

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);
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Aggregations

JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)8 JavaClass (com.tngtech.archunit.core.domain.JavaClass)7 Test (org.junit.Test)7 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)3 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)1 AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 CodeUnit (com.tngtech.archunit.core.importer.RawAccessRecord.CodeUnit)1