use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method all_accesses_know_which_exceptions_are_handled.
@Test
public void all_accesses_know_which_exceptions_are_handled() {
@SuppressWarnings({ "unused", "WriteOnlyObject" })
class Origin {
void method() {
new ArrayList<>().add(1);
try {
Data_all_accesses_know_which_exceptions_are_handled.Target target = new Data_all_accesses_know_which_exceptions_are_handled.Target();
target.target(target.field);
} catch (IllegalStateException e) {
e.printStackTrace();
}
new Object();
}
}
JavaClasses classes = new ClassFileImporter().importClasses(Origin.class, Data_all_accesses_know_which_exceptions_are_handled.Target.class);
JavaMethod method = classes.get(Origin.class).getMethod("method");
Set<JavaAccess<?>> accesses = method.getAccessesFromSelf();
Function<JavaAccess<?>, Boolean> targetsTarget = GET_TARGET.then(Get.owner()).then(equivalentTo(Data_all_accesses_know_which_exceptions_are_handled.Target.class)::test);
Map<Boolean, List<JavaAccess<?>>> accessesByTargetsTarget = accesses.stream().collect(groupingBy(targetsTarget));
assertThat(accessesByTargetsTarget.get(true)).as("accesses that target target").isNotEmpty();
for (JavaAccess<?> access : accessesByTargetsTarget.get(true)) {
assertThatTryCatchBlock(getOnlyElement(access.getContainingTryBlocks())).isDeclaredIn(method).catches(IllegalStateException.class);
}
assertThat(accessesByTargetsTarget.get(false)).as("accesses that do not target target").isNotEmpty();
for (JavaAccess<?> access : accessesByTargetsTarget.get(false)) {
assertThat(access.getContainingTryBlocks()).as("containing try-catch-blocks").isEmpty();
}
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks.
@Test
public void method_calls_know_which_exceptions_are_handled_in_nested_complex_try_catch_blocks() {
JavaClasses classes = new ClassFileImporter().importClasses(ClassWithComplexTryCatchBlocks.class, ClassHoldingMethods.class);
JavaClass classHoldingMethods = classes.get(ClassHoldingMethods.class);
JavaClass classWithTryCatchBlocks = classes.get(ClassWithComplexTryCatchBlocks.class);
JavaMethod setSomeInt = classHoldingMethods.getMethod("setSomeInt", int.class);
JavaMethod setSomeString = classHoldingMethods.getMethod("setSomeString", String.class);
JavaMethod doSomething = classHoldingMethods.getMethod("doSomething");
JavaConstructor constructor = classWithTryCatchBlocks.getConstructor();
assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(12)).isNotWrappedInTryCatch();
assertThatCall(methodCallTo(setSomeInt).from(constructor).inLineNumber(14)).isWrappedWithTryCatchFor(Exception.class);
assertThatCall(methodCallTo(doSomething).from(constructor).inLineNumber(17)).isWrappedWithTryCatchFor(Exception.class).isWrappedWithTryCatchFor(IllegalArgumentException.class).isWrappedWithTryCatchFor(UnsupportedOperationException.class);
assertThatCall(methodCallTo(setSomeString).from(constructor).inLineNumber(26)).isWrappedWithTryCatchFor(Throwable.class);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_own_get_field_access.
@Test
public void imports_own_get_field_access() {
JavaClass classWithOwnFieldAccess = new ClassFileImporter().importUrl(getClass().getResource("testexamples/fieldaccessimport")).get(OwnFieldAccess.class);
JavaMethod getStringValue = classWithOwnFieldAccess.getMethod("getStringValue");
JavaFieldAccess access = getOnlyElement(getStringValue.getFieldAccesses());
assertThatAccess(access).isOfType(GET).isFrom(getStringValue).isTo("stringValue").inLineNumber(8);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_simple_try_catch_block.
@Test
public void imports_simple_try_catch_block() {
@SuppressWarnings("unused")
class SomeClass {
void method() {
try {
new Object();
} catch (IllegalStateException ignored) {
}
}
}
JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchExactly(IllegalStateException.class);
assertThat(tryCatchBlock.getAccessesContainedInTryBlock()).isEqualTo(method.getAccessesFromSelf());
assertThat(tryCatchBlock.getOwner()).isEqualTo(method);
assertThat(tryCatchBlock.getSourceCodeLocation()).isEqualTo(getOnlyElement(method.getAccessesFromSelf()).getSourceCodeLocation());
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_simple_try_catch_block_that_returns_directly_from_try.
@Test
public void imports_simple_try_catch_block_that_returns_directly_from_try() {
// In this case the end-label has no associated line number, so it must be handled differently
@SuppressWarnings("unused")
class SomeClass {
Object method() {
try {
return new Object();
} catch (IllegalStateException ignored) {
return null;
}
}
}
JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchExactly(IllegalStateException.class);
assertThat(tryCatchBlock.getAccessesContainedInTryBlock()).isEqualTo(method.getAccessesFromSelf());
}
Aggregations