Search in sources :

Example 1 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.

the class AbstractCodingRulesTests method notContainBusinessCode.

/**
 * This predicate will be used to identify business code in a class.
 *
 * <p>
 * For that it will look for the following patterns in a Java class:
 * <p>
 * <ul>
 * <li>methods which are not getters with a field matching the name of the method</li>
 * </ul>
 *
 * @return A predicate which can be used to identify business code in a class
 */
private ArchCondition<JavaClass> notContainBusinessCode() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "not contain business code") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            boolean isConditionSatisfied = true;
            Set<JavaMethod> methods = javaClass.getMethods();
            Iterator<JavaMethod> iterator = methods.iterator();
            while (isConditionSatisfied && iterator.hasNext()) {
                JavaMethod javaMethod = iterator.next();
                String name = javaMethod.getName();
                if (name.startsWith(IS) && javaMethod.getRawReturnType().isAssignableTo(Boolean.class)) {
                    name = name.substring(IS.length());
                } else if (name.startsWith(GET)) {
                    name = name.substring(GET.length());
                }
                if (!name.isBlank()) {
                    name = name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
                    isConditionSatisfied = javaClass.tryGetField(name).isPresent();
                } else {
                    isConditionSatisfied = false;
                }
            }
            // $NON-NLS-1$
            String message = "The abstract class does not have any business code";
            if (!isConditionSatisfied) {
                // $NON-NLS-1$
                String pattern = "The abstract class {0} does contain business code, please favor composition over inheritance to share business code";
                message = MessageFormat.format(pattern, javaClass.getSimpleName());
            }
            events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) 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 2 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.

the class AbstractImmutableTests method haveABuilderMethod.

private ArchCondition<JavaClass> haveABuilderMethod() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "have a builder method") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            // @formatter:off
            long count = javaClass.getMethods().stream().filter(// $NON-NLS-1$
            method -> method.getName().equals("new" + javaClass.getSimpleName())).filter(// $NON-NLS-1$
            method -> "Builder".equals(method.getRawReturnType().getSimpleName())).count();
            // @formatter:on
            boolean isConditionSatisfied = count > 0;
            // $NON-NLS-1$
            String message = "The class has a builder method";
            if (!isConditionSatisfied) {
                // $NON-NLS-1$
                message = MessageFormat.format("The class {0} does not have a builder method", javaClass.getSimpleName());
            }
            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 3 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.

the class HaveAValidBuilderCondition method check.

@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    String fullName = javaClass.getFullName();
    // $NON-NLS-1$
    JavaClass builderJavaClass = this.javaClasses.get(fullName + "$Builder");
    boolean isValidBuilder = builderJavaClass.getModifiers().contains(FINAL);
    isValidBuilder = isValidBuilder && builderJavaClass.getModifiers().contains(PUBLIC);
    // @formatter:off
    List<JavaField> javaFields = javaClass.getAllFields().stream().filter(field -> !field.getModifiers().contains(STATIC)).collect(Collectors.toUnmodifiableList());
    for (JavaField javaField : javaFields) {
        JavaField builderField = builderJavaClass.getField(javaField.getName());
        isValidBuilder = isValidBuilder && builderField != null && builderField.getRawType().getName().equals(javaField.getRawType().getName());
    }
    for (JavaMethod javaMethod : builderJavaClass.getMethods()) {
        if (!BUILD_METHOD_NAME.equals(javaMethod.getName()) && !javaMethod.getName().contains(LAMBDA)) {
            isValidBuilder = isValidBuilder && javaMethod.getRawReturnType().equals(builderJavaClass);
            JavaField javaField = builderJavaClass.getField(javaMethod.getName());
            isValidBuilder = isValidBuilder && javaField != null;
            isValidBuilder = isValidBuilder && javaMethod.getRawParameterTypes().size() == 1;
        }
    }
    // @formatter:off
    long buildMethodCount = builderJavaClass.getMethods().stream().filter(method -> BUILD_METHOD_NAME.equals(method.getName())).filter(method -> javaClass.getSimpleName().equals(method.getRawReturnType().getSimpleName())).count();
    // @formatter:on
    isValidBuilder = isValidBuilder && buildMethodCount == 1;
    // $NON-NLS-1$
    String message = "The builder is valid";
    if (!isValidBuilder) {
        // $NON-NLS-1$
        message = MessageFormat.format("The builder of the class {0} is not valid", javaClass.getSimpleName());
    }
    events.add(new SimpleConditionEvent(builderJavaClass, isValidBuilder, message));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ArchCondition(com.tngtech.archunit.lang.ArchCondition) Collectors(java.util.stream.Collectors) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) JavaField(com.tngtech.archunit.core.domain.JavaField) MessageFormat(java.text.MessageFormat) Objects(java.util.Objects) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) 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) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod)

Example 4 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project ArchUnit by TNG.

the class SessionBeanRulesTest method haveAUniqueImplementation.

private static ArchCondition<JavaClass> haveAUniqueImplementation() {
    return new ArchCondition<JavaClass>("have a unique implementation") {

        @Override
        public void check(JavaClass businessInterface, ConditionEvents events) {
            events.add(new SimpleConditionEvent(businessInterface, businessInterface.getAllSubclasses().size() <= 1, describe(businessInterface)));
        }

        private String describe(JavaClass businessInterface) {
            return String.format("%s is implemented by %s", businessInterface.getSimpleName(), joinNamesOf(businessInterface.getAllSubclasses()));
        }

        private String joinNamesOf(Set<JavaClass> implementations) {
            if (implementations.isEmpty()) {
                return "";
            }
            Deque<JavaClass> toJoin = new LinkedList<>(implementations);
            StringBuilder sb = new StringBuilder(toJoin.pollFirst().getSimpleName());
            for (JavaClass javaClass : toJoin) {
                sb.append(", ").append(javaClass.getSimpleName());
            }
            return sb.toString();
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Set(java.util.Set) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) LinkedList(java.util.LinkedList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 5 with SimpleConditionEvent

use of com.tngtech.archunit.lang.SimpleConditionEvent in project ArchUnit by TNG.

the class GivenMembersTest method beAnnotatedWith.

static ArchCondition<JavaMember> beAnnotatedWith(final Class<? extends Annotation> annotationType) {
    return new ArchCondition<JavaMember>("be annotated with @%s", annotationType.getSimpleName()) {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = member.isAnnotatedWith(annotationType);
            String message = String.format("Member '%s' %s @%s", formatMember(member), satisfied ? "is annotated with" : "is not annotated with", annotationType.getSimpleName());
            events.add(new SimpleConditionEvent(member, satisfied, message));
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Aggregations

SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 ArchCondition (com.tngtech.archunit.lang.ArchCondition)13 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)13 JavaClass (com.tngtech.archunit.core.domain.JavaClass)11 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)5 JavaField (com.tngtech.archunit.core.domain.JavaField)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)3 FINAL (com.tngtech.archunit.core.domain.JavaModifier.FINAL)3 PUBLIC (com.tngtech.archunit.core.domain.JavaModifier.PUBLIC)3 STATIC (com.tngtech.archunit.core.domain.JavaModifier.STATIC)3 MessageFormat (java.text.MessageFormat)3 ArchRule (com.tngtech.archunit.lang.ArchRule)2 ArchRuleDefinition (com.tngtech.archunit.lang.syntax.ArchRuleDefinition)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 Immutable (org.eclipse.sirius.components.annotations.Immutable)2 Test (org.junit.jupiter.api.Test)2 PublicAPI (com.tngtech.archunit.PublicAPI)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1