Util simplification
MODIFY
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java
@@ -9,29 +9,15 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader.BootstrapClassLoaderProxy;
-import java.lang.reflect.Method;
import java.net.URL;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDefinition;
public class Utils {
- // This is used in HelperInjectionTest.groovy
- private static Method findLoadedClassMethod = null;
-
private static final BootstrapClassLoaderProxy unitTestBootstrapProxy =
new BootstrapClassLoaderProxy(new URL[0]);
- static {
- try {
- findLoadedClassMethod = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
- } catch (NoSuchMethodException | SecurityException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private static final String CLASS_SUFFIX = ".class";
-
/** Return the classloader the core agent is running on. */
public static ClassLoader getAgentClassLoader() {
return AgentInstaller.class.getClassLoader();
@@ -48,27 +34,17 @@ public class Utils {
/** com.foo.Bar to com/foo/Bar.class */
public static String getResourceName(String className) {
- if (className.endsWith(CLASS_SUFFIX)) {
- return className;
- }
- return className.replace('.', '/') + CLASS_SUFFIX;
+ return className.replace('.', '/') + ".class";
}
- /** com/foo/Bar.class to com.foo.Bar */
- public static String getClassName(String resourceName) {
- return stripDotClassSuffix(resourceName).replace('/', '.');
+ /** com/foo/Bar to com.foo.Bar */
+ public static String getClassName(String internalName) {
+ return internalName.replace('/', '.');
}
/** com.foo.Bar to com/foo/Bar */
- public static String getInternalName(String resourceName) {
- return stripDotClassSuffix(resourceName).replace('.', '/');
- }
-
- private static String stripDotClassSuffix(String resourceName) {
- if (resourceName.endsWith(CLASS_SUFFIX)) {
- return resourceName.substring(0, resourceName.length() - CLASS_SUFFIX.length());
- }
- return resourceName;
+ public static String getInternalName(Class<?> clazz) {
+ return clazz.getName().replace('.', '/');
}
/**

MODIFY
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/context/FieldBackedProvider.java
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/context/FieldBackedProvider.java
@@ -87,7 +87,7 @@ public class FieldBackedProvider implements InstrumentationContextProvider {
"io.opentelemetry.javaagent.bootstrap.instrumentation.context.";
private static final String INJECTED_FIELDS_MARKER_CLASS_NAME =
- Utils.getInternalName(FieldBackedContextStoreAppliedMarker.class.getName());
+ Utils.getInternalName(FieldBackedContextStoreAppliedMarker.class);
private static final Method CONTEXT_GET_METHOD;
private static final Method GET_CONTEXT_STORE_METHOD;
@@ -194,8 +194,7 @@ public class FieldBackedProvider implements InstrumentationContextProvider {
public void visitMethodInsn(
int opcode, String owner, String name, String descriptor, boolean isInterface) {
pushOpcode(opcode);
- if (Utils.getInternalName(CONTEXT_GET_METHOD.getDeclaringClass().getName())
- .equals(owner)
+ if (Utils.getInternalName(CONTEXT_GET_METHOD.getDeclaringClass()).equals(owner)
&& CONTEXT_GET_METHOD.getName().equals(name)
&& Type.getMethodDescriptor(CONTEXT_GET_METHOD).equals(descriptor)) {
log.trace("Found context-store access in {}", instrumenterClass.getName());

MODIFY
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/muzzle/collector/MuzzleCodeGenerator.java
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/muzzle/collector/MuzzleCodeGenerator.java
@@ -514,7 +514,7 @@ class MuzzleCodeGenerator implements AsmVisitorWrapper {
// drops "$1" suffix for enum constants that override/implement super class methods
private String getEnumClassInternalName(Reference.Flag flag) {
- String fullInternalName = Utils.getInternalName(flag.getClass().getName());
+ String fullInternalName = Utils.getInternalName(flag.getClass());
Matcher m = ANONYMOUS_ENUM_CONSTANT_CLASS.matcher(fullInternalName);
return m.matches() ? m.group("enumClass") : fullInternalName;
}

MODIFY
javaagent-tooling/src/test/groovy/io/opentelemetry/javaagent/tooling/UtilsTest.groovy
javaagent-tooling/src/test/groovy/io/opentelemetry/javaagent/tooling/UtilsTest.groovy
@@ -18,18 +18,25 @@ class UtilsTest extends Specification {
stackTrace.contains(System.getProperty("line.separator"))
}
- def "getClassName() removes suffix and slashes to dots"(){
+ def "getResourceName() adds suffix and converts dots to slashes"() {
setup:
- def result = Utils.getClassName("com/example/Something.class")
+ def result = Utils.getResourceName("com.example.Something")
+ expect:
+ result == "com/example/Something.class"
+ }
+
+ def "getClassName() converts slashes to dots"() {
+ setup:
+ def result = Utils.getClassName("com/example/Something")
expect:
result == "com.example.Something"
}
- def "getInternalName() removes suffix and slashes to dots"(){
+ def "getInternalName() converts slashes to dots"() {
setup:
- def result = Utils.getInternalName("com.example.Something.class")
+ def result = Utils.getInternalName(UtilsTest)
expect:
- result == "com/example/Something"
+ result == "io/opentelemetry/javaagent/tooling/UtilsTest"
}
def "convertToInnerClassName() makes dollar into dots"() {

Util simplification (#2394)