Skip to content
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

Provide invocation arguments for RepositoryMethodInvocation #2681

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

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

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

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

try {

Expand All @@ -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;
}
}
Expand All @@ -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,
Expand All @@ -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;
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
});
}
Expand Down Expand Up @@ -378,33 +378,36 @@ private static class RepositoryMethodInvocationCaptor {
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() {
Expand All @@ -424,6 +427,10 @@ public Throwable getError() {
return error;
}

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

long getDuration() {
return (endTime != null ? endTime : System.nanoTime()) - startTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down