Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a field to java.lang.Object

Tags:

java

jvm

I added a field to Object class, as in :

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:

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:

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

Thanks, Horatiu

like image 248
Horatiu Jula Avatar asked Aug 11 '10 20:08

Horatiu Jula


People also ask

How do you set a field to an object in Java?

The set() method of java. lang. reflect. Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter.

How do you add a new field in Java?

In the diagram editor, right-click a Java class or interface; then click Add Java > Field. In the Create Java Field wizard, in the Name field, type a name for the new field. In the Initial Value field, specify the initial value expression.


2 Answers

Don't modify Object. Don't modify anything in 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.

Think about what you're doing - you're adding this class (and possible behaviour) to every object. ClassLoaders, Strings, Threads, InputStreams, Throwables, XMLGregorianCalendar, everything. 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.


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.

like image 200
Andrzej Doyle Avatar answered Oct 23 '22 07:10

Andrzej Doyle


Error occurred during initialization of VM java.lang.IllegalStateException at java.lang.Throwable.initCause(Throwable.java:337) at java.lang.ExceptionInInitializerError.(ExceptionInInitializerError.java:79)

The java.lang.IllegalStateException is thrown if initCause() is called more than once. 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.

Why do you want to modify the definition of Object?

like image 24
Jim Garrison Avatar answered Oct 23 '22 08:10

Jim Garrison