use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_other_static_field_accesses.
@Test
public void imports_other_static_field_accesses() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
JavaClass classWithOwnFieldAccess = classes.get(OwnStaticFieldAccess.class);
JavaClass classWithForeignFieldAccess = classes.get(ForeignStaticFieldAccess.class);
Set<JavaFieldAccess> accesses = classWithForeignFieldAccess.getFieldAccessesFromSelf();
assertThat(accesses).as("Number of field accesses from " + classWithForeignFieldAccess.getName()).hasSize(2);
assertThatAccess(getOnly(accesses, "staticStringValue", GET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getStaticStringFromOther")).isTo(classWithOwnFieldAccess.getField("staticStringValue")).inLineNumber(5);
assertThatAccess(getOnly(accesses, "staticStringValue", SET)).isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setStaticStringFromOther")).isTo(classWithOwnFieldAccess.getField("staticStringValue")).inLineNumber(9);
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_other_field_accesses_from_static_initializer.
@Test
public void imports_other_field_accesses_from_static_initializer() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport"));
JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
JavaClass fieldAccessFromInitializer = classes.get(ForeignFieldAccessFromStaticInitializer.class);
Set<JavaFieldAccess> accesses = fieldAccessFromInitializer.getFieldAccessesFromSelf();
assertThat(accesses).as("Number of field accesses from " + fieldAccessFromInitializer.getName()).hasSize(2);
assertThatAccess(getOnly(accesses, "stringValue", GET)).isFrom(fieldAccessFromInitializer.getCodeUnitWithParameterTypes(STATIC_INITIALIZER_NAME)).isTo(classWithOwnFieldAccess.getField("stringValue")).inLineNumber(5);
assertThatAccess(getOnly(accesses, "intValue", SET)).isFrom(fieldAccessFromInitializer.getCodeUnitWithParameterTypes(STATIC_INITIALIZER_NAME)).isTo(classWithOwnFieldAccess.getField("intValue")).inLineNumber(6);
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method classes_know_shadowed_field_accesses_to_themselves.
@Test
public void classes_know_shadowed_field_accesses_to_themselves() {
@SuppressWarnings("unused")
class Base {
String shadowed;
String nonShadowed;
}
class Child extends Base {
String shadowed;
}
@SuppressWarnings("unused")
class Accessor {
void access(Child child) {
consume(child.shadowed);
consume(child.nonShadowed);
}
void consume(String string) {
}
}
JavaClasses classes = new ClassFileImporter().importClasses(Accessor.class, Base.class, Child.class);
JavaFieldAccess access = getOnlyByCaller(classes.get(Base.class).getFieldAccessesToSelf(), classes.get(Accessor.class).getMethod("access", Child.class));
assertThatAccess(access).isFrom("access", Child.class).isTo("nonShadowed");
access = getOnlyByCaller(classes.get(Child.class).getFieldAccessesToSelf(), classes.get(Accessor.class).getMethod("access", Child.class));
assertThatAccess(access).isFrom("access", Child.class).isTo("shadowed");
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_field_access_target_owners.
@Test
public void automatically_resolves_field_access_target_owners() {
class Target {
String field;
}
@SuppressWarnings({ "unused", "ConstantConditions" })
class Origin {
Object resolvesFieldAccessOwner() {
Target target = null;
return target.field;
}
}
JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
JavaFieldAccess access = getOnlyElement(javaClass.getMethod("resolvesFieldAccessOwner").getFieldAccesses());
assertThat(access.getTargetOwner()).isFullyImported(true);
}
use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method classes_know_the_field_accesses_to_them.
@Test
public void classes_know_the_field_accesses_to_them() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/dependents"));
JavaClass classHoldingDependencies = classes.get(ClassHoldingDependencies.class);
JavaClass firstClassWithDependency = classes.get(FirstClassWithDependency.class);
JavaClass secondClassWithDependency = classes.get(SecondClassWithDependency.class);
Set<JavaFieldAccess> accesses = classHoldingDependencies.getFieldAccessesToSelf();
Set<JavaFieldAccess> expected = ImmutableSet.<JavaFieldAccess>builder().addAll(classHoldingDependencies.getFieldAccessesFromSelf()).addAll(firstClassWithDependency.getFieldAccessesFromSelf()).addAll(secondClassWithDependency.getFieldAccessesFromSelf()).build();
assertThat(accesses).as("Field Accesses to class").isEqualTo(expected);
}
Aggregations