Search in sources :

Example 6 with SimpleConditionEvent

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

the class PublicAPIRules method notBePublic.

private static ArchCondition<JavaMember> notBePublic() {
    return new ArchCondition<JavaMember>("not be public") {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = !member.getModifiers().contains(PUBLIC);
            events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %spublic in %s", member.getOwner().getName(), member.getName(), satisfied ? "not " : "", member.getSourceCodeLocation())));
        }
    };
}
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)

Example 7 with SimpleConditionEvent

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

the class PublicAPIRules method bePublicAPIForInheritance.

private static ArchCondition<JavaClass> bePublicAPIForInheritance() {
    return new ArchCondition<JavaClass>("be public API for inheritance") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isAnnotatedWith(publicApiForInheritance()) || markedAsPublicAPIForInheritance().test(item);
            events.add(new SimpleConditionEvent(item, satisfied, String.format("class %s is %smeant for inheritance", item.getName(), satisfied ? "" : "not ")));
        }
    };
}
Also used : 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 8 with SimpleConditionEvent

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

the class PublicAPIRules method bePubliclyAccessible.

private static ArchCondition<JavaMember> bePubliclyAccessible() {
    return new ArchCondition<JavaMember>("be publicly accessible") {

        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean declaringClassIsPublic = getAllEnclosingClasses(member).allMatch(c -> c.getModifiers().contains(PUBLIC));
            boolean satisfied = member.getModifiers().contains(PUBLIC) && declaringClassIsPublic;
            events.add(new SimpleConditionEvent(member, satisfied, String.format("member %s.%s is %sdeclared in public location in %s", member.getOwner().getName(), member.getName(), satisfied ? "" : "not ", member.getSourceCodeLocation())));
        }

        private Stream<JavaClass> getAllEnclosingClasses(JavaMember member) {
            List<JavaClass> enclosingClasses = newArrayList(member.getOwner());
            while (getLast(enclosingClasses).getEnclosingClass().isPresent()) {
                enclosingClasses.add(getLast(enclosingClasses).getEnclosingClass().get());
            }
            return enclosingClasses.stream();
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMember(com.tngtech.archunit.core.domain.JavaMember) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 9 with SimpleConditionEvent

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

the class PublicAPIRules method beInterfaces.

private static ArchCondition<? super JavaClass> beInterfaces() {
    return new ArchCondition<JavaClass>("be interfaces") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isInterface();
            events.add(new SimpleConditionEvent(item, satisfied, String.format("class %s is %sinterface", item.getName(), satisfied ? "" : "no ")));
        }
    };
}
Also used : 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 10 with SimpleConditionEvent

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

the class ProxyRules method directly_call_other_methods_declared_in_the_same_class_that.

/**
 * Returns a condition that matches classes that directly calls other methods
 * declared in the same class that matches the given predicate.
 *
 * <p>
 * For an example, see {@link #directly_call_other_methods_declared_in_the_same_class_that_are_annotated_with(Class)}
 * </p>
 */
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaClass> directly_call_other_methods_declared_in_the_same_class_that(final DescribedPredicate<? super MethodCallTarget> predicate) {
    return new ArchCondition<JavaClass>("directly call other methods declared in the same class that " + predicate.getDescription()) {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            for (JavaMethodCall call : javaClass.getMethodCallsFromSelf()) {
                boolean satisfied = call.getOriginOwner().equals(call.getTargetOwner()) && predicate.test(call.getTarget());
                events.add(new SimpleConditionEvent(call, satisfied, call.getDescription()));
            }
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) PublicAPI(com.tngtech.archunit.PublicAPI)

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