Search in sources :

Example 16 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition 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 17 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition 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 18 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition 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)

Example 19 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project flink by apache.

the class Conditions method haveLeafExceptionTypes.

/**
 * Tests leaf exception types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafExceptionTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf exception types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getExceptionTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Exception leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) 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 20 with ArchCondition

use of com.tngtech.archunit.lang.ArchCondition in project flink by splunk.

the class Conditions method haveLeafArgumentTypes.

/**
 * Tests leaf argument types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getParameterTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) 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)

Aggregations

ArchCondition (com.tngtech.archunit.lang.ArchCondition)30 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)30 JavaClass (com.tngtech.archunit.core.domain.JavaClass)23 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)21 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)20 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)8 List (java.util.List)8 JavaMember (com.tngtech.archunit.core.domain.JavaMember)7 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Test (org.junit.Test)5 JavaField (com.tngtech.archunit.core.domain.JavaField)4 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)2 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)2