-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54a9f00
commit a7f57cf
Showing
5 changed files
with
87 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/IndyProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** Interface added to proxy classes injected for indy instrumentation */ | ||
@SuppressWarnings("InterfaceWithOnlyStatics") // TODO: remove this once we have a method | ||
public interface IndyProxy { | ||
|
||
/** | ||
* Unwraps the proxied object | ||
* | ||
* @return unwrapped object | ||
*/ | ||
default Object unwrap() { | ||
try { | ||
// current implementation based on introspection + public delegate field | ||
Field delegate = this.getClass().getField("delegate"); | ||
return delegate.get(this); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Transparently unwraps an object that might have been proxied for indy instrumentation. When | ||
* unwrapping is not needed, for example for inlined advices, the original object is returned | ||
* effectively making this equivalent to a type cast. | ||
* | ||
* @param o object that might need unwrapping | ||
* @param type expected unwrapped object type | ||
* @param <T> type of the proxied object | ||
* @return unwrapped object | ||
* @throws IllegalArgumentException when object can't be cast or unwrapped to the desired type | ||
*/ | ||
static <T> T unwrapIfNeeded(Object o, Class<T> type) { | ||
if (type.isAssignableFrom(o.getClass())) { | ||
return type.cast(o); | ||
} | ||
if (o instanceof IndyProxy) { | ||
Object unwrapped = ((IndyProxy) o).unwrap(); | ||
if (type.isAssignableFrom(unwrapped.getClass())) { | ||
return type.cast(unwrapped); | ||
} | ||
} | ||
throw new IllegalArgumentException("unexpected object unwrap"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters