Search in sources :

Example 11 with JavaClass

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

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

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

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

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

the class ClassFileImporterTest method imports_urls_of_files.

@Test
public void imports_urls_of_files() {
    Set<URL> urls = newHashSet(urlOf(ClassToImportOne.class), urlOf(ClassWithNestedClass.class));
    Set<JavaClass> classesFoundAtUrls = new HashSet<>();
    for (JavaClass javaClass : new ClassFileImporter().importUrls(urls)) {
        if (!Object.class.getName().equals(javaClass.getName())) {
            classesFoundAtUrls.add(javaClass);
        }
    }
    assertThat(classesFoundAtUrls).as("Number of classes at the given URLs").hasSize(2);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithNestedClass(com.tngtech.archunit.core.importer.testexamples.nestedimport.ClassWithNestedClass) ClassToImportOne(com.tngtech.archunit.core.importer.testexamples.simpleimport.ClassToImportOne) URL(java.net.URL) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9