Search in sources :

Example 1 with JavaMethodReference

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_method_reference_target_owners.

@Test
public void automatically_resolves_method_reference_target_owners() {
    class Target {

        void method() {
        }
    }
    @SuppressWarnings({ "unused", "ConstantConditions" })
    class Origin {

        Runnable resolvesMethodCallTargetOwner() {
            Target target = null;
            return target::method;
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
    JavaMethodReference reference = getOnlyElement(javaClass.getMethodReferencesFromSelf());
    assertThat(reference.getTargetOwner()).isFullyImported(true);
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 2 with JavaMethodReference

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

the class ClassFileImporterCodeUnitReferencesTest method creates_correct_description_for_references.

@Test
public void creates_correct_description_for_references() {
    JavaClasses javaClasses = new ClassFileImporter().importClasses(Origin.class, Target.class);
    JavaClass originClass = javaClasses.get(Origin.class);
    JavaClass targetClass = javaClasses.get(Target.class);
    JavaMethod constructorReferenceOrigin = originClass.getMethod("referencesConstructor");
    JavaConstructor targetConstructor = targetClass.getConstructor();
    JavaMethod methodReferenceOrigin = originClass.getMethod("referencesMethod");
    JavaMethod targetMethod = targetClass.getMethod("call");
    JavaConstructorReference constructorReference = getOnlyElement(constructorReferenceOrigin.getConstructorReferencesFromSelf());
    assertThat(constructorReference.getDescription()).isEqualTo(String.format("Method <%s> references constructor <%s> in (%s.java:%d)", constructorReferenceOrigin.getFullName(), targetConstructor.getFullName(), Origin.class.getSimpleName(), 9));
    JavaMethodReference methodReference = getOnlyElement(originClass.getMethod("referencesMethod").getMethodReferencesFromSelf());
    assertThat(methodReference.getDescription()).isEqualTo(String.format("Method <%s> references method <%s> in (%s.java:%d)", methodReferenceOrigin.getFullName(), targetMethod.getFullName(), Origin.class.getSimpleName(), 13));
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Test(org.junit.Test)

Example 3 with JavaMethodReference

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

the class ClassGraphCreator method createMethodReferencesFor.

@Override
public Set<JavaMethodReference> createMethodReferencesFor(JavaCodeUnit codeUnit, Set<TryCatchBlockBuilder> tryCatchBlockBuilders) {
    ImmutableSet.Builder<JavaMethodReference> result = ImmutableSet.builder();
    for (AccessRecord<MethodReferenceTarget> record : processedMethodReferenceRecords.get(codeUnit)) {
        JavaMethodReference methodReference = accessBuilderFrom(new JavaMethodReferenceBuilder(), record).build();
        result.add(methodReference);
        handlePossibleTryBlockAccess(tryCatchBlockBuilders, record, methodReference);
    }
    return result.build();
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) ImmutableSet(com.google.common.collect.ImmutableSet) MethodReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget) JavaMethodReferenceBuilder(com.tngtech.archunit.core.importer.DomainBuilders.JavaMethodReferenceBuilder)

Example 4 with JavaMethodReference

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

the class ClassFileImporterLambdaAccessesTest method imports_method_reference_from_lambda_without_parameter.

@Test
public void imports_method_reference_from_lambda_without_parameter() {
    class Target {

        void target() {
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        Supplier<Consumer<Target>> call() {
            return () -> Target::target;
        }
    }
    JavaClasses classes = new ClassFileImporter().importClasses(Target.class, Caller.class);
    JavaMethodReference reference = getOnlyElement(classes.get(Caller.class).getMethodReferencesFromSelf());
    assertThatAccess(reference).isFrom("call").isTo(Target.class, "target");
}
Also used : JavaMethodReference(com.tngtech.archunit.core.domain.JavaMethodReference) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) Consumer(java.util.function.Consumer) Test(org.junit.Test)

Aggregations

JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)4 Test (org.junit.Test)3 JavaClass (com.tngtech.archunit.core.domain.JavaClass)2 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 MethodReferenceTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 JavaMethodReferenceBuilder (com.tngtech.archunit.core.importer.DomainBuilders.JavaMethodReferenceBuilder)1 Consumer (java.util.function.Consumer)1