我有一个动态创建的字符串格式的类:
private String demoClassString = "import java.io.Serializable;\n" +
"public class DemoClass implements Serializable{\n" +
"\tpublic String getResult(Integer a){\n" +
"\t\treturn \"Result is \"+a;\n" +
"\t}\n" +
"\n" +
"}";
我只想编译一次,然后将其编译后的类对象保存在持久性存储中(我用文件对其进行了测试,但最终将存储在DB中)
测试方案如下:
@Test
public void testSaveFormulaWithJanino() throws Exception {
final String className = "DemoClass";
SimpleCompiler sc = new SimpleCompiler();
sc.cook(demoClassString);
Class<?> demoClass = sc.getClassLoader().loadClass(className);
testDemoClass(demoClass, "getResult");
byte[] serialized = SerializationUtils.serialize(demoClass);
logger.info("Serialized with SerializationUtils size = "+serialized.length);
Object deserializedClass = SerializationUtils.deserialize(serialized);
testDemoClass((Class<?>)deserializedClass, "getResult");
saveObject(demoClass);
}
testdemoClass
方法以反射方式调用动态创建的对象的getResult
方法:
private void testDemoClass(Class<?> demoClass, String methodName) {
try {
Object deSerializedObject = demoClass.newInstance();
Method doWork = deSerializedObject.getClass().getDeclaredMethod(methodName, Integer.class);
Integer param = 15;
Object result = doWork.invoke(deSerializedObject, param);
logger.info("Database object method invocation with reflection, result is = " + result);
assertTrue(true);
} catch (Exception e) {
fail();
}
}
此测试的结果如下:
Database object method invocation with reflection, result is = Result is 15
Serialized with SerializationUtils byte[] size = 30
org.apache.commons.lang3.SerializationException: java.lang.ClassNotFoundException: DemoClass
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:229)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:267)
at com.intrasoft.ssp.testing.formulas.FormulaCompilationFileTest.testSaveFormulaWithJanino(FormulaCompilationFileTest.java:115)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: DemoClass
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:686)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1866)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1749)
at java.io.ObjectInputStream.readClass(ObjectInputStream.java:1714)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1554)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223)
... 24 more
为什么classLoader找不到之前已成功加载2条语句的类?