Search in sources :

Example 1 with Immutable

use of org.eclipse.sirius.components.annotations.Immutable in project sirius-components by eclipse-sirius.

the class AbstractCodingRulesTests method noMethodsShouldBeStatic.

/**
 * Static methods make the lifecycle of the code complex. They make the code harder to reason about and they can
 * very easily become either:
 *
 * <ul>
 * <li>A problem to unit tests a class since they can't be mocked</li>
 * <li>God classes with a collection of unrelated utility methods</li>
 * </ul>
 *
 * With the introduction of this test, it appears that apart from some utility constructors like in the @Immutable
 * classes, we do not have a single static method with a real behavior. Thus nothing of value will be lost.
 *
 * In this test, we will ignore the following use cases:
 *
 * <ul>
 * <li>Enum since Java enum are considered as extending java.lang.Enum which comes with static methods</li>
 * <li>Java 8+ lambdas which are compiled to hidden static methods (to make it short)</li>
 * <li>@Parameters methods used by JUnit test cases</li>
 * <li>@Immutable classes which are using a static method to create the builder</li>
 * <li>@Constructor methods which are used as alternate constructors</li>
 * </ul>
 */
@Test
public void noMethodsShouldBeStatic() {
    // @formatter:off
    ArchRule rule = ArchRuleDefinition.noMethods().that().areDeclaredInClassesThat().resideInAPackage(this.getProjectRootPackage()).and().areDeclaredInClassesThat().areNotAnnotatedWith(Immutable.class).and().areDeclaredInClassesThat().areNotAssignableTo(Enum.class).and().areDeclaredInClassesThat(this.isNotTestCase()).and(this.isNotLambda()).and(this.isNotSwitchTable()).should().beStatic();
    // @formatter:on
    rule.check(this.getClasses());
}
Also used : Immutable(org.eclipse.sirius.components.annotations.Immutable) ArchRule(com.tngtech.archunit.lang.ArchRule) Test(org.junit.jupiter.api.Test)

Aggregations

ArchRule (com.tngtech.archunit.lang.ArchRule)1 Immutable (org.eclipse.sirius.components.annotations.Immutable)1 Test (org.junit.jupiter.api.Test)1