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

The following examples show how to use com.tngtech.archunit.core.domain.JavaClass#getCodeUnitWithParameterTypes() . 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_constructor_calls_on_self() throws Exception {
    JavaClass classThatCallsOwnConstructor = classesIn("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);
}
 
Example 3
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void imports_constructor_calls_on_other() throws Exception {
    ImportedClasses classes = classesIn("testexamples/callimport");
    JavaClass classThatCallsOtherConstructor = classes.get(CallsOtherConstructor.class);
    JavaClass otherClass = classes.get(CallsOwnConstructor.class);
    JavaCodeUnit caller = classThatCallsOtherConstructor.getCodeUnitWithParameterTypes("createOther");

    Set<JavaConstructorCall> calls = classThatCallsOtherConstructor.getConstructorCallsFromSelf();

    assertThatCall(getOnlyByCaller(calls, caller))
            .isFrom(caller)
            .isTo(otherClass.getConstructor(String.class))
            .inLineNumber(5);
}
 
Example 4
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);
}