-1

In JVM, I want to change the behaviour of toString method of Object class to return getClass().getName(); instead of getClass().getName() + "@" + Integer.toHexString(hashCode());

I tried Javassist's Hotswapper, but it requires a debug port to be allocated while starting the tomcat server. Is there any other way to change the functionality of toString() of Object class in JVM ?

My use case: some of the objects in my JVM didn't have toString() implementation. So basic implementation in Object.class is taken, which will not be unique(because of hashcode). I have a record and test environment, in which values needs to be unique, only then they can be compared automatically.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 5
    Modifying the behavior of all classes in Java sounds like a seriously bad idea to me. – Andy Turner Aug 06 '18 at 09:06
  • 4
    Your question is tagged [mockito], that makes me think you are probably doing tests. Could you explain why you want to do that? It sounds like a XY problem. – jhamon Aug 06 '18 at 09:07
  • It sounds like misunderstanding of OOP... – Vadim Aug 06 '18 at 09:08
  • Why you want to change the behaviuor of Object class. – Dinesh Aug 06 '18 at 09:15
  • some of the objects in my JVM didn't have toString() implementation. SO basic implementation in Object.class is taken, which will not be unique(bcaz of hashcode). I have a record and test environment, in which values needs to be unique, only then they can be compared automatically – Manikchand Parthiban Aug 06 '18 at 09:49
  • Please note: A) I updated my answer to reflect your latest input B) I am pretty sure that no other "better" answer will be coming in (so consider re/accepting at some point. C) I would suggest: put up another question. Give an *example* class, and show us the code that has trouble because of toString() not resulting in unique values. You see, that whole claim is a bit dubious. So: ask a new question, give (minimal, but enough) example code ... and then see what happens. And sure, you can drop me a comment when you have done that ;-) – GhostCat Aug 28 '18 at 18:45

1 Answers1

6

You probably could do that by tampering with the actual Object.class file (compiling your very own version of that, to then replace the .class file in the corresponding JAR file). But I would seriously hope that the JVM notices that, and slaps on your fingers.

Doing it "later", after JVM startup, is fully impossible. The java.lang classes are all loaded by the initial "boot" class loader, which is baked into the JVM, and which you can't change (until you start building your own JVM). So long before any of your code is invoked by the JVM, it has already loaded java.lang.Object, and the implementation from the .class file it found in the file system.

As the comments imply, the real answer here is: you are going down the wrong rabbit hole.

There are probably hundreds, if not thousands of internal APIs or 3rd party libraries that assume that the default toString() result looks like it does. Of course, relying on that assumption is a bad idea, but nonetheless, your change could break such code.

Beyond that, the real answer is: when you don't like the default behavior, change it for those classes that matter to you.

Update 1, one other (theoretical) approach: it might be possible to hook into the class loader, so that when specific "user classes" get loaded, that process is intercepted, and a generated toString() method is added (probably what the OP tried with javaasist). But the OP wants to do this in a tomcat context, and tomcat has its own complex hierarchy of class loaders. No realistic chance for this option either! And even if you would have full control over class loaders, we are talking "black magic voodoo" style hacking here. Things you do for an education project, but not for something that has to function robustly and reliably in the real world.

Update 2, regarding the updated requirements. The OP basically wants to change specific behavior of classes without re-compiling them. But given the other requirements, this is simply not possible here. All options to enable such "dynamics" in Java are not suited for a real world production environment.

Thus the answer simply stays put: what the OP asks for might be theoretically possible, but for all practical purposes, it is not feasible.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I have tried JavaAssist HotSwapperAgent to swap the class loaded in classloader and it is working successfully. Let me check the feasibility of what I have done and update you. Thanks for sharing your knowledge. This is just a try in testing environment. – Manikchand Parthiban Aug 29 '18 at 10:15