Hi, I'm trying to run a test task with <this java ...
# community-support
j
Hi, I'm trying to run a test task with this java agent. The agent instruments every instanceof and checkcast, so basically every class. i added it to the test jvm with
jvmArgs("-javaagent:${configurations.typePollutionAgent.singleFile}")
, as described here. However, it leads to the following error:
Copy code
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: io/type/pollution/agent/TraceInstanceOf
        at org.slf4j.LoggerFactory.findPossibleStaticLoggerBinderPathSet(LoggerFactory.java:311)
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:146)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:417)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:362)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
        at org.gradle.process.internal.worker.messaging.WorkerConfigSerializer.<clinit>(WorkerConfigSerializer.java:46)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:81)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:66)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
Caused by: java.lang.ClassNotFoundException: io.type.pollution.agent.TraceInstanceOf
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        ... 11 more
My assessment is that GradleWorkerMain is creating a FilteringClassLoader that does not let the agent classes through from the boot classpath, which then blows up when the instrumented classes in the class loader attempt to access the agent code. I see a concept of "shared packages" which might help if I added the agent package to it, but I don't see a way to configure the shared packages. Is there a way to fix this or to just disable the class loading isolation?
c
So we had to do something similar to get our tests to stop warning about self-registering agents
Copy code
val testAgent by configurations.creating

dependencies {
    testAgent("net.bytebuddy:byte-buddy-agent:1.14.6")
}

testing {
    suites {
        val test by getting(JvmTestSuite::class) {
            useJUnitJupiter("5.10.1")
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.22")
                implementation("io.mockk:mockk:1.13.9")
            }
            targets.configureEach {
                testTask.configure {
                    jvmArgs(testAgent.files.map { "-javaagent:${it.absolutePath}" })
                }
            }
        }
    }
}
Does something like that work for you?
j
no it doesn't change anything, the classpath is still broken