Search in sources :

Example 6 with JavaFieldAccess

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);
}
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)

Example 7 with JavaFieldAccess

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);
}
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)

Example 8 with JavaFieldAccess

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

Example 9 with JavaFieldAccess

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

Example 10 with JavaFieldAccess

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);
}
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