Search in sources :

Example 1 with FieldAccessTargetBuilder

use of com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder 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)

Aggregations

FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 JavaClass (com.tngtech.archunit.core.domain.JavaClass)1 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)1 JavaField (com.tngtech.archunit.core.domain.JavaField)1 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)1 FieldAccessTargetBuilder (com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder)1 Test (org.junit.Test)1