Skip to content

Commit 3840b15

Browse files
authored
fix: provide context class loader when transforming classes (#1601)
### Motivation When a class is currently being transformed the system class loader is used to provide access to classes that are required to build the final class file. However, this causes issues when classes are required that are not available to the system class loader, e.g. if dynamically loaded by the server software. ### Modification Use the class loader of the class that is being transformed, as it must be able to provide all necessary classes to perform the transformation. ### Result No more issues during class transforming due to unknown classes.
1 parent b209b6a commit 3840b15

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

wrapper-jvm/impl/src/main/java/eu/cloudnetservice/wrapper/impl/transform/DefaultClassTransformerRegistry.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import eu.cloudnetservice.wrapper.transform.ClassTransformerRegistry;
2121
import jakarta.inject.Singleton;
2222
import java.lang.classfile.ClassFile;
23+
import java.lang.classfile.ClassHierarchyResolver;
2324
import java.lang.instrument.ClassFileTransformer;
2425
import java.lang.instrument.Instrumentation;
2526
import java.security.ProtectionDomain;
@@ -110,9 +111,15 @@ public byte[] transform(
110111
var transformerClassName = this.transformer.getClass().getName();
111112
LOGGER.debug("Transforming class {} using transformer {}", className, transformerClassName);
112113

114+
// use the class loader that is loading the class to transform as this loader
115+
// must also know all relevant imported classes. by default this would be
116+
// the system class loader, which might not have all necessary classes present
117+
var classHierarchyResolver = ClassHierarchyResolver.ofClassLoading(loader);
118+
var classHierarchyResolverOption = ClassFile.ClassHierarchyResolverOption.of(classHierarchyResolver);
119+
113120
try {
114121
// apply the transformation to the provided class file
115-
var classFile = ClassFile.of();
122+
var classFile = ClassFile.of(classHierarchyResolverOption);
116123
var classModel = classFile.parse(classfileBuffer);
117124
var classTransform = this.transformer.provideClassTransform();
118125
return classFile.transform(classModel, classTransform);

0 commit comments

Comments
 (0)