Search in sources :

Example 46 with JavaMethod

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));
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 47 with JavaMethod

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);
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) OnlyLeftAncestorOverridesRootMethod(com.tngtech.archunit.core.importer.testexamples.methodresolution.OnlyLeftAncestorOverridesRootMethod) ExpectedMethod(com.tngtech.archunit.core.importer.testexamples.methodresolution.ExpectedMethod) Method(java.lang.reflect.Method) OnlyRightAncestorOverridesRootMethod(com.tngtech.archunit.core.importer.testexamples.methodresolution.OnlyRightAncestorOverridesRootMethod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 48 with JavaMethod

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

Example 49 with JavaMethod

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);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassWithSynthetics(com.tngtech.archunit.core.importer.testexamples.syntheticimport.ClassWithSynthetics) Test(org.junit.Test)

Example 50 with JavaMethod

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

Aggregations

JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)56 JavaClass (com.tngtech.archunit.core.domain.JavaClass)36 Test (org.junit.Test)32 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)22 ArchCondition (com.tngtech.archunit.lang.ArchCondition)21 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 List (java.util.List)12 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)10 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)9 Collectors (java.util.stream.Collectors)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 Stream (java.util.stream.Stream)8 Collections (java.util.Collections)7 JavaField (com.tngtech.archunit.core.domain.JavaField)6 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)5 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)5 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)5