Skip to content

Provide invocation arguments for RepositoryMethodInvocation #2681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ public void notifyListeners(Method method, Object[] args, RepositoryMethodInvoca

/**
* {@link RepositoryInvocationMulticaster} implementation that notifies {@link RepositoryMethodInvocationListener}
* upon {@link #notifyListeners(Method, Object[], RepositoryMethodInvocationResult)}.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just fixed this broken JavaDoc which I noticed along the way, it's not directly related to this pr.

* upon {@link #notifyListeners(Method, Object[], RepositoryMethodInvocation)}.
*
* @author Mark Paluch
*/
Original file line number Diff line number Diff line change
@@ -49,20 +49,23 @@ class RepositoryMethodInvocation {
private final long durationNs;
private final Class<?> repositoryInterface;
private final Method method;
private final Object[] arguments;
private final RepositoryMethodInvocationResult result;

/**
* @param repositoryInterface the repository interface that was used to call {@link Method}.
* @param method the actual method that was called.
* @param arguments the actual arguments provided to the repository method.
* @param result the outcome of the invocation. Must not be {@literal null}.
* @param durationNs the duration in {@link TimeUnit#NANOSECONDS}.
*/
public RepositoryMethodInvocation(Class<?> repositoryInterface, Method method,
RepositoryMethodInvocationResult result, long durationNs) {
Object[] arguments, RepositoryMethodInvocationResult result, long durationNs) {

this.durationNs = durationNs;
this.repositoryInterface = repositoryInterface;
this.method = method;
this.arguments = arguments;
this.result = result;
}

@@ -81,6 +84,10 @@ public Method getMethod() {
return method;
}

public Object[] getArguments() {
return arguments;
}

@Nullable
public RepositoryMethodInvocationResult getResult() {
return result;
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ private Object doInvoke(Class<?> repositoryInterface, RepositoryInvocationMultic
throws Exception {

RepositoryMethodInvocationCaptor invocationResultCaptor = RepositoryMethodInvocationCaptor
.captureInvocationOn(repositoryInterface);
.captureInvocationOn(repositoryInterface, args);

try {

@@ -142,14 +142,14 @@ private Object doInvoke(Class<?> repositoryInterface, RepositoryInvocationMultic

if (result instanceof Stream) {
return ((Stream<?>) result).onClose(
() -> multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success())));
() -> multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success(args))));
}

multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success()));
multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success(args)));

return result;
} catch (Exception e) {
multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.error(e)));
multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.error(e, args)));
throw e;
}
}
@@ -168,7 +168,7 @@ private Object doInvokeReactiveToSuspended(Class<?> repositoryInterface, Reposit
args[args.length - 1] = null;

RepositoryMethodInvocationCaptor invocationResultCaptor = RepositoryMethodInvocationCaptor
.captureInvocationOn(repositoryInterface);
.captureInvocationOn(repositoryInterface, args);
try {

Publisher<?> result = new ReactiveInvocationListenerDecorator().decorate(repositoryInterface, multicaster, args,
@@ -184,7 +184,7 @@ private Object doInvokeReactiveToSuspended(Class<?> repositoryInterface, Reposit

return AwaitKt.awaitSingleOrNull(result, continuation);
} catch (Exception e) {
multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.error(e)));
multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.error(e, args)));
throw e;
}
}
@@ -195,8 +195,8 @@ private static Object collectToList(Object result) {
}

private RepositoryMethodInvocation computeInvocationResult(RepositoryMethodInvocationCaptor captured) {
return new RepositoryMethodInvocation(captured.getRepositoryInterface(), method, captured.getCapturedResult(),
captured.getDuration());
return new RepositoryMethodInvocation(captured.getRepositoryInterface(), method, captured.getArguments(),
captured.getCapturedResult(), captured.getDuration());
}

interface Invokable {
@@ -224,34 +224,34 @@ Publisher<Object> decorate(Class<?> repositoryInterface, RepositoryInvocationMul

if (result instanceof Mono) {
return Mono.usingWhen(
Mono.fromSupplier(() -> RepositoryMethodInvocationCaptor.captureInvocationOn(repositoryInterface)), it -> {
Mono.fromSupplier(() -> RepositoryMethodInvocationCaptor.captureInvocationOn(repositoryInterface, args)), it -> {
it.trackStart();
return ReactiveWrapperConverters.toWrapper(result, Mono.class);
}, it -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.success()));
multicaster.notifyListeners(method, args, computeInvocationResult(it.success(args)));
return Mono.empty();
}, (it, e) -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.error(e)));
multicaster.notifyListeners(method, args, computeInvocationResult(it.error(e, args)));
return Mono.empty();
}, it -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.canceled()));
multicaster.notifyListeners(method, args, computeInvocationResult(it.canceled(args)));
return Mono.empty();
});
}

return Flux.usingWhen(
Mono.fromSupplier(() -> RepositoryMethodInvocationCaptor.captureInvocationOn(repositoryInterface)), it -> {
Mono.fromSupplier(() -> RepositoryMethodInvocationCaptor.captureInvocationOn(repositoryInterface, args)), it -> {
it.trackStart();
return result instanceof Publisher ? (Publisher<?>) result
: ReactiveWrapperConverters.toWrapper(result, Publisher.class);
}, it -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.success()));
multicaster.notifyListeners(method, args, computeInvocationResult(it.success(args)));
return Mono.empty();
}, (it, e) -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.error(e)));
multicaster.notifyListeners(method, args, computeInvocationResult(it.error(e, args)));
return Mono.empty();
}, it -> {
multicaster.notifyListeners(method, args, computeInvocationResult(it.canceled()));
multicaster.notifyListeners(method, args, computeInvocationResult(it.canceled(args)));
return Mono.empty();
});
}
@@ -377,34 +377,36 @@ private static class RepositoryMethodInvocationCaptor {
private @Nullable Long endTime;
private final State state;
private final @Nullable Throwable error;
private final Object[] arguments;

protected RepositoryMethodInvocationCaptor(Class<?> repositoryInterface, long startTime, Long endTime, State state,
@Nullable Throwable exception) {
@Nullable Throwable exception, Object[] arguments) {

this.repositoryInterface = repositoryInterface;
this.startTime = startTime;
this.endTime = endTime;
this.state = state;
this.error = exception instanceof InvocationTargetException ? exception.getCause() : exception;
this.arguments = arguments;
}

public static RepositoryMethodInvocationCaptor captureInvocationOn(Class<?> repositoryInterface) {
return new RepositoryMethodInvocationCaptor(repositoryInterface, System.nanoTime(), null, State.RUNNING, null);
public static RepositoryMethodInvocationCaptor captureInvocationOn(Class<?> repositoryInterface, Object[] arguments) {
return new RepositoryMethodInvocationCaptor(repositoryInterface, System.nanoTime(), null, State.RUNNING, null, arguments);
}

public RepositoryMethodInvocationCaptor error(Throwable exception) {
public RepositoryMethodInvocationCaptor error(Throwable exception, Object[] arguments) {
return new RepositoryMethodInvocationCaptor(repositoryInterface, startTime, System.nanoTime(), State.ERROR,
exception);
exception, arguments);
}

public RepositoryMethodInvocationCaptor success() {
public RepositoryMethodInvocationCaptor success(Object[] arguments) {
return new RepositoryMethodInvocationCaptor(repositoryInterface, startTime, System.nanoTime(), State.SUCCESS,
null);
null, arguments);
}

public RepositoryMethodInvocationCaptor canceled() {
public RepositoryMethodInvocationCaptor canceled(Object[] arguments) {
return new RepositoryMethodInvocationCaptor(repositoryInterface, startTime, System.nanoTime(), State.CANCELED,
null);
null, arguments);
}

Class<?> getRepositoryInterface() {
@@ -424,6 +426,10 @@ public Throwable getError() {
return error;
}

public Object[] getArguments() {
return arguments;
}

long getDuration() {
return (endTime != null ? endTime : System.nanoTime()) - startTime;
}
Original file line number Diff line number Diff line change
@@ -197,6 +197,7 @@ void capturesImperativeSuccessCorrectly() throws Exception {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.SUCCESS);
assertThat(multicaster.first().getResult().getError()).isNull();
assertThat(multicaster.first().getArguments()).isNotNull();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt like its appropriate to check all state case results should have arguments present, any thoughts on this? I could make this a more explicit check if you want.

}

@Test // DATACMNS-1764
@@ -208,6 +209,7 @@ void capturesReactiveCompletionCorrectly() throws Exception {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.SUCCESS);
assertThat(multicaster.first().getResult().getError()).isNull();
assertThat(multicaster.first().getArguments()).isNotNull();
}

@Test // DATACMNS-1764
@@ -218,6 +220,7 @@ void capturesImperativeErrorCorrectly() {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.ERROR);
assertThat(multicaster.first().getResult().getError()).isInstanceOf(IllegalStateException.class);
assertThat(multicaster.first().getArguments()).isNotNull();
}

@Test // DATACMNS-1764
@@ -231,6 +234,7 @@ void capturesReactiveErrorCorrectly() throws Exception {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.ERROR);
assertThat(multicaster.first().getResult().getError()).isInstanceOf(IllegalStateException.class);
assertThat(multicaster.first().getArguments()).isNotNull();
}

@Test // DATACMNS-1764
@@ -242,6 +246,31 @@ void capturesReactiveCancellationCorrectly() throws Exception {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.CANCELED);
assertThat(multicaster.first().getResult().getError()).isNull();
assertThat(multicaster.first().getArguments()).isNotNull();
}

@Test
void capturesImperativeArgumentsCorrectly() throws Exception {
String id = "id";
String name = "name";

repositoryMethodInvoker("findByMultipleParameters").invoke(id, name);

assertThat(multicaster.first().getArguments()).isNotNull();
assertThat(multicaster.first().getArguments()).containsExactly(id, name);
}

@Test
void capturesReactiveArgumentsCorrectly() throws Exception {
String id = "id";
String name = "name";

when(query.execute(any())).thenReturn(Mono.just(new TestDummy()));

repositoryMethodInvokerForReactive("findByMultipleParameters").<Mono<TestDummy>> invoke(id, name).subscribe();

assertThat(multicaster.first().getArguments()).isNotNull();
assertThat(multicaster.first().getArguments()).containsExactly(id, name);
}

@Test // DATACMNS-1764
@@ -273,6 +302,7 @@ public void resumeWith(@NotNull Object o) {

assertThat(multicaster.first().getResult().getState()).isEqualTo(State.SUCCESS);
assertThat(multicaster.first().getResult().getError()).isNull();
assertThat(multicaster.first().getArguments()).isNotNull();
}

RepositoryMethodInvokerStub repositoryMethodInvoker(String methodName) {
@@ -398,11 +428,15 @@ interface DummyRepository extends CrudRepository<TestDummy, String> {
TestDummy findByName(String name);

Stream<TestDummy> streamAll();

TestDummy findByMultipleParameters(String id, String name);
}

interface ReactiveDummyRepository extends ReactiveCrudRepository<TestDummy, String> {

Mono<TestDummy> findByName(String name);

Mono<TestDummy> findByMultipleParameters(String id, String name);
}

@ToString