Skip to content

Commit dc6175e

Browse files
committed
Fix #393: Loosen InvocationEventHanndler context bound to object
1 parent 508770d commit dc6175e

File tree

11 files changed

+150
-189
lines changed

11 files changed

+150
-189
lines changed

tritium-api/src/main/java/com/palantir/tritium/event/InvocationEventHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @see java.lang.reflect.InvocationHandler
2727
* @param <C> invocation context
2828
*/
29-
public interface InvocationEventHandler<C extends InvocationContext> {
29+
public interface InvocationEventHandler<C> {
3030

3131
/**
3232
* Returns true if this event handler instance is enabled, otherwise false.
@@ -57,7 +57,7 @@ public interface InvocationEventHandler<C extends InvocationContext> {
5757
* @param context the current invocation context.
5858
* @param result the return value from invocation, or null if {@link Void}.
5959
*/
60-
void onSuccess(@Nullable InvocationContext context, @Nullable Object result);
60+
void onSuccess(@Nullable C context, @Nullable Object result);
6161

6262
/**
6363
* Invoked when an invocation fails.
@@ -69,6 +69,6 @@ public interface InvocationEventHandler<C extends InvocationContext> {
6969
* @param context the current invocation context.
7070
* @param cause the throwable which caused the failure.
7171
*/
72-
void onFailure(@Nullable InvocationContext context, @Nonnull Throwable cause);
72+
void onFailure(@Nullable C context, @Nonnull Throwable cause);
7373

7474
}

tritium-core/src/main/java/com/palantir/tritium/event/AbstractInvocationEventHandler.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
*
2929
* @param <C> invocation context
3030
*/
31-
public abstract class AbstractInvocationEventHandler<C extends InvocationContext>
32-
implements InvocationEventHandler<C> {
31+
public abstract class AbstractInvocationEventHandler<C> implements InvocationEventHandler<C> {
3332

3433
private static final Logger logger = LoggerFactory.getLogger(AbstractInvocationEventHandler.class);
3534

@@ -78,7 +77,7 @@ public final boolean isEnabled() {
7877
*
7978
* @param context invocation context
8079
*/
81-
protected final void debugIfNullContext(@Nullable InvocationContext context) {
80+
protected final void debugIfNullContext(@Nullable C context) {
8281
if (context == null) {
8382
logger.debug(
8483
"{} encountered null metric context, likely due to exception in preInvocation",
@@ -98,7 +97,7 @@ private static SafeArg<String> safeClassName(@Nullable Object obj) {
9897
* @return false if "instrument.fully.qualified.class.Name" is set to "false", otherwise true
9998
*/
10099
protected static com.palantir.tritium.api.functions.BooleanSupplier getSystemPropertySupplier(
101-
Class<? extends InvocationEventHandler<InvocationContext>> clazz) {
100+
Class<? extends InvocationEventHandler<?>> clazz) {
102101
checkNotNull(clazz, "clazz");
103102
return InstrumentationProperties.getSystemPropertySupplier(clazz.getName());
104103
}

tritium-core/src/main/java/com/palantir/tritium/event/CompositeInvocationEventHandler.java

+30-48
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,20 @@
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030

31-
public final class CompositeInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {
31+
public final class CompositeInvocationEventHandler extends AbstractInvocationEventHandler<Object[]> {
3232

3333
private static final Logger logger = LoggerFactory.getLogger(CompositeInvocationEventHandler.class);
3434

35-
private final InvocationEventHandler<InvocationContext>[] handlers;
35+
private final InvocationEventHandler<?>[] handlers;
3636

37-
@SuppressWarnings("unchecked")
38-
private CompositeInvocationEventHandler(List<InvocationEventHandler<InvocationContext>> handlers) {
37+
private CompositeInvocationEventHandler(List<InvocationEventHandler<?>> handlers) {
3938
this.handlers = checkNotNull(handlers, "handlers").toArray(new InvocationEventHandler[0]);
40-
for (InvocationEventHandler<InvocationContext> handler : handlers) {
39+
for (InvocationEventHandler<?> handler : handlers) {
4140
checkNotNull(handler, "Null handlers are not allowed");
4241
}
4342
}
4443

45-
public static InvocationEventHandler<InvocationContext> of(
46-
List<InvocationEventHandler<InvocationContext>> handlers) {
44+
public static InvocationEventHandler<?> of(List<InvocationEventHandler<?>> handlers) {
4745
if (handlers.isEmpty()) {
4846
return NoOpInvocationEventHandler.INSTANCE;
4947
} else if (handlers.size() == 1) {
@@ -54,56 +52,58 @@ public static InvocationEventHandler<InvocationContext> of(
5452
}
5553

5654
@Nullable
57-
private InvocationEventHandler<InvocationContext> tryGetEnabledHandler(int index) {
58-
InvocationEventHandler<InvocationContext> handler = handlers[index];
55+
private InvocationEventHandler<?> tryGetEnabledHandler(int index) {
56+
InvocationEventHandler<?> handler = handlers[index];
5957
if (handler.isEnabled()) {
6058
return handler;
6159
}
6260
return null;
6361
}
6462

6563
@Override
66-
public InvocationContext preInvocation(@Nonnull Object instance, @Nonnull Method method, @Nonnull Object[] args) {
67-
InvocationContext[] contexts = new InvocationContext[handlers.length];
64+
public Object[] preInvocation(@Nonnull Object instance, @Nonnull Method method, @Nonnull Object[] args) {
65+
Object[] contexts = new Object[handlers.length];
6866

6967
for (int i = 0; i < handlers.length; i++) {
7068
contexts[i] = handlePreInvocation(tryGetEnabledHandler(i), instance, method, args);
7169
}
7270

73-
return new CompositeInvocationContext(instance, method, args, contexts);
71+
return contexts;
7472
}
7573

7674
@Override
77-
public void onSuccess(@Nullable InvocationContext context, @Nullable Object result) {
75+
public void onSuccess(@Nullable Object[] context, @Nullable Object result) {
7876
debugIfNullContext(context);
7977
if (context != null) {
80-
success(((CompositeInvocationContext) context).getContexts(), result);
78+
success(context, result);
8179
}
8280
}
8381

84-
private void success(@Nonnull InvocationContext[] contexts, @Nullable Object result) {
82+
@SuppressWarnings("unchecked")
83+
private void success(@Nonnull Object[] contexts, @Nullable Object result) {
8584
for (int i = contexts.length - 1; i > -1; i--) {
86-
handleSuccess(handlers[i], contexts[i], result);
85+
handleSuccess((InvocationEventHandler<Object>) handlers[i], contexts[i], result);
8786
}
8887
}
8988

9089
@Override
91-
public void onFailure(@Nullable InvocationContext context, @Nonnull Throwable cause) {
90+
public void onFailure(@Nullable Object[] context, @Nonnull Throwable cause) {
9291
debugIfNullContext(context);
9392
if (context != null) {
94-
failure(((CompositeInvocationContext) context).getContexts(), cause);
93+
failure(context, cause);
9594
}
9695
}
9796

98-
private void failure(InvocationContext[] contexts, @Nonnull Throwable cause) {
97+
@SuppressWarnings("unchecked")
98+
private void failure(Object[] contexts, @Nonnull Throwable cause) {
9999
for (int i = contexts.length - 1; i > -1; i--) {
100-
handleFailure(handlers[i], contexts[i], cause);
100+
handleFailure((InvocationEventHandler<Object>) handlers[i], contexts[i], cause);
101101
}
102102
}
103103

104104
@Nullable
105-
private static InvocationContext handlePreInvocation(
106-
@Nullable InvocationEventHandler<? extends InvocationContext> handler,
105+
private static Object handlePreInvocation(
106+
@Nullable InvocationEventHandler<?> handler,
107107
Object instance,
108108
Method method,
109109
Object[] args) {
@@ -123,7 +123,7 @@ public String toString() {
123123
}
124124

125125
private static void preInvocationFailed(
126-
@Nullable InvocationEventHandler<? extends InvocationContext> handler,
126+
@Nullable InvocationEventHandler<?> handler,
127127
@Nullable Object instance,
128128
Method method,
129129
@Nullable Exception exception) {
@@ -136,9 +136,9 @@ private static void preInvocationFailed(
136136
exception);
137137
}
138138

139-
private static void handleSuccess(
140-
InvocationEventHandler<?> handler,
141-
@Nullable InvocationContext context,
139+
private static <T> void handleSuccess(
140+
InvocationEventHandler<T> handler,
141+
@Nullable T context,
142142
@Nullable Object result) {
143143
if (context != null) {
144144
try {
@@ -149,9 +149,9 @@ private static void handleSuccess(
149149
}
150150
}
151151

152-
private static void handleFailure(
153-
InvocationEventHandler<?> handler,
154-
@Nullable InvocationContext context,
152+
private static <T> void handleFailure(
153+
InvocationEventHandler<T> handler,
154+
@Nullable T context,
155155
Throwable cause) {
156156
if (context != null) {
157157
try {
@@ -164,7 +164,7 @@ private static void handleFailure(
164164

165165
private static void eventFailed(
166166
String event,
167-
@Nullable InvocationContext context,
167+
@Nullable Object context,
168168
@Nullable Object result,
169169
RuntimeException exception) {
170170
logger.warn(
@@ -174,22 +174,4 @@ private static void eventFailed(
174174
UnsafeArg.of("result", result),
175175
exception);
176176
}
177-
178-
static class CompositeInvocationContext extends DefaultInvocationContext {
179-
180-
private final InvocationContext[] contexts;
181-
182-
CompositeInvocationContext(
183-
Object instance,
184-
Method method,
185-
@Nullable Object[] args,
186-
InvocationContext[] contexts) {
187-
super(System.nanoTime(), instance, method, args);
188-
this.contexts = checkNotNull(contexts);
189-
}
190-
191-
InvocationContext[] getContexts() {
192-
return contexts;
193-
}
194-
}
195177
}

0 commit comments

Comments
 (0)