Search in sources :

Example 56 with JavaClass

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

the class ClassesShouldEvaluator method on.

Set<JavaClass> on(Class<?>... toCheck) {
    JavaClasses classes = importClasses(toCheck);
    List<String> relevantFailures = getRelevantFailures(classes);
    return classes.stream().filter(clazz -> anyLineMatches(relevantFailures, clazz)).collect(toSet());
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArchConfiguration(com.tngtech.archunit.ArchConfiguration) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ArchRule(com.tngtech.archunit.lang.ArchRule) Pattern.quote(java.util.regex.Pattern.quote) Annotation(java.lang.annotation.Annotation) JavaClass(com.tngtech.archunit.core.domain.JavaClass) FailureReport(com.tngtech.archunit.lang.FailureReport) Splitter(com.google.common.base.Splitter) Collectors.toSet(java.util.stream.Collectors.toSet) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses)

Example 57 with JavaClass

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

the class ImportTestUtils method simulateImport.

private static JavaClass simulateImport(Class<?> owner, ImportedTestClasses importedClasses) {
    JavaClass javaClass = ImportTestUtils.javaClassFor(owner);
    importedClasses.register(javaClass);
    ImportContext context = simulateImportContext(owner, importedClasses);
    DomainObjectCreationContext.completeMembers(javaClass, context);
    return javaClass;
}
Also used : ImportContext(com.tngtech.archunit.core.domain.ImportContext) JavaClass(com.tngtech.archunit.core.domain.JavaClass)

Example 58 with JavaClass

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

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

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

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9