我在Object课堂上添加了一个字段,如:
class Object {
...
private Object _objInfo;
}
Run Code Online (Sandbox Code Playgroud)
我更改了java.lang.Object源代码并重新编译了OpenJDK 6.当VM启动时,我收到以下异常:
Error occurred during initialization of VM
java.lang.IllegalStateException
at java.lang.Throwable.initCause(Throwable.java:337)
at java.lang.ExceptionInInitializerError.<init>(ExceptionInInitializerError.java:79)
Run Code Online (Sandbox Code Playgroud)
当我定义自己的Object类并将其添加到bootclasspath时会出现同样的问题,如:
java -Xbootclasspath/p:<path to my Object class>
Run Code Online (Sandbox Code Playgroud)
谢谢,Horatiu
不要修改Object.不要修改任何东西java.lang.我不知道它是否在技术上是可行的,但它绝对是一个非常糟糕的主意,并且基本上打破了Java平台(" 问:合同是Object.equals()什么?答:这取决于JVM的自定义修改使它做什么.. .) - 你将无法完成任何事情.
想想你正在做什么 - 你将这个类(和可能的行为)添加到每个对象.ClassLoaders,Strings,Threads,InputStreams,Throwables,XMLGregorianCalendar,一切.这几乎肯定不是你想要的.
Instead, an alternative approach would be to add your modifications to an abstract class AppnameSuperObject, and extend this for the classes that you really want to add this behaviour to.
On the other hand, if you really do want to do this for all objects for some kind of logging/profiling/etc kind of work, look at using aspect-oriented programming to weave the extra fields onto the classes at runtime.
在java.lang.Throwable.initCause(Throwable.java:337)上的java java.lang.IllegalStateException初始化期间发生错误在java.lang.ExceptionInInitializerError.(ExceptionInInitializerError.java:79)
将java.lang.IllegalStateException被抛出,如果initCause()被调用一次以上.听起来像对Object的修改导致异常,当JVM尝试创建Exception对象(它是Object的子类)时,它会进入递归循环并尝试在同一个Exception对象上多次调用initCause().
为什么要修改Object的定义?
| 归档时间: |
|
| 查看次数: |
1830 次 |
| 最近记录: |