Search in sources :

Example 16 with JavaFieldAccess

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);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 17 with JavaFieldAccess

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"));
}
Also used : ParentClass(com.tngtech.archunit.core.importer.testexamples.complexexternal.ParentClass) JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ClassWithIntAndObjectFields(com.tngtech.archunit.core.importer.testexamples.fieldimport.ClassWithIntAndObjectFields) Test(org.junit.Test)

Example 18 with JavaFieldAccess

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);
}
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 19 with JavaFieldAccess

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"));
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ChildClass(com.tngtech.archunit.core.importer.testexamples.complexexternal.ChildClass) Test(org.junit.Test)

Example 20 with JavaFieldAccess

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);
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Aggregations

JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)22 Test (org.junit.Test)21 JavaClass (com.tngtech.archunit.core.domain.JavaClass)16 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)10 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)3 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)1 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)1 Formatters.formatNamesOf (com.tngtech.archunit.core.domain.Formatters.formatNamesOf)1 JavaAccess (com.tngtech.archunit.core.domain.JavaAccess)1 CONSTRUCTOR_NAME (com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME)1 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaField (com.tngtech.archunit.core.domain.JavaField)1 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 Utils.namesOf (com.tngtech.archunit.core.domain.properties.HasName.Utils.namesOf)1 FieldAccessRecord (com.tngtech.archunit.core.importer.AccessRecord.FieldAccessRecord)1 FieldAccessTargetBuilder (com.tngtech.archunit.core.importer.DomainBuilders.FieldAccessTargetBuilder)1