Search in sources :

Example 11 with JavaMethod

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

the class ArchUnitFixtureTest method checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes.

@Nonnull
private ConditionEvent checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes(String methodName, Class<?>... arguments) {
    ArchCondition<JavaMethod> archCondition = haveOnlyArgumentsOrReturnTypesThatAre(resideInAnyPackage("java.lang").or(primitive).or(resideInAnyPackage("java.util")).as("allowed"));
    JavaClass javaClass = new ClassFileImporter().importClass(AllowedMethodTypesClass.class);
    JavaMethod validMethod = javaClass.getMethod(methodName, arguments);
    ConditionEvents events = new ConditionEvents();
    archCondition.check(validMethod, events);
    assertThat(events).hasSize(1);
    return events.iterator().next();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Nonnull(javax.annotation.Nonnull)

Example 12 with JavaMethod

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

the class ProviderMigrationArchitectureTest method hasSetter.

private static boolean hasSetter(JavaMethod input) {
    PropertyAccessorType accessorType = PropertyAccessorType.fromName(input.getName());
    String propertyNameFromGetter = accessorType.propertyNameFor(input.getName());
    return input.getOwner().getAllMethods().stream().filter(method -> PropertyAccessorType.fromName(method.getName()) == PropertyAccessorType.SETTER).anyMatch(method -> PropertyAccessorType.SETTER.propertyNameFor(method.getName()).equals(propertyNameFromGetter));
}
Also used : ArchRuleDefinition.methods(com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) Predicates.declaredIn(com.tngtech.archunit.core.domain.JavaMember.Predicates.declaredIn) TextResource(org.gradle.api.resources.TextResource) Predicates.annotatedWith(com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) WelcomeMessageConfiguration(org.gradle.api.launcher.cli.WelcomeMessageConfiguration) Predicates.assignableTo(com.tngtech.archunit.core.domain.JavaClass.Predicates.assignableTo) HasDescription(com.tngtech.archunit.base.HasDescription) DescribedPredicate.not(com.tngtech.archunit.base.DescribedPredicate.not) HasSourceCodeLocation(com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation) FileCollection(org.gradle.api.file.FileCollection) PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType) ArchUnitFixture.public_api_methods(org.gradle.architecture.test.ArchUnitFixture.public_api_methods) Inject(javax.inject.Inject) Configuration(org.gradle.api.artifacts.Configuration) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) StartParameter(org.gradle.StartParameter) ArchPredicates.are(com.tngtech.archunit.lang.conditions.ArchPredicates.are) DefaultTask(org.gradle.api.DefaultTask) JavaClass(com.tngtech.archunit.core.domain.JavaClass) AnalyzeClasses(com.tngtech.archunit.junit.AnalyzeClasses) ArchTest(com.tngtech.archunit.junit.ArchTest) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArchUnitFixture.freeze(org.gradle.architecture.test.ArchUnitFixture.freeze) ArchPredicates(com.tngtech.archunit.lang.conditions.ArchPredicates) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) ArchRule(com.tngtech.archunit.lang.ArchRule) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Property(org.gradle.api.provider.Property) PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType)

Example 13 with JavaMethod

use of com.tngtech.archunit.core.domain.JavaMethod in project opentelemetry-java by open-telemetry.

the class SdkDesignTest method implementOrOverride.

static DescribedPredicate<? super JavaMethod> implementOrOverride() {
    return new DescribedPredicate<>("implement or override a method") {

        @Override
        public boolean apply(JavaMethod input) {
            List<JavaClass> params = input.getRawParameterTypes();
            Class<?>[] paramsType = new Class<?>[params.size()];
            for (int i = 0, n = params.size(); i < n; i++) {
                paramsType[i] = params.get(i).reflect();
            }
            String name = input.getName();
            List<JavaClass> parents = new ArrayList<>(input.getOwner().getAllRawSuperclasses());
            parents.addAll(input.getOwner().getAllRawInterfaces());
            for (JavaClass parent : parents) {
                Optional<JavaMethod> found = parent.tryGetMethod(name, paramsType);
                if (found.isPresent()) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) ArrayList(java.util.ArrayList) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaClass(com.tngtech.archunit.core.domain.JavaClass)

Example 14 with JavaMethod

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

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 15 with JavaMethod

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

the class GivenThatIsTestedConsistentlyTest method classes_that_tests_all_relevant_methods.

@Test
public void classes_that_tests_all_relevant_methods() {
    JavaClasses classes = importClassesWithContext(GivenClassesThatTest.class, ClassesThat.class);
    JavaClass test = classes.get(GivenClassesThatTest.class);
    for (JavaMethod method : classes.get(ClassesThat.class).getMethods()) {
        assertAccessFrom(test, method);
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) 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