Search in sources :

Example 51 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project git-machete-intellij-plugin by VirtusLab.

the class UiThreadUnsafeMethodInvocationsTestSuite method only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods.

@Test
public void only_ui_thread_unsafe_methods_should_call_other_ui_thread_unsafe_methods() {
    methods().that().areNotAnnotatedWith(UIThreadUnsafe.class).should(new ArchCondition<JavaMethod>("never call any ${UIThreadUnsafeName} methods") {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            // which would in turn heavily reduce readability, hence potentially leading to bugs in the long run.
            if (method.getName().startsWith("access$") || method.getName().startsWith("lambda$")) {
                return;
            }
            method.getCallsFromSelf().forEach(access -> {
                AccessTarget accessTarget = access.getTarget();
                if (accessTarget.isAnnotatedWith(UIThreadUnsafe.class)) {
                    String message = "a non-${UIThreadUnsafeName} method ${method.getFullName()} " + "calls a ${UIThreadUnsafeName} method ${accessTarget.getFullName()}";
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            });
        }
    }).check(importedClasses);
}
Also used : ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 52 with JavaMethod

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

the class NoTestIgnoreWithoutCommentRuleTest method notBeIgnoredWithoutAComment.

public static ArchCondition<JavaClass> notBeIgnoredWithoutAComment() {
    return new ArchCondition<JavaClass>(NO_JUNIT_IGNORE_WITHOUT_COMMENT_VIOLATION_MESSAGE) {

        @Override
        @SuppressWarnings("squid:S1166")
        public void check(JavaClass item, ConditionEvents events) {
            // class level checks
            String violationMessageAtClassLevel = item.getName() + ", at class level";
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Ignore.class, violationMessageAtClassLevel), events);
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Disabled.class, violationMessageAtClassLevel), events);
            // method level checks
            for (JavaMethod method : item.getMethods()) {
                String violationMessageAtMethodLevel = item.getName() + " - " + method.getName() + ", at method level";
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Ignore.class, violationMessageAtMethodLevel), events);
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Disabled.class, violationMessageAtMethodLevel), events);
            }
        }

        private void addViolationEvent(Optional<ConditionEvent> violation, ConditionEvents events) {
            if (violation.isPresent()) {
                events.add(violation.get());
            }
        }

        private Optional<ConditionEvent> buildViolationIfAnnotationWithNoValueFound(HasAnnotations item, Class annotation, String violationMessage) {
            try {
                if (getAnnotationValue(item, annotation).isEmpty()) {
                    return Optional.of(SimpleConditionEvent.violated(item, violationMessage));
                }
            } catch (IllegalArgumentException e) {
            // if there's no Ignore annotation, IllegalArgument exception is thrown.
            // we swallow it, as it means there's no annotation at class level.
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            // this won't happen, as we use reflection on a type that we know has the method we are looking for
            }
            return Optional.empty();
        }

        private String getAnnotationValue(HasAnnotations annotatedObj, Class annotation) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            Annotation ann = annotatedObj.getAnnotationOfType(annotation);
            Method valueMethod = ann.getClass().getDeclaredMethod("value");
            return (String) valueMethod.invoke(ann);
        }
    };
}
Also used : HasAnnotations(com.tngtech.archunit.core.domain.properties.HasAnnotations) Ignore(org.junit.Ignore) Optional(java.util.Optional) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Annotation(java.lang.annotation.Annotation) ConditionEvent(com.tngtech.archunit.lang.ConditionEvent) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Disabled(org.junit.jupiter.api.Disabled) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 53 with JavaMethod

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

the class AbstractImmutableTests method haveAPublicGetter.

private ArchCondition<JavaField> haveAPublicGetter() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "have a public getter") {

        @Override
        public void check(JavaField javaField, ConditionEvents events) {
            if (!javaField.getModifiers().contains(STATIC)) {
                JavaClass javaClass = javaField.getOwner();
                // $NON-NLS-1$
                String getterName = "get" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                if (javaField.getRawType().getFullName().equals("java.lang.Boolean") || javaField.getRawType().getFullName().equals("boolean")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    // $NON-NLS-1$
                    getterName = "is" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                }
                boolean isConditionSatisfied = false;
                try {
                    JavaMethod method = javaClass.getMethod(getterName);
                    isConditionSatisfied = method != null && method.getModifiers().contains(PUBLIC);
                } catch (IllegalArgumentException exception) {
                    // Getter not found
                    isConditionSatisfied = false;
                }
                // $NON-NLS-1$
                String message = "The field has a getter";
                if (!isConditionSatisfied) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    message = "The field " + javaField.getFullName() + " does not have a getter";
                }
                events.add(new SimpleConditionEvent(javaField, isConditionSatisfied, message));
            }
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) 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) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 54 with JavaMethod

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

the class AbstractImmutableTests method notHaveSetters.

private ArchCondition<JavaClass> notHaveSetters() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "not have setters") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            // @formatter:off
            long settersCount = javaClass.getMethods().stream().filter(// $NON-NLS-1$
            javaMethod -> javaMethod.getName().startsWith("set")).count();
            // @formatter:on
            boolean isConditionSatisfied = settersCount == 0;
            // $NON-NLS-1$
            String message = "The class does not have any setters";
            if (!isConditionSatisfied) {
                // $NON-NLS-1$//$NON-NLS-2$
                message = "The class " + javaClass.getName() + " does have setters";
            }
            events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
        }
    };
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Immutable(org.eclipse.sirius.components.annotations.Immutable) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ArchCondition(com.tngtech.archunit.lang.ArchCondition) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) JavaField(com.tngtech.archunit.core.domain.JavaField) MessageFormat(java.text.MessageFormat) Test(org.junit.jupiter.api.Test) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) ArchRule(com.tngtech.archunit.lang.ArchRule) ArchRuleDefinition(com.tngtech.archunit.lang.syntax.ArchRuleDefinition) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) PUBLIC(com.tngtech.archunit.core.domain.JavaModifier.PUBLIC) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 55 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project flink-mirror by flink-ci.

the class Conditions method haveLeafExceptionTypes.

/**
 * Tests leaf exception types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafExceptionTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf exception types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getExceptionTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Exception leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

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