-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInstrumentationProxyHelper.java
More file actions
36 lines (31 loc) · 1.08 KB
/
InstrumentationProxyHelper.java
File metadata and controls
36 lines (31 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.bootstrap;
public class InstrumentationProxyHelper {
private InstrumentationProxyHelper() {}
/**
* Unwraps and casts an indy proxy, or just casts if it's not an indy proxy.
*
* @param <T> type of object to return
* @param o object to unwrap
* @param type expected object type
* @return unwrapped proxy instance or the original object (if not a proxy) cast to the expected
* type
* @throws IllegalArgumentException if the provided object the proxied object can't be cast to the
* expected type
*/
public static <T> T unwrapIfNeeded(Object o, Class<T> type) {
if (o instanceof InstrumentationProxy) {
Object delegate = ((InstrumentationProxy) o).__getIndyProxyDelegate();
if (type.isAssignableFrom(delegate.getClass())) {
return type.cast(delegate);
}
}
if (type.isAssignableFrom(o.getClass())) {
return type.cast(o);
}
throw new IllegalArgumentException("unexpected object type");
}
}