use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_try_catch_block_with_multiple_caught_throwables.
@Test
public void imports_try_catch_block_with_multiple_caught_throwables() {
@SuppressWarnings("unused")
class SomeClass {
void method() {
try {
new Object();
} catch (IllegalStateException | IllegalArgumentException ignored) {
} catch (UnsupportedOperationException ignored) {
System.out.println("unsupported");
} finally {
System.out.println("finally");
}
}
}
JavaMethod method = new ClassFileImporter().importClass(SomeClass.class).getMethod("method");
TryCatchBlock tryCatchBlock = getOnlyElement(method.getTryCatchBlocks());
assertThatTypes(tryCatchBlock.getCaughtThrowables()).matchInAnyOrder(IllegalStateException.class, IllegalArgumentException.class, UnsupportedOperationException.class);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method imports_shadowed_and_superclass_method_calls.
@Test
public void imports_shadowed_and_superclass_method_calls() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/hierarchicalmethodcall"));
JavaClass classThatCallsMethodOfSuperclass = classes.get(CallOfSuperAndSubclassMethod.class);
JavaClass superclassWithCalledMethod = classes.get(SuperclassWithCalledMethod.class);
JavaClass subClassWithCalledMethod = classes.get(SubclassWithCalledMethod.class);
Set<JavaMethodCall> calls = classThatCallsMethodOfSuperclass.getMethodCallsFromSelf();
assertThat(calls).hasSize(2);
JavaCodeUnit callSuperclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSuperclassMethod);
JavaMethod expectedSuperclassMethod = superclassWithCalledMethod.getMethod(SuperclassWithCalledMethod.method);
MethodCallTarget expectedSuperclassCall = newMethodCallTargetBuilder().withOwner(subClassWithCalledMethod).withName(expectedSuperclassMethod.getName()).withParameters(expectedSuperclassMethod.getRawParameterTypes()).withReturnType(expectedSuperclassMethod.getRawReturnType()).withMember(() -> Optional.of(expectedSuperclassMethod)).build();
assertThatCall(getOnlyByCaller(calls, callSuperclassMethod)).isFrom(callSuperclassMethod).isTo(expectedSuperclassCall).inLineNumber(CallOfSuperAndSubclassMethod.callSuperclassLineNumber);
JavaCodeUnit callSubclassMethod = classThatCallsMethodOfSuperclass.getCodeUnitWithParameterTypes(CallOfSuperAndSubclassMethod.callSubclassMethod);
assertThatCall(getOnlyByCaller(calls, callSubclassMethod)).isFrom(callSubclassMethod).isTo(subClassWithCalledMethod.getMethod(SubclassWithCalledMethod.maskedMethod)).inLineNumber(CallOfSuperAndSubclassMethod.callSubclassLineNumber);
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_methods_with_two_annotations_correctly.
@Test
public void imports_methods_with_two_annotations_correctly() throws Exception {
JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithAnnotatedMethods.class).get(ClassWithAnnotatedMethods.class).getMethod(stringAndIntAnnotatedMethod);
Set<JavaAnnotation<JavaMethod>> annotations = method.getAnnotations();
assertThat(annotations).hasSize(2);
assertThat(annotations).extractingResultOf("getOwner").containsOnly(method);
assertThat(annotations).extractingResultOf("getAnnotatedElement").containsOnly(method);
JavaAnnotation<?> annotationWithString = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithStringValue.class.getName());
assertThat(annotationWithString.get("value").get()).isEqualTo("otherThing");
JavaAnnotation<?> annotationWithInt = method.getAnnotationOfType(ClassWithAnnotatedMethods.MethodAnnotationWithIntValue.class.getName());
assertThat(annotationWithInt.get("otherValue").get()).isEqualTo("overridden");
assertThat(method).isEquivalentTo(ClassWithAnnotatedMethods.class.getMethod(stringAndIntAnnotatedMethod));
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAnnotationsTest method imports_method_with_one_parameter_with_two_annotations.
@Test
public void imports_method_with_one_parameter_with_two_annotations() {
JavaMethod method = new ClassFileImporter().importPackagesOf(ClassWithMethodWithAnnotatedParameters.class).get(ClassWithMethodWithAnnotatedParameters.class).getMethod(ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithTwoAnnotations, String.class);
List<Set<JavaAnnotation<JavaParameter>>> parameterAnnotations = method.getParameterAnnotations();
Set<JavaAnnotation<JavaParameter>> annotations = getOnlyElement(parameterAnnotations);
assertThat(annotations).isEqualTo(getOnlyElement(method.getParameters()).getAnnotations());
assertThatAnnotations(annotations).match(ImmutableSet.copyOf(method.reflect().getParameterAnnotations()[0]));
}
use of com.tngtech.archunit.core.domain.JavaMethod in project ArchUnit by TNG.
the class ClassFileImporterAutomaticResolutionTest method importFirstTypeArgumentMethodParameterBound.
private static JavaType importFirstTypeArgumentMethodParameterBound(Class<?> clazz) {
JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_PROPERTY_NAME, MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_DEFAULT_VALUE).importClass(clazz);
JavaMethod method = getOnlyElement(javaClass.getMethods());
return getFirstTypeArgumentUpperBound(method.getParameterTypes().get(0));
}
Aggregations