use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_enclosing_classes.
@Test
public void automatically_resolves_enclosing_classes() throws ClassNotFoundException {
@SuppressWarnings("unused")
class Outermost {
class LessOuter {
class LeastOuter {
void call() {
class LessInner {
class Innermost {
}
}
}
}
}
}
Class<?> lessInnerClass = Class.forName(Outermost.LessOuter.LeastOuter.class.getName() + "$1LessInner");
JavaClass innermost = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ENCLOSING_TYPES_PROPERTY_NAME, MAX_ITERATIONS_FOR_ENCLOSING_TYPES_DEFAULT_VALUE).importClass(Class.forName(lessInnerClass.getName() + "$Innermost"));
JavaClass lessInner = innermost.getEnclosingClass().get();
assertThat(lessInner).isFullyImported(true);
assertThatType(lessInner).matches(lessInnerClass);
JavaClass leastOuter = lessInner.getEnclosingClass().get();
assertThat(leastOuter).isFullyImported(true);
assertThatType(leastOuter).matches(Outermost.LessOuter.LeastOuter.class);
JavaClass lessOuter = leastOuter.getEnclosingClass().get();
assertThat(lessOuter).isFullyImported(true);
assertThatType(lessOuter).matches(Outermost.LessOuter.class);
JavaClass outermost = lessOuter.getEnclosingClass().get();
assertThat(outermost).isFullyImported(true);
assertThatType(outermost).matches(Outermost.class);
}
use of com.tngtech.archunit.core.domain.JavaClass 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.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterCodeUnitReferencesTest method imports_method_and_constructor_references_to_self_as_accesses.
@Test
public void imports_method_and_constructor_references_to_self_as_accesses() {
JavaClasses javaClasses = new ClassFileImporter().importClasses(Data_imports_method_and_constructor_references_as_accesses.Origin.class, Data_imports_method_and_constructor_references_as_accesses.ReferencedTarget.class);
JavaClass targetClass = javaClasses.get(Data_imports_method_and_constructor_references_as_accesses.ReferencedTarget.class);
assertThat(targetClass.getAccessesToSelf()).containsAll(targetClass.getCodeUnitAccessesToSelf());
assertThat(targetClass.getCodeUnitAccessesToSelf()).containsAll(targetClass.getCodeUnitReferencesToSelf());
assertThat(targetClass.getCodeUnitReferencesToSelf()).hasSize(8).containsAll(targetClass.getMethodReferencesToSelf()).containsAll(targetClass.getConstructorReferencesToSelf()).containsAll(targetClass.getMethod("call").getReferencesToSelf()).containsAll(targetClass.getConstructor().getReferencesToSelf());
assertThat(targetClass.getConstructorReferencesToSelf()).hasSize(4).containsAll(targetClass.getConstructor().getReferencesToSelf());
assertThat(targetClass.getMethodReferencesToSelf()).hasSize(4).containsAll(targetClass.getMethod("call").getReferencesToSelf());
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterCodeUnitReferencesTest method test_imports_instance_method_references_bound_to_instance.
@Test
@UseDataProvider
public void test_imports_instance_method_references_bound_to_instance(Class<?> originClassInput, Class<?> targetClassInput) {
JavaClasses javaClasses = new ClassFileImporter().importClasses(originClassInput, targetClassInput);
JavaClass originClass = javaClasses.get(originClassInput);
JavaClass targetClass = javaClasses.get(targetClassInput);
assertThatAccess(getOnlyElement(originClass.getMethod("referencesInstanceMethodBoundToInstance").getMethodReferencesFromSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
assertThatAccess(getOnlyElement(targetClass.getMethod("instanceMethodToReference").getReferencesToSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterCodeUnitReferencesTest method test_imports_instance_method_references_not_bound_to_instance.
@Test
@UseDataProvider
public void test_imports_instance_method_references_not_bound_to_instance(Class<?> originClassInput, Class<?> targetClassInput) {
JavaClasses javaClasses = new ClassFileImporter().importClasses(originClassInput, targetClassInput);
JavaClass originClass = javaClasses.get(originClassInput);
JavaClass targetClass = javaClasses.get(targetClassInput);
assertThatAccess(getOnlyElement(originClass.getMethod("referencesInstanceMethodNotBoundToInstance").getMethodReferencesFromSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodNotBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
assertThatAccess(getOnlyElement(targetClass.getMethod("instanceMethodToReference").getReferencesToSelf())).isFrom(originClass.getCodeUnitWithParameterTypeNames("referencesInstanceMethodNotBoundToInstance")).isTo(targetClass.getMethod("instanceMethodToReference"));
}
Aggregations