Search in sources :

Example 1 with JavaFieldAccess

use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.

the class ClassGraphCreator method createFieldAccessesFor.

@Override
public Set<JavaFieldAccess> createFieldAccessesFor(JavaCodeUnit codeUnit, Set<TryCatchBlockBuilder> tryCatchBlockBuilders) {
    ImmutableSet.Builder<JavaFieldAccess> result = ImmutableSet.builder();
    for (FieldAccessRecord record : processedFieldAccessRecords.get(codeUnit)) {
        JavaFieldAccess access = accessBuilderFrom(new JavaFieldAccessBuilder(), record).withAccessType(record.getAccessType()).build();
        result.add(access);
        handlePossibleTryBlockAccess(tryCatchBlockBuilders, record, access);
    }
    return result.build();
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) ImmutableSet(com.google.common.collect.ImmutableSet) FieldAccessRecord(com.tngtech.archunit.core.importer.AccessRecord.FieldAccessRecord) JavaFieldAccessBuilder(com.tngtech.archunit.core.importer.DomainBuilders.JavaFieldAccessBuilder)

Example 2 with JavaFieldAccess

use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.

the class FieldAccessConditionTest method FieldGetAccessCondition_only_satisfied_on_get_field.

@Test
public void FieldGetAccessCondition_only_satisfied_on_get_field() {
    JavaFieldAccess getAccess = accessFromCallerToTargetWithType(GET);
    FieldGetAccessCondition getFieldCondition = new FieldGetAccessCondition(target(name(getAccess.getTarget().getName())));
    assertSatisfiedWithMessage(getFieldCondition, getAccess, "gets");
    JavaFieldAccess setAccess = accessFromCallerToTargetWithType(SET);
    getFieldCondition = new FieldGetAccessCondition(target(name(setAccess.getTarget().getName())));
    assertViolatedWithMessage(getFieldCondition, setAccess, "sets");
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) FieldGetAccessCondition(com.tngtech.archunit.lang.conditions.FieldAccessCondition.FieldGetAccessCondition) Test(org.junit.Test)

Example 3 with JavaFieldAccess

use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.

the class FieldAccessConditionTest method FieldSetAccessCondition_only_satisfied_on_set_field.

@Test
public void FieldSetAccessCondition_only_satisfied_on_set_field() {
    JavaFieldAccess setAccess = accessFromCallerToTargetWithType(SET);
    FieldSetAccessCondition setFieldCondition = new FieldSetAccessCondition(target(name(setAccess.getTarget().getName())));
    assertSatisfiedWithMessage(setFieldCondition, setAccess, "sets");
    JavaFieldAccess getAccess = accessFromCallerToTargetWithType(GET);
    setFieldCondition = new FieldSetAccessCondition(target(name(getAccess.getTarget().getName())));
    assertViolatedWithMessage(setFieldCondition, getAccess, "gets");
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) FieldSetAccessCondition(com.tngtech.archunit.lang.conditions.FieldAccessCondition.FieldSetAccessCondition) Test(org.junit.Test)

Example 4 with JavaFieldAccess

use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.

the class ViolationHandlingTest method multiple_accesses_necessary_for_violation_are_reported_together.

@Test
public void multiple_accesses_necessary_for_violation_are_reported_together() {
    EvaluationResult result = classes().should().accessField(Target.class, "notExisting").evaluate(importClasses(Origin.class, Target.class));
    FieldAccessRecorder fieldAccessRecorder = new FieldAccessRecorder();
    result.handleViolations(fieldAccessRecorder);
    assertThat(fieldAccessRecorder.getRecords()).as("number of violations").hasSize(1);
    ReportedViolation<JavaFieldAccess> reportedViolation = getOnlyElement(fieldAccessRecorder.getRecords());
    assertThatAccesses(reportedViolation.accesses).contain(accessToTargetField("fieldOne")).contain(accessToTargetField("fieldTwo")).contain(accessToTargetField("fieldThree"));
}
Also used : JavaFieldAccess(com.tngtech.archunit.core.domain.JavaFieldAccess) EvaluationResult(com.tngtech.archunit.lang.EvaluationResult) Test(org.junit.Test)

Example 5 with JavaFieldAccess

use of com.tngtech.archunit.core.domain.JavaFieldAccess in project ArchUnit by TNG.

the class ClassFileImporterAccessesTest method fields_know_their_accesses.

@Test
public void fields_know_their_accesses() {
    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.getField("someInt").getAccessesToSelf();
    Set<JavaFieldAccess> expected = ImmutableSet.<JavaFieldAccess>builder().addAll(getByName(classHoldingDependencies.getFieldAccessesFromSelf(), "someInt")).addAll(getByName(firstClassWithDependency.getFieldAccessesFromSelf(), "someInt")).addAll(getByName(secondClassWithDependency.getFieldAccessesFromSelf(), "someInt")).build();
    assertThat(accesses).as("Field Accesses to someInt").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