Search in sources :

Example 26 with JavaMethod

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

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

Example 28 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project flink by splunk.

the class Conditions method haveLeafReturnTypes.

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

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            for (JavaClass leafType : getLeafTypes(method.getReturnType())) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Returned 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) 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 29 with JavaMethod

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

the class GivenCodeUnitsTest method types_match_for_methods.

@Test
public void types_match_for_methods() {
    EvaluationResult result = methods().that(new DescribedPredicate<JavaMember>("are there") {

        @Override
        public boolean test(JavaMember input) {
            return true;
        }
    }).and(new DescribedPredicate<JavaMethod>("are there") {

        @Override
        public boolean test(JavaMethod input) {
            return true;
        }
    }).should(new ArchCondition<JavaMethod>("not exist") {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            events.add(SimpleConditionEvent.violated(method, "expected violation"));
        }
    }).evaluate(importClasses(ClassWithVariousMembers.class));
    assertThat(Joiner.on(" ").join(result.getFailureReport().getDetails())).contains("expected violation");
}
Also used : DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMember(com.tngtech.archunit.core.domain.JavaMember) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Test(org.junit.Test)

Example 30 with JavaMethod

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

the class SlicesTest method slices_of_dependencies.

@Test
public void slices_of_dependencies() {
    JavaMethod methodThatCallsJavaUtil = TestUtils.importClassWithContext(Object.class).getMethod("toString");
    JavaMethod methodThatCallsJavaLang = TestUtils.importClassWithContext(Map.class).getMethod("put", Object.class, Object.class);
    simulateCall().from(methodThatCallsJavaUtil, 5).to(methodThatCallsJavaLang);
    simulateCall().from(methodThatCallsJavaLang, 1).to(methodThatCallsJavaUtil);
    Dependency first = dependencyFrom(getOnlyElement(methodThatCallsJavaUtil.getMethodCallsFromSelf()));
    Dependency second = dependencyFrom(getOnlyElement(methodThatCallsJavaLang.getMethodCallsFromSelf()));
    Slices slices = Slices.matching("java.(*)..").transform(ImmutableSet.of(first, second));
    assertThat(slices).extractingResultOf("getDescription").containsOnly("Slice lang", "Slice util");
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) Dependency(com.tngtech.archunit.core.domain.Dependency) Map(java.util.Map) Test(org.junit.Test)

Aggregations

JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)56 JavaClass (com.tngtech.archunit.core.domain.JavaClass)36 Test (org.junit.Test)32 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)22 ArchCondition (com.tngtech.archunit.lang.ArchCondition)21 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 List (java.util.List)12 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)10 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)9 Collectors (java.util.stream.Collectors)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 Stream (java.util.stream.Stream)8 Collections (java.util.Collections)7 JavaField (com.tngtech.archunit.core.domain.JavaField)6 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 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)5 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)5 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)5