use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_own_static_field_accesses.
@Test
public void imports_own_static_field_accesses() {
JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnStaticFieldAccess.class);
Set<JavaFieldAccess> accesses = classWithOwnFieldAccess.getFieldAccessesFromSelf();
assertThat(accesses).hasSize(2);
JavaFieldAccess getAccess = getOnly(accesses, "staticStringValue", GET);
assertThatAccess(getAccess).isFrom("getStaticStringValue").isTo("staticStringValue").inLineNumber(7);
JavaFieldAccess setAccess = getOnly(accesses, "staticStringValue", SET);
assertThatAccess(setAccess).isFrom("setStaticStringValue", String.class).isTo("staticStringValue").inLineNumber(11);
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_external_field_access.
@Test
public void imports_external_field_access() {
JavaClass classWithExternalFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(ExternalFieldAccess.class);
JavaFieldAccess access = getOnlyElement(classWithExternalFieldAccess.getMethod("access").getFieldAccesses());
assertThatAccess(access).isFrom(classWithExternalFieldAccess.getCodeUnitWithParameterTypes("access")).inLineNumber(8);
assertThat(access.getTarget()).isEquivalentTo(field(ClassWithIntAndObjectFields.class, "objectField"));
access = getOnlyElement(classWithExternalFieldAccess.getMethod("accessInheritedExternalField").getFieldAccesses());
assertThatAccess(access).isFrom(classWithExternalFieldAccess.getCodeUnitWithParameterTypes("accessInheritedExternalField")).inLineNumber(12);
assertThat(access.getTarget()).isEquivalentTo(field(ParentClass.class, "someParentField"));
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess 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.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_external_field_access_with_shadowed_field.
@Test
public void imports_external_field_access_with_shadowed_field() {
JavaClass classWithExternalFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(ExternalShadowedFieldAccess.class);
JavaFieldAccess access = getOnlyElement(classWithExternalFieldAccess.getFieldAccessesFromSelf());
assertThatAccess(access).isFrom(classWithExternalFieldAccess.getCodeUnitWithParameterTypes("accessField")).inLineNumber(7);
assertThat(access.getTarget()).isEquivalentTo(field(ChildClass.class, "someField"));
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_multiple_accesses_from_same_method.
@Test
public void imports_multiple_accesses_from_same_method() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
JavaClass multipleFieldAccesses = classes.get(MultipleFieldAccessInSameMethod.class);
Set<JavaFieldAccess> accesses = multipleFieldAccesses.getFieldAccessesFromSelf();
assertThat(accesses).as("Number of field accesses from " + multipleFieldAccesses.getName()).hasSize(5);
Set<JavaFieldAccess> setStringValues = getByNameAndAccessType(accesses, "stringValue", SET);
assertThat(setStringValues).hasSize(2);
assertThat(targetsOf(setStringValues)).containsOnly(targetFrom(classWithOwnFieldAccess.getField("stringValue")));
assertThat(lineNumbersOf(setStringValues)).containsOnly(6, 8);
assertThatAccess(getOnly(accesses, "stringValue", GET)).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(7);
assertThatAccess(getOnly(accesses, "intValue", GET)).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(10);
assertThatAccess(getOnly(accesses, "intValue", SET)).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(11);
}
Aggregations