简体   繁体   English

将字段添加到java.lang.Object

[英]Add a field to java.lang.Object

I added a field to Object class, as in : 我在Object类中添加了一个字段,如:

class Object {
   ...

   private Object _objInfo;
}

I changed java.lang.Object 's source code and recompiled OpenJDK 6. I get the following exception when the VM boots: 我更改了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)

The same problem occurs when I define my own Object class and prepended it to bootclasspath, as in: 当我定义自己的Object类并将其添加到bootclasspath时会出现同样的问题,如:

java -Xbootclasspath/p:<path to my Object class>

Thanks, Horatiu 谢谢,Horatiu

Don't modify Object . 不要修改Object Don't modify anything in java.lang . 不要修改java.lang任何内容。 I don't know if it's technically possible, but it is definitely an exceptionally bad idea, and basically breaks the Java platform (" Q : What's the contract of Object.equals() ? A : It depends what the custom modifications to the JVM make it do...") - you wouldn't be able to get anything done. 我不知道它是否在技术上是可行的,但它绝对是一个非常糟糕的主意,并且基本上打破了Java平台(“ Object.equals()的合同是什么? :这取决于对JVM的自定义修改让它做......“) - 你将无法完成任何事情。

Think about what you're doing - you're adding this class (and possible behaviour) to every object. 想想你正在做什么 - 你将这个类(和可能的行为)添加到每个对象。 ClassLoaders, Strings, Threads, InputStreams, Throwables, XMLGregorianCalendar, everything . ClassLoaders,Strings,Threads,InputStreams,Throwables,XMLGregorianCalendar, 一切 This is almost certainly not what you intended. 这几乎肯定不是你想要的。

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. 相反,另一种方法是将您的修改添加到抽象类AppnameSuperObject ,并为您真正想要添加此行为的类扩展它。


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 . 另一方面,如果您真的想为所有对象执行此类操作以进行某种日志记录/分析/等工作,请查看使用面向方面编程 在运行时将额外字段编织到类上

Error occurred during initialization of VM java.lang.IllegalStateException at java.lang.Throwable.initCause(Throwable.java:337) at java.lang.ExceptionInInitializerError.(ExceptionInInitializerError.java:79) 在java.lang.Throwable.initCause(Throwable.java:337)上的java java.lang.IllegalStateException初始化期间发生错误在java.lang.ExceptionInInitializerError。(ExceptionInInitializerError.java:79)

The java.lang.IllegalStateException is thrown if initCause() is called more than once. 如果多次调用initCause()initCause() java.lang.IllegalStateException Sounds like your modification of Object is causing an exception and when the JVM tries to create an Exception object (which is a subclass of Object) it gets into a recursive loop and attempts to call initCause() more than once on the same Exception object. 听起来像对Object的修改导致异常,当JVM尝试创建Exception对象(它是Object的子类)时,它会进入递归循环并尝试在同一个Exception对象上多次调用initCause()。

Why do you want to modify the definition of Object? 为什么要修改Object的定义?

Apparently, there are still a number of places in native code where field offsets are hardwired. 显然,本机代码中仍有许多地方存在字段偏移硬连线。 Modifying some classes, such as Thread , mess this up. 修改一些类,比如Thread ,搞砸了。 If you change Object , you mess them all up. 如果您更改了Object ,则会将它们全部搞砸。

I suspect that there is something inside the implementation of the JVM that assumes the size of Object. 我怀疑JVM的实现中有一些东西假定了Object的大小。 You've made it larger so that code is failing. 你已经把它做得更大,以致代码失败了。

Because this is an error that the JVM implementors never considered, error handling breaks. 因为这是JVM实现者从未考虑过的错误,所以错误处理会中断。

The answer: you can't modify Object without doing a lot more work. 答案是:如果不做更多工作,就无法修改Object。

您最好使用要放入Object的此字段创建一个类X,并使您的类继承自X.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM