Search in sources :

Example 1 with JavaConstructorReference

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

the class ClassFileImporterAutomaticResolutionTest method automatically_resolves_constructor_reference_target_owners.

@Test
public void automatically_resolves_constructor_reference_target_owners() {
    @SuppressWarnings("unused")
    class Origin {

        Supplier<?> resolvesConstructorReferenceTargetOwner() {
            return Data_automatically_resolves_constructor_reference_target_owners.Target::new;
        }
    }
    JavaClass javaClass = ImporterWithAdjustedResolutionRuns.disableAllIterationsExcept(MAX_ITERATIONS_FOR_ACCESSES_TO_TYPES_PROPERTY_NAME).importClass(Origin.class);
    JavaConstructorReference reference = getOnlyElement(javaClass.getMethod("resolvesConstructorReferenceTargetOwner").getConstructorReferencesFromSelf());
    assertThat(reference.getTargetOwner()).isFullyImported(true);
}
Also used : JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Example 2 with JavaConstructorReference

use of com.tngtech.archunit.core.domain.JavaConstructorReference 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 JavaConstructorReference

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

the class ClassGraphCreator method createConstructorReferencesFor.

@Override
public Set<JavaConstructorReference> createConstructorReferencesFor(JavaCodeUnit codeUnit, Set<TryCatchBlockBuilder> tryCatchBlockBuilders) {
    ImmutableSet.Builder<JavaConstructorReference> result = ImmutableSet.builder();
    for (AccessRecord<ConstructorReferenceTarget> record : processedConstructorReferenceRecords.get(codeUnit)) {
        JavaConstructorReference constructorReference = accessBuilderFrom(new JavaConstructorReferenceBuilder(), record).build();
        result.add(constructorReference);
        handlePossibleTryBlockAccess(tryCatchBlockBuilders, record, constructorReference);
    }
    return result.build();
}
Also used : JavaConstructorReference(com.tngtech.archunit.core.domain.JavaConstructorReference) ImmutableSet(com.google.common.collect.ImmutableSet) ConstructorReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget) JavaConstructorReferenceBuilder(com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorReferenceBuilder)

Example 4 with JavaConstructorReference

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

the class ClassFileImporterLambdaAccessesTest method imports_constructor_reference_from_lambda_without_parameter.

@Test
public void imports_constructor_reference_from_lambda_without_parameter() {
    @SuppressWarnings("unused")
    class Caller {

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

Aggregations

JavaConstructorReference (com.tngtech.archunit.core.domain.JavaConstructorReference)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 ConstructorReferenceTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget)1 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)1 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)1 JavaMethodReference (com.tngtech.archunit.core.domain.JavaMethodReference)1 JavaConstructorReferenceBuilder (com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorReferenceBuilder)1 Supplier (java.util.function.Supplier)1