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);
}
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"));
}
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();
}
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"));
}
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"));
}
Aggregations