Search in sources :

Example 1 with ConstructorCallTarget

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

the class AccessTargetNewerJavaVersionTest method function_resolve_member.

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

        String access() {
            Data_function_resolve_member.Target target = new Data_function_resolve_member.Target();
            Supplier<Data_function_resolve_member.Target> supplier = Data_function_resolve_member.Target::new;
            Consumer<Data_function_resolve_member.Target> consumer = Data_function_resolve_member.Target::method;
            target.method();
            return target.field;
        }
    }
    JavaClass targetClass = new ClassFileImporter().importClasses(Origin.class, Data_function_resolve_member.Target.class).get(Data_function_resolve_member.Target.class);
    MethodCallTarget methodCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodCallTarget.class);
    assertThat(AccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(AccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    assertThat(CodeUnitAccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(CodeUnitAccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    assertThat(MethodCallTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(MethodCallTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    MethodReferenceTarget methodReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodReferenceTarget.class);
    assertThat(MethodReferenceTarget.Functions.RESOLVE_MEMBER.apply(methodReferenceTarget)).contains(methodReferenceTarget.resolveMember().get());
    ConstructorCallTarget constructorCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorCallTarget.class);
    assertThat(ConstructorCallTarget.Functions.RESOLVE_MEMBER.apply(constructorCallTarget)).contains(constructorCallTarget.resolveMember().get());
    assertThat(ConstructorCallTarget.Functions.RESOLVE.apply(constructorCallTarget)).isEqualTo(ImmutableSet.of(constructorCallTarget.resolveMember().get()));
    ConstructorReferenceTarget constructorReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorReferenceTarget.class);
    assertThat(ConstructorReferenceTarget.Functions.RESOLVE_MEMBER.apply(constructorReferenceTarget)).contains(constructorReferenceTarget.resolveMember().get());
    FieldAccessTarget fieldAccessTarget = findTargetWithType(targetClass.getAccessesToSelf(), FieldAccessTarget.class);
    assertThat(FieldAccessTarget.Functions.RESOLVE_MEMBER.apply(fieldAccessTarget)).contains(fieldAccessTarget.resolveMember().get());
    assertThat(FieldAccessTarget.Functions.RESOLVE.apply(fieldAccessTarget)).isEqualTo(ImmutableSet.of(fieldAccessTarget.resolveMember().get()));
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) ConstructorReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) CodeUnitAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.CodeUnitAccessTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) MethodReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ConstructorReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget) MethodReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) Test(org.junit.Test)

Example 2 with ConstructorCallTarget

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

the class ClassFileImporterAccessesTest method imports_constructor_calls_on_external_class.

@Test
public void imports_constructor_calls_on_external_class() throws Exception {
    JavaClass classThatCallsOwnConstructor = new ClassFileImporter().importUrl(getClass().getResource("testexamples/callimport")).get(CallsOwnConstructor.class);
    JavaCodeUnit constructorCallingObjectInit = classThatCallsOwnConstructor.getConstructor(String.class);
    JavaConstructorCall objectInitCall = getOnlyElement(constructorCallingObjectInit.getConstructorCallsFromSelf());
    assertThatCall(objectInitCall).isFrom(constructorCallingObjectInit).inLineNumber(4);
    ConstructorCallTarget target = objectInitCall.getTarget();
    assertThat(target.getFullName()).isEqualTo(Object.class.getName() + ".<init>()");
    assertThat(reflect(target)).isEqualTo(Object.class.getConstructor());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall) Test(org.junit.Test)

Example 3 with ConstructorCallTarget

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

the class ClassFileImporterAccessesTest method assertConstructorCall.

private void assertConstructorCall(JavaCodeUnit call, Class<?> constructorOwner, int lineNumber) {
    JavaConstructorCall callToExternalClass = getOnlyElement(getByTargetOwner(call.getConstructorCallsFromSelf(), constructorOwner));
    assertThatCall(callToExternalClass).isFrom(call).inLineNumber(lineNumber);
    ConstructorCallTarget target = callToExternalClass.getTarget();
    assertThat(target.getFullName()).isEqualTo(constructorOwner.getName() + ".<init>()");
    assertThat(reflect(target)).isEqualTo(constructor(constructorOwner));
}
Also used : ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall)

Example 4 with ConstructorCallTarget

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

the class AccessTargetTest method function_resolve_member.

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

        String access() {
            Data_function_resolve_member.Target target = new Data_function_resolve_member.Target();
            Supplier<Data_function_resolve_member.Target> supplier = Data_function_resolve_member.Target::new;
            Consumer<Data_function_resolve_member.Target> consumer = Data_function_resolve_member.Target::method;
            target.method();
            return target.field;
        }
    }
    JavaClass targetClass = new ClassFileImporter().importClasses(Origin.class, Data_function_resolve_member.Target.class).get(Data_function_resolve_member.Target.class);
    MethodCallTarget methodCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodCallTarget.class);
    assertThat(AccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(AccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    assertThat(AccessTarget.CodeUnitAccessTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(AccessTarget.CodeUnitAccessTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    assertThat(MethodCallTarget.Functions.RESOLVE_MEMBER.apply(methodCallTarget)).contains(methodCallTarget.resolveMember().get());
    assertThat(MethodCallTarget.Functions.RESOLVE.apply(methodCallTarget)).isEqualTo(ImmutableSet.of(methodCallTarget.resolveMember().get()));
    MethodReferenceTarget methodReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), MethodReferenceTarget.class);
    assertThat(MethodReferenceTarget.Functions.RESOLVE_MEMBER.apply(methodReferenceTarget)).contains(methodReferenceTarget.resolveMember().get());
    ConstructorCallTarget constructorCallTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorCallTarget.class);
    assertThat(ConstructorCallTarget.Functions.RESOLVE_MEMBER.apply(constructorCallTarget)).contains(constructorCallTarget.resolveMember().get());
    assertThat(ConstructorCallTarget.Functions.RESOLVE.apply(constructorCallTarget)).isEqualTo(ImmutableSet.of(constructorCallTarget.resolveMember().get()));
    ConstructorReferenceTarget constructorReferenceTarget = findTargetWithType(targetClass.getAccessesToSelf(), ConstructorReferenceTarget.class);
    assertThat(ConstructorReferenceTarget.Functions.RESOLVE_MEMBER.apply(constructorReferenceTarget)).contains(constructorReferenceTarget.resolveMember().get());
    FieldAccessTarget fieldAccessTarget = findTargetWithType(targetClass.getAccessesToSelf(), FieldAccessTarget.class);
    assertThat(FieldAccessTarget.Functions.RESOLVE_MEMBER.apply(fieldAccessTarget)).contains(fieldAccessTarget.resolveMember().get());
    assertThat(FieldAccessTarget.Functions.RESOLVE.apply(fieldAccessTarget)).isEqualTo(ImmutableSet.of(fieldAccessTarget.resolveMember().get()));
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) ConstructorReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) ConstructorReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget) CodeUnitCallTarget(com.tngtech.archunit.core.domain.AccessTarget.CodeUnitCallTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) MethodReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget) FieldAccessTarget(com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget) MethodReferenceTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) Test(org.junit.Test)

Example 5 with ConstructorCallTarget

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

the class ClassGraphCreator method createConstructorCallsFor.

@Override
public Set<JavaConstructorCall> createConstructorCallsFor(JavaCodeUnit codeUnit, Set<TryCatchBlockBuilder> tryCatchBlockBuilders) {
    ImmutableSet.Builder<JavaConstructorCall> result = ImmutableSet.builder();
    for (AccessRecord<ConstructorCallTarget> record : processedConstructorCallRecords.get(codeUnit)) {
        JavaConstructorCall call = accessBuilderFrom(new JavaConstructorCallBuilder(), record).build();
        result.add(call);
        handlePossibleTryBlockAccess(tryCatchBlockBuilders, record, call);
    }
    return result.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) JavaConstructorCallBuilder(com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorCallBuilder) ConstructorCallTarget(com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget) JavaConstructorCall(com.tngtech.archunit.core.domain.JavaConstructorCall)

Aggregations

ConstructorCallTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget)5 JavaConstructorCall (com.tngtech.archunit.core.domain.JavaConstructorCall)3 Test (org.junit.Test)3 ConstructorReferenceTarget (com.tngtech.archunit.core.domain.AccessTarget.ConstructorReferenceTarget)2 FieldAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.FieldAccessTarget)2 MethodCallTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget)2 MethodReferenceTarget (com.tngtech.archunit.core.domain.AccessTarget.MethodReferenceTarget)2 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 CodeUnitAccessTarget (com.tngtech.archunit.core.domain.AccessTarget.CodeUnitAccessTarget)1 CodeUnitCallTarget (com.tngtech.archunit.core.domain.AccessTarget.CodeUnitCallTarget)1 JavaClass (com.tngtech.archunit.core.domain.JavaClass)1 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)1 JavaConstructorCallBuilder (com.tngtech.archunit.core.importer.DomainBuilders.JavaConstructorCallBuilder)1