File tree Expand file tree Collapse file tree 3 files changed +48
-10
lines changed
main/java/io/opentelemetry/javaagent/bootstrap
test/java/io/opentelemetry/javaagent/bootstrap Expand file tree Collapse file tree 3 files changed +48
-10
lines changed Original file line number Diff line number Diff line change 55
66package io .opentelemetry .javaagent .bootstrap ;
77
8- /**
9- * Interface added to indy proxies to allow unwrapping the proxy object
10- */
8+ /** Interface added to indy proxies to allow unwrapping the proxy object */
119public interface IndyProxy {
1210
1311 /**
Original file line number Diff line number Diff line change @@ -15,21 +15,21 @@ private IndyProxyHelper() {}
1515 * @param <T> type of object to return
1616 * @param o object to unwrap
1717 * @param type expected object type
18- * @return unwrapped proxy instance or the original object (if not a proxy) cast to the expected type
19- * @throws IllegalArgumentException if the provided object the proxied object can't be cast to the expected type
20- *
18+ * @return unwrapped proxy instance or the original object (if not a proxy) cast to the expected
19+ * type
20+ * @throws IllegalArgumentException if the provided object the proxied object can't be cast to the
21+ * expected type
2122 */
2223 public static <T > T unwrapIfNeeded (Object o , Class <T > type ) {
23- if (type .isAssignableFrom (o .getClass ())) {
24- return type .cast (o );
25- }
26-
2724 if (o instanceof IndyProxy ) {
2825 Object delegate = ((IndyProxy ) o ).__getIndyProxyDelegate ();
2926 if (type .isAssignableFrom (delegate .getClass ())) {
3027 return type .cast (delegate );
3128 }
3229 }
30+ if (type .isAssignableFrom (o .getClass ())) {
31+ return type .cast (o );
32+ }
3333
3434 throw new IllegalArgumentException ("unexpected object type" );
3535 }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+
6+ package io .opentelemetry .javaagent .bootstrap ;
7+
8+ import static org .assertj .core .api .Assertions .assertThat ;
9+ import static org .junit .jupiter .api .Assertions .assertThrows ;
10+
11+ import org .junit .jupiter .api .Test ;
12+
13+ class IndyProxyHelperTest {
14+
15+ @ Test
16+ void wrongType () {
17+ assertThrows (
18+ IllegalArgumentException .class ,
19+ () -> IndyProxyHelper .unwrapIfNeeded ("" , Integer .class ));
20+ assertThrows (
21+ IllegalArgumentException .class ,
22+ () -> IndyProxyHelper .unwrapIfNeeded (proxy ("" ), Integer .class ));
23+ }
24+
25+ @ Test
26+ void unwrap () {
27+
28+ // no wrapping
29+ Number number = IndyProxyHelper .unwrapIfNeeded (42 , Number .class );
30+ assertThat (number ).isEqualTo (42 );
31+
32+ // unwrap needed
33+ String string = IndyProxyHelper .unwrapIfNeeded (proxy ("hello" ), String .class );
34+ assertThat (string ).isEqualTo ("hello" );
35+ }
36+
37+ private static IndyProxy proxy (Object delegate ) {
38+ return () -> delegate ;
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments