Java Code Examples for com.tngtech.archunit.core.domain.JavaClass#getMethodCallsFromSelf()

The following examples show how to use com.tngtech.archunit.core.domain.JavaClass#getMethodCallsFromSelf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void imports_shadowed_and_superclass_method_calls() throws Exception {
    ImportedClasses classes = classesIn("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 = new MethodCallTargetBuilder()
            .withOwner(subClassWithCalledMethod)
            .withName(expectedSuperClassMethod.getName())
            .withParameters(expectedSuperClassMethod.getRawParameterTypes())
            .withReturnType(expectedSuperClassMethod.getRawReturnType())
            .withMethods(Suppliers.ofInstance(Collections.singleton(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);
}
 
Example 2
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void imports_non_unique_targets_for_diamond_scenarios() throws Exception {
    ImportedClasses diamondScenario = classesIn("testexamples/diamond");
    JavaClass classCallingDiamond = diamondScenario.get(ClassCallingDiamond.class);
    JavaClass diamondLeftInterface = diamondScenario.get(InterfaceB.class);
    JavaClass diamondRightInterface = diamondScenario.get(InterfaceC.class);
    JavaClass diamondPeakInterface = diamondScenario.get(InterfaceD.class);
    JavaClass diamondPeakClass = diamondScenario.get(ClassImplementingD.class);

    Set<JavaMethodCall> calls = classCallingDiamond.getMethodCallsFromSelf();

    assertThat(calls).hasSize(2);

    JavaCodeUnit callInterface = classCallingDiamond
            .getCodeUnitWithParameterTypes(ClassCallingDiamond.callInterface);
    JavaMethodCall callToInterface = getOnlyByCaller(calls, callInterface);
    assertThatCall(callToInterface)
            .isFrom(callInterface)
            .inLineNumber(ClassCallingDiamond.callInterfaceLineNumber);
    // NOTE: There is no java.lang.reflect.Method InterfaceD.implementMe(), because the method is inherited
    assertThat(callToInterface.getTarget().getName()).isEqualTo(InterfaceD.implementMe);
    assertThat(callToInterface.getTarget().getOwner()).isEqualTo(diamondPeakInterface);
    assertThat(callToInterface.getTarget().getRawParameterTypes()).isEmpty();
    assertThat(callToInterface.getTarget().resolve()).extracting("fullName")
            .containsOnly(
                    diamondLeftInterface.getMethod(InterfaceB.implementMe).getFullName(),
                    diamondRightInterface.getMethod(InterfaceB.implementMe).getFullName());

    JavaCodeUnit callImplementation = classCallingDiamond
            .getCodeUnitWithParameterTypes(ClassCallingDiamond.callImplementation);
    assertThatCall(getOnlyByCaller(calls, callImplementation))
            .isFrom(callImplementation)
            .isTo(diamondPeakClass.getMethod(InterfaceD.implementMe))
            .inLineNumber(ClassCallingDiamond.callImplementationLineNumber);
}