Search in sources :

Example 1 with JavaMethodCall

use of com.tngtech.archunit.core.domain.JavaMethodCall 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 JavaMethodCall

use of com.tngtech.archunit.core.domain.JavaMethodCall in project arch-unit-build-plugin-core by societe-generale.

the class NoJodaTimeRuleTest method notUseJodaTime.

protected static ArchCondition<JavaClass> notUseJodaTime() {
    return new ArchCondition<JavaClass>("not use Joda time ") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            List<JavaField> classesWithJodaTimeFields = item.getAllFields().stream().filter(this::isJodaTimeField).collect(toList());
            for (JavaField field : classesWithJodaTimeFields) {
                events.add(SimpleConditionEvent.violated(field, NO_JODA_VIOLATION_MESSAGE + " - class: " + field.getOwner().getName() + " - field name: " + field.getName()));
            }
            List<JavaMethodCall> methodsUsingJodaTimeInternally = item.getCodeUnits().stream().filter(codeUnit -> codeUnit instanceof JavaMethod).flatMap(method -> method.getMethodCallsFromSelf().stream()).filter(method -> method instanceof JavaMethodCall).filter(this::isMethodUsingJodaTimeInternally).collect(toList());
            for (JavaMethodCall methodCall : methodsUsingJodaTimeInternally) {
                events.add(SimpleConditionEvent.violated(methodCall.getOriginOwner(), NO_JODA_VIOLATION_MESSAGE + " - class: " + methodCall.getOriginOwner().getName() + " - method: " + methodCall.getTarget().getOwner().getSimpleName() + "." + methodCall.getTarget().getName() + " - line: " + methodCall.getLineNumber()));
            }
        }

        private boolean isJodaTimeField(JavaField field) {
            return field.getRawType().getName().startsWith(JODATIME_PACKAGE_PREFIX);
        }

        private boolean isMethodUsingJodaTimeInternally(JavaMethodCall javaMethodCall) {
            return javaMethodCall.getTarget().getFullName().startsWith(JODATIME_PACKAGE_PREFIX);
        }
    };
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Collection(java.util.Collection) JavaField(com.tngtech.archunit.core.domain.JavaField) ArchUtils(com.societegenerale.commons.plugin.utils.ArchUtils) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchRuleDefinition.classes(com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ScopePathProvider(com.societegenerale.commons.plugin.service.ScopePathProvider) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 3 with JavaMethodCall

use of com.tngtech.archunit.core.domain.JavaMethodCall in project sirius-components by eclipse-sirius.

the class AbstractServicesTests method nonAuditedRepositoryMethod.

private DescribedPredicate<JavaMethodCall> nonAuditedRepositoryMethod() {
    return new // $NON-NLS-1$
    DescribedPredicate<>(// $NON-NLS-1$
    "the repository method called has not been audited") {

        @Override
        public boolean apply(JavaMethodCall javaMethodCall) {
            JavaClass targetJavaClass = javaMethodCall.getTargetOwner();
            String fullName = targetJavaClass.getFullName();
            // $NON-NLS-1$
            boolean isRepository = fullName.endsWith("Repository");
            boolean hasBeenAudited = javaMethodCall.getTarget().isAnnotatedWith(Audited.class);
            return isRepository && !hasBeenAudited;
        }
    };
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall)

Example 4 with JavaMethodCall

use of com.tngtech.archunit.core.domain.JavaMethodCall in project ArchUnit by TNG.

the class ClassFileImporterLambdaAccessesTest method imports_multiple_method_calls_from_single_lambda.

@Test
public void imports_multiple_method_calls_from_single_lambda() {
    class Target {

        void target() {
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        Runnable call() {
            return () -> {
                target.target();
                target.target();
            };
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    Set<JavaMethodCall> calls = classes.get(Caller.class).getMethodCallsFromSelf();
    assertThat(calls).hasSize(2);
    calls.forEach(call -> assertThatCall(call).isFrom("call").isTo(Target.class, "target"));
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 5 with JavaMethodCall

use of com.tngtech.archunit.core.domain.JavaMethodCall in project ArchUnit by TNG.

the class ClassFileImporterLambdaAccessesTest method imports_multiple_method_calls_from_multiple_lambda_in_one_method.

@Test
public void imports_multiple_method_calls_from_multiple_lambda_in_one_method() {
    @SuppressWarnings("unused")
    class Target {

        Target target(String s) {
            return this;
        }

        Target target(int i) {
            return this;
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        private Target target;

        Function<String, Target> call(int i) {
            Function<Target, Target> function = t -> t.target(i);
            return s -> target.target(s);
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    Set<JavaMethodCall> calls = classes.get(Caller.class).getMethodCallsFromSelf();
    assertThat(filterOriginByName(calls, "call")).extracting(JavaMethodCall::getTarget).areExactly(1, targetCodeUnit("target", String.class)).areExactly(1, targetCodeUnit("target", int.class)).hasSize(2);
}
Also used : AccessesAssertion.access(com.tngtech.archunit.testutil.assertion.AccessesAssertion.access) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) RunWith(org.junit.runner.RunWith) DataProviders.$(com.tngtech.java.junit.dataprovider.DataProviders.$) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) JavaClassDescriptorImporterTestUtils.isLambdaMethodName(com.tngtech.archunit.core.importer.JavaClassDescriptorImporterTestUtils.isLambdaMethodName) Assertions.assertThatCall(com.tngtech.archunit.testutil.Assertions.assertThatCall) Assertions.assertThatAccesses(com.tngtech.archunit.testutil.Assertions.assertThatAccesses) CONSTRUCTOR_NAME(com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Assertions.assertThat(com.tngtech.archunit.testutil.Assertions.assertThat) Utils.namesOf(com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf) JavaAccess(com.tngtech.archunit.core.domain.JavaAccess) Collectors.toSet(java.util.stream.Collectors.toSet) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) DataProviders.$$(com.tngtech.java.junit.dataprovider.DataProviders.$$) Predicate(java.util.function.Predicate) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) Formatters.formatNamesOf(com.tngtech.archunit.core.domain.Formatters.formatNamesOf) Consumer(java.util.function.Consumer) JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) Assertions.assertThatAccess(com.tngtech.archunit.testutil.Assertions.assertThatAccess) Condition(org.assertj.core.api.Condition) JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Aggregations

JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)29 Test (org.junit.Test)25 JavaClass (com.tngtech.archunit.core.domain.JavaClass)21 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)15 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)15 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)8 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)4 Formatters.formatNamesOf (com.tngtech.archunit.core.domain.Formatters.formatNamesOf)4 JavaAccess (com.tngtech.archunit.core.domain.JavaAccess)4 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)4 CONSTRUCTOR_NAME (com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME)4 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)4 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)4 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)4 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)4 Utils.namesOf (com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf)4 JavaClassDescriptorImporterTestUtils.isLambdaMethodName (com.tngtech.archunit.core.importer.JavaClassDescriptorImporterTestUtils.isLambdaMethodName)4 Assertions.assertThat (com.tngtech.archunit.testutil.Assertions.assertThat)4 Assertions.assertThatAccess (com.tngtech.archunit.testutil.Assertions.assertThatAccess)4 Assertions.assertThatAccesses (com.tngtech.archunit.testutil.Assertions.assertThatAccesses)4