Search in sources :

Example 1 with PUBLIC

use of com.tngtech.archunit.core.domain.JavaModifier.PUBLIC 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 2 with PUBLIC

use of com.tngtech.archunit.core.domain.JavaModifier.PUBLIC 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 3 with PUBLIC

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

the class ClassFileImporterMembersTest method classes_know_which_instanceof_checks_check_their_type.

@Test
public void classes_know_which_instanceof_checks_check_their_type() {
    JavaClass clazz = new ClassFileImporter().importPackagesOf(InstanceofChecked.class).get(InstanceofChecked.class);
    Set<JavaClass> origins = clazz.getInstanceofChecksWithTypeOfSelf().stream().map(instanceofCheck -> instanceofCheck.getOwner().getOwner()).collect(toSet());
    assertThatTypes(origins).matchInAnyOrder(ChecksInstanceofInMethod.class, ChecksInstanceofInConstructor.class, ChecksInstanceofInStaticInitializer.class);
}
Also used : SomeEnum(com.tngtech.archunit.core.importer.testexamples.SomeEnum) OtherClass(com.tngtech.archunit.core.importer.testexamples.OtherClass) SecondCheckedException(com.tngtech.archunit.core.importer.testexamples.SecondCheckedException) Objects(org.assertj.core.util.Objects) ClassWithStringField(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithStringField) JavaField(com.tngtech.archunit.core.domain.JavaField) ReferencedClassObjectsAssertion(com.tngtech.archunit.testutil.assertion.ReferencedClassObjectsAssertion) Iterables.concat(com.google.common.collect.Iterables.concat) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Assertions.assertThat(com.tngtech.archunit.testutil.Assertions.assertThat) Method(java.lang.reflect.Method) Path(java.nio.file.Path) ClassFileImporterTestUtils.getFields(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getFields) SomeClass(com.tngtech.archunit.core.importer.testexamples.SomeClass) Collectors.toSet(java.util.stream.Collectors.toSet) PRIVATE(com.tngtech.archunit.core.domain.JavaModifier.PRIVATE) ThrowsDeclaration(com.tngtech.archunit.core.domain.ThrowsDeclaration) BaseClass(com.tngtech.archunit.core.importer.testexamples.classhierarchyimport.BaseClass) ImmutableSet(com.google.common.collect.ImmutableSet) ClassWithStringStringMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithStringStringMethod) ReferencingClassObjects(com.tngtech.archunit.core.importer.testexamples.referencedclassobjects.ReferencingClassObjects) ClassWithSimpleConstructors(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithSimpleConstructors) ReferencedClassObjectsAssertion.referencedClassObject(com.tngtech.archunit.testutil.assertion.ReferencedClassObjectsAssertion.referencedClassObject) Set(java.util.Set) ChecksInstanceofInStaticInitializer(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInStaticInitializer) ReflectionTestUtils.field(com.tngtech.archunit.testutil.ReflectionTestUtils.field) FileSystem(java.nio.file.FileSystem) Serializable(java.io.Serializable) ClassWithThrowingConstructor(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithThrowingConstructor) Assertions.assertThatThrowsClause(com.tngtech.archunit.testutil.Assertions.assertThatThrowsClause) Optional(java.util.Optional) Assertions.assertThatType(com.tngtech.archunit.testutil.Assertions.assertThatType) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) FirstCheckedException(com.tngtech.archunit.core.importer.testexamples.FirstCheckedException) ChecksInstanceofInConstructor(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInConstructor) PROTECTED(com.tngtech.archunit.core.domain.JavaModifier.PROTECTED) ClassWithComplexMethod(com.tngtech.archunit.core.importer.testexamples.complexmethodimport.ClassWithComplexMethod) ClassWithIntAndObjectFields(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithIntAndObjectFields) Assertions.assertThatReferencedClassObjects(com.tngtech.archunit.testutil.Assertions.assertThatReferencedClassObjects) Constructor(java.lang.reflect.Constructor) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) ChecksInstanceofInMethod(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.ChecksInstanceofInMethod) Assertions.assertThatTypes(com.tngtech.archunit.testutil.Assertions.assertThatTypes) FilterInputStream(java.io.FilterInputStream) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Charset(java.nio.charset.Charset) ClassFileImporterTestUtils.getCodeUnits(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getCodeUnits) ClassFileImporterTestUtils.getMethods(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.getMethods) Buffer(java.nio.Buffer) InstanceofChecked(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.InstanceofChecked) Utils.namesOf(com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf) ClassWithThrowingMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithThrowingMethod) PUBLIC(com.tngtech.archunit.core.domain.JavaModifier.PUBLIC) ClassWithObjectVoidAndIntIntSerializableMethod(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithObjectVoidAndIntIntSerializableMethod) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ClassWithMultipleMethods(com.tngtech.archunit.core.importer.testexamples.methodimport.ClassWithMultipleMethods) Test(org.junit.Test) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) File(java.io.File) ClassWithComplexConstructor(com.tngtech.archunit.core.importer.testexamples.constructorimport.ClassWithComplexConstructor) ClassFileImporterTestUtils.findAnyByName(com.tngtech.archunit.core.importer.ClassFileImporterTestUtils.findAnyByName) Subclass(com.tngtech.archunit.core.importer.testexamples.classhierarchyimport.Subclass) VOLATILE(com.tngtech.archunit.core.domain.JavaModifier.VOLATILE) TRANSIENT(com.tngtech.archunit.core.domain.JavaModifier.TRANSIENT) JavaClass(com.tngtech.archunit.core.domain.JavaClass) InstanceofChecked(com.tngtech.archunit.core.importer.testexamples.instanceofcheck.InstanceofChecked) Test(org.junit.Test)

Example 4 with PUBLIC

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

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)4 JavaField (com.tngtech.archunit.core.domain.JavaField)4 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)4 FINAL (com.tngtech.archunit.core.domain.JavaModifier.FINAL)4 PUBLIC (com.tngtech.archunit.core.domain.JavaModifier.PUBLIC)4 STATIC (com.tngtech.archunit.core.domain.JavaModifier.STATIC)4 ArchCondition (com.tngtech.archunit.lang.ArchCondition)3 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)3 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)3 MessageFormat (java.text.MessageFormat)3 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.concat (com.google.common.collect.Iterables.concat)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 PRIVATE (com.tngtech.archunit.core.domain.JavaModifier.PRIVATE)1 PROTECTED (com.tngtech.archunit.core.domain.JavaModifier.PROTECTED)1 TRANSIENT (com.tngtech.archunit.core.domain.JavaModifier.TRANSIENT)1 VOLATILE (com.tngtech.archunit.core.domain.JavaModifier.VOLATILE)1