Search in sources :

Example 6 with JavaField

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

the class ClassFileImporterAccessesTest method imports_shadowed_and_superclass_field_access.

@Test
public void imports_shadowed_and_superclass_field_access() {
    JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/hierarchicalfieldaccess"));
    JavaClass classThatAccessesFieldOfSuperclass = classes.get(AccessToSuperAndSubclassField.class);
    JavaClass superclassWithAccessedField = classes.get(SuperclassWithAccessedField.class);
    JavaClass subClassWithAccessedField = classes.get(SubclassWithAccessedField.class);
    Set<JavaFieldAccess> accesses = classThatAccessesFieldOfSuperclass.getFieldAccessesFromSelf();
    assertThat(accesses).hasSize(2);
    JavaField field = superclassWithAccessedField.getField("field");
    FieldAccessTarget expectedSuperclassFieldAccess = new FieldAccessTargetBuilder().withOwner(subClassWithAccessedField).withName(field.getName()).withType(field.getRawType()).withMember(() -> Optional.of(field)).build();
    assertThatAccess(getOnly(accesses, "field", GET)).isFrom("accessSuperclassField").isTo(expectedSuperclassFieldAccess).inLineNumber(5);
    assertThatAccess(getOnly(accesses, "maskedField", GET)).isFrom("accessSubclassField").isTo(subClassWithAccessedField.getField("maskedField")).inLineNumber(9);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) FieldAccessTargetBuilder(com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) Test(org.junit.Test)

Example 7 with JavaField

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

the class ClassFileImporterAnnotationsTest method imports_fields_with_complex_annotations_correctly.

@Test
public void imports_fields_with_complex_annotations_correctly() throws Exception {
    JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("enumAndArrayAnnotatedField");
    JavaAnnotation<JavaField> annotation = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class.getName());
    assertThat((JavaEnumConstant) annotation.get("value").get()).isEquivalentTo(OTHER_VALUE);
    assertThat((JavaEnumConstant) annotation.get("valueWithDefault").get()).isEquivalentTo(SOME_VALUE);
    assertThat(((JavaEnumConstant[]) annotation.get("enumArray").get())).matches(SOME_VALUE, OTHER_VALUE);
    assertThat(((JavaEnumConstant[]) annotation.get("enumArrayWithDefault").get())).matches(OTHER_VALUE);
    JavaAnnotation<?> subAnnotation = (JavaAnnotation<?>) annotation.get("subAnnotation").get();
    assertThat(subAnnotation.get("value").get()).isEqualTo("changed");
    assertThat(subAnnotation.getOwner()).isEqualTo(annotation);
    assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(field);
    assertThat(((JavaAnnotation<?>) annotation.get("subAnnotationWithDefault").get()).get("value").get()).isEqualTo("default");
    JavaAnnotation<?>[] subAnnotationArray = (JavaAnnotation<?>[]) annotation.get("subAnnotationArray").get();
    assertThat(subAnnotationArray[0].get("value").get()).isEqualTo("another");
    assertThat(subAnnotationArray[0].getOwner()).isEqualTo(annotation);
    assertThat(subAnnotation.getAnnotatedElement()).isEqualTo(field);
    assertThat(((JavaAnnotation<?>[]) annotation.get("subAnnotationArrayWithDefault").get())[0].get("value").get()).isEqualTo("first");
    assertThatType((JavaClass) annotation.get("clazz").get()).matches(Map.class);
    assertThatType((JavaClass) annotation.get("clazzWithDefault").get()).matches(String.class);
    assertThat((JavaClass[]) annotation.get("classes").get()).matchExactly(Object.class, Serializable.class);
    assertThat((JavaClass[]) annotation.get("classesWithDefault").get()).matchExactly(Serializable.class, List.class);
    assertThat(field).isEquivalentTo(field.getOwner().reflect().getDeclaredField("enumAndArrayAnnotatedField"));
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) JavaEnumConstant(com.tngtech.archunit.core.domain.JavaEnumConstant) Test(org.junit.Test)

Example 8 with JavaField

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

the class ClassFileImporterAnnotationsTest method fields_handle_optional_annotation_correctly.

@Test
public void fields_handle_optional_annotation_correctly() {
    JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("stringAnnotatedField");
    assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class)).isPresent();
    assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class)).isEmpty();
    Optional<JavaAnnotation<JavaField>> optionalAnnotation = field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class.getName());
    assertThat(optionalAnnotation.get().getOwner()).as("owner of optional annotation").isEqualTo(field);
    assertThat(field.tryGetAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithEnumClassAndArrayValue.class.getName())).as("optional annotation").isEmpty();
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 9 with JavaField

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

the class ClassFileImporterAnnotationsTest method imports_fields_with_one_annotation_correctly.

@Test
public void imports_fields_with_one_annotation_correctly() throws Exception {
    JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("stringAnnotatedField");
    JavaAnnotation<JavaField> annotation = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class.getName());
    assertThatType(annotation.getRawType()).matches(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class);
    assertThat(annotation.get("value").get()).isEqualTo("something");
    assertThat(annotation.getOwner()).as("owning field").isEqualTo(field);
    assertThat(field).isEquivalentTo(field.getOwner().reflect().getDeclaredField("stringAnnotatedField"));
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) Test(org.junit.Test)

Example 10 with JavaField

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

the class ClassFileImporterAnnotationsTest method imports_fields_with_two_annotations_correctly.

@Test
public void imports_fields_with_two_annotations_correctly() throws Exception {
    JavaField field = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedFields.class).get(ClassWithAnnotatedFields.class).getField("stringAndIntAnnotatedField");
    Set<JavaAnnotation<JavaField>> annotations = field.getAnnotations();
    assertThat(annotations).hasSize(2);
    assertThat(annotations).extractingResultOf("getOwner").containsOnly(field);
    assertThat(annotations).extractingResultOf("getAnnotatedElement").containsOnly(field);
    JavaAnnotation<JavaField> annotationWithString = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithStringValue.class.getName());
    assertThat(annotationWithString.get("value").get()).isEqualTo("otherThing");
    JavaAnnotation<JavaField> annotationWithInt = field.getAnnotationOfType(ClassWithAnnotatedFields.FieldAnnotationWithIntValue.class.getName());
    assertThat(annotationWithInt.get("intValue").get()).as("Annotation value with default").isEqualTo(0);
    assertThat(annotationWithInt.get("otherValue").get()).isEqualTo("overridden");
    assertThat(field).isEquivalentTo(field.getOwner().reflect().getDeclaredField("stringAndIntAnnotatedField"));
}
Also used : JavaField(com.tngtech.archunit.core.domain.JavaField) ClassWithAnnotatedFields(com.tngtech.archunit.core.importer.testexamples.annotationfieldimport.ClassWithAnnotatedFields) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

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