Search in sources :

Example 21 with JavaMethod

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);
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) TryCatchBlock(com.tngtech.archunit.core.domain.TryCatchBlock) Assertions.assertThatTryCatchBlock(com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock) Test(org.junit.Test)

Example 22 with JavaMethod

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);
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Test(org.junit.Test)

Example 23 with JavaMethod

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));
}
Also used : ClassWithAnnotatedMethods(com.tngtech.archunit.core.importer.testexamples.annotationmethodimport.ClassWithAnnotatedMethods) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 24 with JavaMethod

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]));
}
Also used : ClassWithMethodWithAnnotatedParameters(com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaParameter(com.tngtech.archunit.core.domain.JavaParameter) JavaAnnotation(com.tngtech.archunit.core.domain.JavaAnnotation) Test(org.junit.Test)

Example 25 with JavaMethod

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));
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod)

Aggregations

JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)56 JavaClass (com.tngtech.archunit.core.domain.JavaClass)36 Test (org.junit.Test)32 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)22 ArchCondition (com.tngtech.archunit.lang.ArchCondition)21 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)14 List (java.util.List)12 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)10 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)9 Collectors (java.util.stream.Collectors)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 Stream (java.util.stream.Stream)8 Collections (java.util.Collections)7 JavaField (com.tngtech.archunit.core.domain.JavaField)6 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)5 TryCatchBlock (com.tngtech.archunit.core.domain.TryCatchBlock)5 Assertions.assertThatTryCatchBlock (com.tngtech.archunit.testutil.Assertions.assertThatTryCatchBlock)5