一个例子:
java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
at java.lang.reflect.Field.get(Field.java:393)
at utest.runner.BaseRunner.runUTestTask(BaseRunner.scala:30)
at utest.runner.BaseRunner$$anonfun$utest$runner$BaseRunner$$makeTask$1.apply(BaseRunner.scala:95)
at utest.runner.BaseRunner$$anonfun$utest$runner$BaseRunner$$makeTask$1.apply(BaseRunner.scala:95)
at utest.runner.Task.executeInternal(Task.scala:48)
at utest.runner.Task.execute(Task.scala:19)
at sbt.TestRunner.runTest$1(TestFramework.scala:76)
at sbt.TestRunner.run(TestFramework.scala:85)
at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:185)
at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
at sbt.TestFunction.apply(TestFramework.scala:207)
这是遇到该异常的源位置,这是该行的副本:
val suite = TestUtils.loadModule(name, testClassLoader).asInstanceOf[TestSuite]
明天我将调试 utest 以跟踪这个特殊情况,这一定是一个边缘情况,因为它只发生在我的一个启用 utest 的项目中(可能与那里的目录路径和包对齐有关)。
但是您能否建议一种避免这种反射错误的防御性编程的良好形式,以便代码可以在调用之前捕获错误条件asInstanceOf
,或者,catch 块的惯用代码 - 传达强制失败的原因,以更可行的方式?