10

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

5
  • Why don't you create a classe to put _objInfo? Commented Aug 11, 2010 at 20:22
  • 2
    I don't think you actually ask a question anywhere. Commented Aug 11, 2010 at 20:26
  • 4
    @justin, it is pretty clear what he would like us to help him with... Commented Aug 11, 2010 at 20:27
  • 14
    Better question: Why on earth are you modifying the base object class? Commented Aug 11, 2010 at 20:38
  • You should better describe your use case and what you are trying to achieve in the end. Commented Dec 28, 2010 at 17:20

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Or, instead of lecturing, you could try to answer the OP's question. There is this radical idea that if you don't know the answer, you shouldn't post one. Instead, consider asking clarifying questions in comments.
As Ted Neward already described back in 2001, the bootclasspath exists, in part, for exactly this reason: to be able to modify java.* classes (tedneward.com/files/Papers/BootClasspath/BootClasspath.pdf)
@Oleg -- Andrzej does know the answer. The answer is "don't do that". If your four-year-old asked "How do I get the cap off the bottle of bleach?" what would you answer? @Horatui -- the idea is (I think) that you weave the change into all your classes, not all classes in the system.
6

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?

2 Comments

I have to keep some information at runtime for each object used as a lock. For that , I have a global hash map that maps an obj id returned by System.identityHashCode() to some info associated to the lock. But this lookup is expensive ; it will be much faster if I would have a field in class Object that can point to arbitrary information about the object.
@HoratiuJula this is exactly what I need for my Algorithmic Profiler, except that I need to keep track of all profiled objects. Just as you, I started with a map indexed by System.identityHashCode() only to find it too slow...
4

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. If you change Object, you mess them all up.

3 Comments

I know, in Dalvik VM (used in Android) I had the same problem... But there, I found the offsets and recalculated them. However, I don't know where field offsets for Object, String ,etc., are hardcoded in openjdk...
@Horatiu Jula : it seems like the question you really want answered is... "What changes do I need to make to the openjdk 6 JVM sources if I extend the memory footprint of the Object class?" I would warn you that the openjdk VM is likely to have been the victim of some very intense performance optimizations that will make the changes that you need to make to support your desired changes to Object obtuse and widespread.
@Horatiu Jula : you may need to make mods to javac, too.
1

I suspect that there is something inside the implementation of the JVM that assumes the size of 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.

The answer: you can't modify Object without doing a lot more work.

Comments

0

You better create a class X with this field you want to put in Object and make your classes inherit from X.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.