-3

Is there a plugin or a tool you can use to simply add this method to the Object class in java?

public this set(Object o){
    this = o;
    return this;
}
  • 2
    You can't (and certainly don't want to) add methods to classes, and this isn't even valid Java code. So I guess the answer is no. – JB Nizet Mar 24 '18 at 14:59

1 Answers1

0

Injecting code into the Object class actually is the smaller problem (although personally, I won't help you finding a solution - if one exists), the content of your set() method is really, seriously impossible in Java.

You simply cannot assign to this. And no tool whatsoever will change that.

A given instance of a Java class will keep its identity over all its lifetime. Changing that would need a conceptually different Java Virtual Machine.

With the set() method you ask for, it would become possible to change e.g. a String to a Date, making all code using that object as a String fail. Because after the change, the object no longer has methods like indexOf(), but instead things like before() or getTime().

So, you have to look for a different solution to the problem you want to address with your proposed set() method.

Let me suggest you decribe the use case for your set() method, so we can help you find a solution.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7