Search in sources :

Example 11 with JavaField

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

the class ClassFileImporterTest method handles_synthetic_modifiers.

@Test
public void handles_synthetic_modifiers() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/syntheticimport"));
    JavaField syntheticField = getOnlyElement(classes.get(ClassWithSynthetics.ClassWithSyntheticField.class).getFields());
    assertThat(syntheticField.getModifiers()).as("modifiers of field in ClassWithSynthetics.ClassWithSyntheticField").contains(SYNTHETIC);
    JavaMethod syntheticMethod = getMethodWithReturnType(classes.get(ClassWithSynthetics.ClassWithSyntheticMethod.class), Object.class);
    assertThat(syntheticMethod.getModifiers()).as("modifiers of method in ClassWithSynthetics.ClassWithSyntheticMethod").contains(SYNTHETIC);
    JavaMethod compareMethod = classes.get(ClassWithSynthetics.class).getMethod("compare", Object.class, Object.class);
    assertThat(compareMethod.getModifiers()).as("modifiers of bridge method in ClassWithSynthetics").contains(BRIDGE, SYNTHETIC);
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassWithSynthetics(com.tngtech.archunit.core.importer.testexamples.syntheticimport.ClassWithSynthetics) Test(org.junit.Test)

Example 12 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project AngularPortfolioMgr 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 13 with JavaField

use of com.tngtech.archunit.core.domain.JavaField in project AngularAndSpring 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 14 with JavaField

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

the class AbstractImmutableTests method haveAPublicGetter.

private ArchCondition<JavaField> haveAPublicGetter() {
    return new // $NON-NLS-1$
    ArchCondition<>(// $NON-NLS-1$
    "have a public getter") {

        @Override
        public void check(JavaField javaField, ConditionEvents events) {
            if (!javaField.getModifiers().contains(STATIC)) {
                JavaClass javaClass = javaField.getOwner();
                // $NON-NLS-1$
                String getterName = "get" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                if (javaField.getRawType().getFullName().equals("java.lang.Boolean") || javaField.getRawType().getFullName().equals("boolean")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    // $NON-NLS-1$
                    getterName = "is" + javaField.getName().substring(0, 1).toUpperCase() + javaField.getName().substring(1);
                }
                boolean isConditionSatisfied = false;
                try {
                    JavaMethod method = javaClass.getMethod(getterName);
                    isConditionSatisfied = method != null && method.getModifiers().contains(PUBLIC);
                } catch (IllegalArgumentException exception) {
                    // Getter not found
                    isConditionSatisfied = false;
                }
                // $NON-NLS-1$
                String message = "The field has a getter";
                if (!isConditionSatisfied) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    message = "The field " + javaField.getFullName() + " does not have a getter";
                }
                events.add(new SimpleConditionEvent(javaField, isConditionSatisfied, message));
            }
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) 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) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

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