-
Notifications
You must be signed in to change notification settings - Fork 35
Description
The following test class
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.LoaderOptions;
import com.helger.jcodemodel.JCodeModel;
import com.helger.jcodemodel.JCodeModelException;
import com.helger.jcodemodel.JDefinedClass;
import com.helger.jcodemodel.JMod;
import com.helger.jcodemodel.writer.JCMWriter;
import com.helger.jcodemodel.writer.OutputStreamCodeWriter;
import fr.lelouet.tools.compilation.inmemory.DynamicClassLoader;
public class SnakeYamlImportErrorTest {
@Test
public void testCompiling()
throws JCodeModelException, ClassNotFoundException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
JCodeModel jcm = new JCodeModel();
JDefinedClass cl = jcm._class("Test");
cl.field(JMod.PUBLIC, LoaderOptions.class, "options");
try {
DynamicClassLoader dcl = new DynamicClassLoader(SnakeYamlImportErrorTest.class.getClassLoader()).withCode(jcm);
Class<?> compiled = dcl.loadClass("Test");
compiled.getConstructor().newInstance();
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamCodeWriter acw = new OutputStreamCodeWriter(baos, Charset.defaultCharset());
new JCMWriter(jcm).build(acw);
throw new UnsupportedOperationException(baos.toString(), e);
}
}
}fails on snakeyaml dependency 2.0 or 2.3 .
compile diagnostic //Test.java:1: error: package org.yaml.snakeyaml does not exist
import org.yaml.snakeyaml.LoaderOptions;
^
compile diagnostic //Test.java:4: error: cannot find symbol
public LoaderOptions options;
^
symbol: class LoaderOptions
location: class Test
FAILED: testCompiling
java.lang.UnsupportedOperationException: import org.yaml.snakeyaml.LoaderOptions;public class Test {
public LoaderOptions options;
}at SnakeYamlImportErrorTest.testCompiling(SnakeYamlImportErrorTest.java:36)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: Test
at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:718)
at fr.lelouet.tools.compilation.inmemory.DynamicClassLoader.findClass(DynamicClassLoader.java:53)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at SnakeYamlImportErrorTest.testCompiling(SnakeYamlImportErrorTest.java:30)
... 28 more
It however passes on snakeyaml 1.33 , which has critical security issues
I checked and snakeyaml is not imported with the dependencies (besides testng). If I do a main the same problem arises.
It may be a problem with my dynamic classloader, or the way the class is available in the jar (require module ?)