Java Code Examples for com.tngtech.archunit.core.domain.JavaClass#getFieldAccessesFromSelf()

The following examples show how to use com.tngtech.archunit.core.domain.JavaClass#getFieldAccessesFromSelf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_multiple_own_accesses() throws Exception {
    JavaClass classWithOwnFieldAccess = classesIn("testexamples/fieldaccessimport").get(OwnFieldAccess.class);

    Set<JavaFieldAccess> fieldAccesses = classWithOwnFieldAccess.getFieldAccessesFromSelf();

    assertThat(fieldAccesses).hasSize(4);
    assertThat(getOnly(fieldAccesses, "stringValue", GET).getLineNumber())
            .as("Line number of get stringValue").isEqualTo(8);
    assertThat(getOnly(fieldAccesses, "stringValue", SET).getLineNumber())
            .as("Line number of set stringValue").isEqualTo(12);
    assertThat(getOnly(fieldAccesses, "intValue", GET).getLineNumber())
            .as("Line number of get intValue").isEqualTo(16);
    assertThat(getOnly(fieldAccesses, "intValue", SET).getLineNumber())
            .as("Line number of set intValue").isEqualTo(20);
}
 
Example 2
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_own_static_field_accesses() throws Exception {
    JavaClass classWithOwnFieldAccess = classesIn("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);
}
 
Example 3
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_other_static_field_accesses() throws Exception {
    ImportedClasses classes = classesIn("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);
}
 
Example 4
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_other_field_accesses_from_constructor() throws Exception {
    ImportedClasses classes = classesIn("testexamples/fieldaccessimport");
    JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
    JavaClass fieldAccessFromConstructor = classes.get(ForeignFieldAccessFromConstructor.class);

    Set<JavaFieldAccess> accesses = fieldAccessFromConstructor.getFieldAccessesFromSelf();

    assertThat(accesses).as("Number of field accesses from " + fieldAccessFromConstructor.getName()).hasSize(2);

    assertThatAccess(getOnly(accesses, "stringValue", GET))
            .isFrom(fieldAccessFromConstructor.getCodeUnitWithParameterTypes(CONSTRUCTOR_NAME))
            .isTo(classWithOwnFieldAccess.getField("stringValue"))
            .inLineNumber(5);

    assertThatAccess(getOnly(accesses, "intValue", SET))
            .isFrom(fieldAccessFromConstructor.getCodeUnitWithParameterTypes(CONSTRUCTOR_NAME))
            .isTo(classWithOwnFieldAccess.getField("intValue"))
            .inLineNumber(6);
}
 
Example 5
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_other_field_accesses_from_static_initializer() throws Exception {
    ImportedClasses classes = classesIn("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);
}
 
Example 6
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
public void imports_shadowed_and_superclass_field_access() throws Exception {
    ImportedClasses classes = classesIn("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())
            .withField(Suppliers.ofInstance(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);
}
 
Example 7
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void imports_other_field_accesses() throws Exception {
    ImportedClasses classes = classesIn("testexamples/fieldaccessimport");
    JavaClass classWithOwnFieldAccess = classes.get(OwnFieldAccess.class);
    JavaClass classWithForeignFieldAccess = classes.get(ForeignFieldAccess.class);

    Set<JavaFieldAccess> accesses = classWithForeignFieldAccess.getFieldAccessesFromSelf();

    assertThat(accesses).hasSize(4);

    assertThatAccess(getOnly(accesses, "stringValue", GET))
            .isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getStringFromOther"))
            .isTo(classWithOwnFieldAccess.getField("stringValue"))
            .inLineNumber(5);

    assertThatAccess(getOnly(accesses, "stringValue", SET))
            .isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setStringFromOther"))
            .isTo(classWithOwnFieldAccess.getField("stringValue"))
            .inLineNumber(9);

    assertThatAccess(getOnly(accesses, "intValue", GET))
            .isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("getIntFromOther"))
            .isTo(classWithOwnFieldAccess.getField("intValue"))
            .inLineNumber(13);

    assertThatAccess(getOnly(accesses, "intValue", SET))
            .isFrom(classWithForeignFieldAccess.getCodeUnitWithParameterTypes("setIntFromOther"))
            .isTo(classWithOwnFieldAccess.getField("intValue"))
            .inLineNumber(17);
}
 
Example 8
Source File: ClassFileImporterTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void imports_multiple_accesses_from_same_method() throws Exception {
    ImportedClasses classes = classesIn("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);
}
 
Example 9
Source File: ClassAccessesFieldCondition.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@Override
Collection<JavaFieldAccess> relevantAttributes(JavaClass item) {
    return item.getFieldAccessesFromSelf();
}