-
Notifications
You must be signed in to change notification settings - Fork 879
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
Showing
4 changed files
with
116 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
...test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/ProxyStatementFactory.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,36 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Proxy; | ||
import java.sql.Statement; | ||
|
||
public class ProxyStatementFactory { | ||
|
||
public static Statement proxyStatement(Statement statement) throws Exception { | ||
TestClassLoader classLoader = new TestClassLoader(ProxyStatementFactory.class.getClassLoader()); | ||
Class<?> testInterface = classLoader.loadClass(TestInterface.class.getName()); | ||
if (testInterface.getClassLoader() != classLoader) { | ||
throw new IllegalStateException("wrong class loader"); | ||
} | ||
InvocationHandler invocationHandler = (proxy, method, args) -> method.invoke(statement, args); | ||
Statement proxyStatement = | ||
(Statement) | ||
Proxy.newProxyInstance( | ||
classLoader, new Class<?>[] {Statement.class, testInterface}, invocationHandler); | ||
// adding package private interface TestInterface to jdk proxy forces defining the proxy class | ||
// in the same package as the package private interface | ||
if (!proxyStatement | ||
.getClass() | ||
.getName() | ||
.startsWith("io.opentelemetry.javaagent.instrumentation.jdbc.test")) { | ||
throw new IllegalStateException("proxy statement is in wrong package"); | ||
} | ||
|
||
return proxyStatement; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...t/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/TestClassLoader.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,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
public class TestClassLoader extends URLClassLoader { | ||
|
||
public TestClassLoader(ClassLoader parent) { | ||
super( | ||
new URL[] {TestClassLoader.class.getProtectionDomain().getCodeSource().getLocation()}, | ||
parent); | ||
} | ||
|
||
@Override | ||
protected synchronized Class<?> loadClass(String name, boolean resolve) | ||
throws ClassNotFoundException { | ||
Class<?> clazz = findLoadedClass(name); | ||
if (clazz != null) { | ||
return clazz; | ||
} | ||
if (name.startsWith("io.opentelemetry.javaagent.instrumentation.jdbc.test")) { | ||
try { | ||
return findClass(name); | ||
} catch (ClassNotFoundException exception) { | ||
// ignore | ||
} | ||
} | ||
return super.loadClass(name, resolve); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ent/src/test/java/io/opentelemetry/javaagent/instrumentation/jdbc/test/TestInterface.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,12 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
||
// Adding a package private interface to jdk proxy forces defining the proxy class in the package | ||
// of the package private class. Usually proxy classes are defined in a package that we exclude from | ||
// instrumentation. We use this class to force proxy into a different package so it would get | ||
// instrumented. | ||
interface TestInterface {} |