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);
}
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));
}
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();
}
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");
}
Aggregations