Search in sources :

Example 1 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project AngularAndSpringWithMaps by Angular2Guy.

the class MyArchitectureTests method createNoFieldInjectionRule.

private static ArchRule createNoFieldInjectionRule() {
    ArchCondition<JavaField> annotatedWithSpringAutowired = beAnnotatedWith("org.springframework.beans.factory.annotation.Autowired");
    ArchCondition<JavaField> annotatedWithGuiceInject = beAnnotatedWith("com.google.inject.Inject");
    ArchCondition<JavaField> annotatedWithJakartaInject = beAnnotatedWith("javax.inject.Inject");
    ArchRule beAnnotatedWithAnInjectionAnnotation = ArchRuleDefinition.noFields().should(annotatedWithSpringAutowired.or(annotatedWithGuiceInject).or(annotatedWithJakartaInject).as("be annotated with an injection annotation"));
    return beAnnotatedWithAnInjectionAnnotation;
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) CompositeArchRule(com.tngtech.archunit.lang.CompositeArchRule) JavaField(com.tngtech.archunit.core.domain.JavaField)

Example 2 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project arch-unit-build-plugin-core by societe-generale.

the class NoJodaTimeRuleTest method notUseJodaTime.

protected static ArchCondition<JavaClass> notUseJodaTime() {
    return new ArchCondition<JavaClass>("not use Joda time ") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            List<JavaField> classesWithJodaTimeFields = item.getAllFields().stream().filter(this::isJodaTimeField).collect(toList());
            for (JavaField field : classesWithJodaTimeFields) {
                events.add(SimpleConditionEvent.violated(field, NO_JODA_VIOLATION_MESSAGE + " - class: " + field.getOwner().getName() + " - field name: " + field.getName()));
            }
            List<JavaMethodCall> methodsUsingJodaTimeInternally = item.getCodeUnits().stream().filter(codeUnit -> codeUnit instanceof JavaMethod).flatMap(method -> method.getMethodCallsFromSelf().stream()).filter(method -> method instanceof JavaMethodCall).filter(this::isMethodUsingJodaTimeInternally).collect(toList());
            for (JavaMethodCall methodCall : methodsUsingJodaTimeInternally) {
                events.add(SimpleConditionEvent.violated(methodCall.getOriginOwner(), NO_JODA_VIOLATION_MESSAGE + " - class: " + methodCall.getOriginOwner().getName() + " - method: " + methodCall.getTarget().getOwner().getSimpleName() + "." + methodCall.getTarget().getName() + " - line: " + methodCall.getLineNumber()));
            }
        }

        private boolean isJodaTimeField(JavaField field) {
            return field.getRawType().getName().startsWith(JODATIME_PACKAGE_PREFIX);
        }

        private boolean isMethodUsingJodaTimeInternally(JavaMethodCall javaMethodCall) {
            return javaMethodCall.getTarget().getFullName().startsWith(JODATIME_PACKAGE_PREFIX);
        }
    };
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Collection(java.util.Collection) JavaField(com.tngtech.archunit.core.domain.JavaField) ArchUtils(com.societegenerale.commons.plugin.utils.ArchUtils) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchRuleDefinition.classes(com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ScopePathProvider(com.societegenerale.commons.plugin.service.ScopePathProvider) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 3 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project MovieManager by Angular2Guy.

the class MyArchitectureTests method createNoFieldInjectionRule.

private static ArchRule createNoFieldInjectionRule() {
    ArchCondition<JavaField> annotatedWithSpringAutowired = beAnnotatedWith("org.springframework.beans.factory.annotation.Autowired");
    ArchCondition<JavaField> annotatedWithGuiceInject = beAnnotatedWith("com.google.inject.Inject");
    ArchCondition<JavaField> annotatedWithJakartaInject = beAnnotatedWith("javax.inject.Inject");
    ArchRule beAnnotatedWithAnInjectionAnnotation = ArchRuleDefinition.noFields().should(annotatedWithSpringAutowired.or(annotatedWithGuiceInject).or(annotatedWithJakartaInject).as("be annotated with an injection annotation"));
    return beAnnotatedWithAnInjectionAnnotation;
}
Also used : ArchRule(com.tngtech.archunit.lang.ArchRule) CompositeArchRule(com.tngtech.archunit.lang.CompositeArchRule) JavaField(com.tngtech.archunit.core.domain.JavaField)

Example 4 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project sirius-components by eclipse-sirius.

the class HaveAValidBuilderCondition method check.

@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    String fullName = javaClass.getFullName();
    // $NON-NLS-1$
    JavaClass builderJavaClass = this.javaClasses.get(fullName + "$Builder");
    boolean isValidBuilder = builderJavaClass.getModifiers().contains(FINAL);
    isValidBuilder = isValidBuilder && builderJavaClass.getModifiers().contains(PUBLIC);
    // @formatter:off
    List<JavaField> javaFields = javaClass.getAllFields().stream().filter(field -> !field.getModifiers().contains(STATIC)).collect(Collectors.toUnmodifiableList());
    for (JavaField javaField : javaFields) {
        JavaField builderField = builderJavaClass.getField(javaField.getName());
        isValidBuilder = isValidBuilder && builderField != null && builderField.getRawType().getName().equals(javaField.getRawType().getName());
    }
    for (JavaMethod javaMethod : builderJavaClass.getMethods()) {
        if (!BUILD_METHOD_NAME.equals(javaMethod.getName()) && !javaMethod.getName().contains(LAMBDA)) {
            isValidBuilder = isValidBuilder && javaMethod.getRawReturnType().equals(builderJavaClass);
            JavaField javaField = builderJavaClass.getField(javaMethod.getName());
            isValidBuilder = isValidBuilder && javaField != null;
            isValidBuilder = isValidBuilder && javaMethod.getRawParameterTypes().size() == 1;
        }
    }
    // @formatter:off
    long buildMethodCount = builderJavaClass.getMethods().stream().filter(method -> BUILD_METHOD_NAME.equals(method.getName())).filter(method -> javaClass.getSimpleName().equals(method.getRawReturnType().getSimpleName())).count();
    // @formatter:on
    isValidBuilder = isValidBuilder && buildMethodCount == 1;
    // $NON-NLS-1$
    String message = "The builder is valid";
    if (!isValidBuilder) {
        // $NON-NLS-1$
        message = MessageFormat.format("The builder of the class {0} is not valid", javaClass.getSimpleName());
    }
    events.add(new SimpleConditionEvent(builderJavaClass, isValidBuilder, message));
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) STATIC(com.tngtech.archunit.core.domain.JavaModifier.STATIC) ArchCondition(com.tngtech.archunit.lang.ArchCondition) Collectors(java.util.stream.Collectors) FINAL(com.tngtech.archunit.core.domain.JavaModifier.FINAL) JavaField(com.tngtech.archunit.core.domain.JavaField) MessageFormat(java.text.MessageFormat) Objects(java.util.Objects) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) PUBLIC(com.tngtech.archunit.core.domain.JavaModifier.PUBLIC) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod)

Example 5 with JavaField

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

the class ClassFileImporterAccessesTest method resolve.

// only temporary to make sure resolveMember() and resolve() are in sync. Inline again when we throw out resolve()
private JavaField resolve(FieldAccessTarget fieldAccessTarget) {
    JavaField field = fieldAccessTarget.resolveMember().get();
    assertThat(fieldAccessTarget.resolve()).containsExactly(field);
    return field;
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField)

Aggregations

JavaField (com.tngtech.archunit.core.domain.JavaField)14 Test (org.junit.Test)6 JavaClass (com.tngtech.archunit.core.domain.JavaClass)5 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)4 ClassWithAnnotatedFields (com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields)4 ArchRule (com.tngtech.archunit.lang.ArchRule)4 CompositeArchRule (com.tngtech.archunit.lang.CompositeArchRule)4 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)3 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3 ArchCondition (com.tngtech.archunit.lang.ArchCondition)3 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)3 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)3 List (java.util.List)2 ScopePathProvider (com.societegenerale.commons.plugin.service.ScopePathProvider)1 ArchUtils (com.societegenerale.commons.plugin.utils.ArchUtils)1 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 JavaEnumConstant (com.tngtech.archunit.core.domain.JavaEnumConstant)1 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1 FINAL (com.tngtech.archunit.core.domain.JavaModifier.FINAL)1