From 8beb134910a931ef9e0decce21121b007af9a691 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Thu, 28 Apr 2022 18:37:09 +0300 Subject: [PATCH] Load agent classes child first (#5950) --- .../javaagent/bootstrap/AgentClassLoader.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java index f2cccb8f6e0b..8c654def4497 100644 --- a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java +++ b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java @@ -138,11 +138,25 @@ public Class loadClass(String name, boolean resolve) throws ClassNotFoundExce throw new ClassNotFoundException(name); } - return super.loadClass(name, resolve); + synchronized (getClassLoadingLock(name)) { + Class clazz = findLoadedClass(name); + // first search agent classes + if (clazz == null) { + clazz = findAgentClass(name); + } + // search from parent and urls added to this loader + if (clazz == null) { + clazz = super.loadClass(name, false); + } + if (resolve) { + resolveClass(clazz); + } + + return clazz; + } } - @Override - protected Class findClass(String name) throws ClassNotFoundException { + private Class findAgentClass(String name) throws ClassNotFoundException { JarEntry jarEntry = findJarEntry(name.replace('.', '/') + ".class"); if (jarEntry != null) { byte[] bytes; @@ -156,8 +170,7 @@ protected Class findClass(String name) throws ClassNotFoundException { return defineClass(name, bytes); } - // find class from agent initializer jar - return super.findClass(name); + return null; } public Class defineClass(String name, byte[] bytes) { @@ -281,7 +294,7 @@ private URL getJarEntryUrl(JarEntry jarEntry) { public Enumeration findResources(String name) throws IOException { // find resources from agent initializer jar Enumeration delegate = super.findResources(name); - // agent jar can have only once resource for given name + // agent jar can have only one resource for given name URL url = findJarResource(name); if (url != null) { return new Enumeration() {