[英]Compiling a java class with plexus-compiler-eclipse and running it as a JUnit test
I am currently trying to generate code on the fly, compile it then run it as a JUnit4 test. 我目前正在尝试动态生成代码,对其进行编译,然后将其作为JUnit4测试运行。 I am quite close to the goal, but I have an issue which might be either with plexus-compiler-eclipse or how I load the compiled class.
我已经很接近目标了,但是我遇到了一个问题,要么与plexus-compiler-eclipse有关,要么与我如何加载已编译的类有关。 Let me explain:
让我解释:
URLClassLoader.newInstance(new URL[] {resource }).loadClass("classname")
where resource is getClass().getResource("filename")
(also tried with getClass().forName(className, true, classLoader)
where the classLoader is the same URLClassLoader as above) URLClassLoader.newInstance(new URL[] {resource }).loadClass("classname")
加载类,其中资源是getClass().getResource("filename")
(也尝试使用getClass().forName(className, true, classLoader)
,其中classLoader与上述URLClassLoader相同) Everything seems fine until point 4, where JUnit does not see any test method on the class (I obviously did annotate with @org.junit.Test each test method in the generated code). 一切似乎都很好,直到第4点为止为止,在第4点,JUnit在类上看不到任何测试方法(我显然用@ org.junit进行了注释。在生成的代码中测试每个测试方法)。 Debugging, I can see that if I do a newInstance() on the loaded class or inspect the loaded class, I have no annotations, no public methods, no declared fields, SFA!!!
调试时,我可以看到,如果对加载的类执行newInstance()或检查加载的类,则没有注释,没有公共方法,没有声明的字段,SFA! The newInstance() only has a private member, which is normal, since its reference type is Object, but somehow I am not able to cast it to try calling the public methods that should be there.
newInstance()仅具有一个私有成员,这是正常的,因为其引用类型为Object,但是以某种方式,我无法将其强制转换为尝试调用应该存在的公共方法。
Any idea what I am missing in my compilation/class loading that makes the runtime class incomplete? 知道我的编译/类加载中缺少什么导致运行时类不完整吗?
Thanks in advance for any ideas... 预先感谢您的任何想法...
Apparently, this was pretty simple! 显然,这很简单! I found this answer that made me aware that my URLClassLoader was missing a parent.
我找到了这个答案 ,使我意识到我的URLClassLoader缺少父母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.