0

In JavaScript it is easy to modify (or even replace entirely) built-in classes. One could simply write a name of a method and inject it into built-in class in JavaScript simply by typing = and defining a function. Then this method can be used on every instance of that class.

For example, I injected a method multiply into String JavaScript built-in class which multiplies string which string object contains given number of times.

Can I do something similar in Java?

I know that I can extend some built-in class and in my custom class define this method. But, is it possible to inject a method in a built-in Java class like in JavaScript so that everywhere where that class is used in the program this custom method is available?

  • Sounds like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is your end goal here? – VLAZ Oct 20 '21 at 10:41
  • No, it is not XY, this was just a hypothetical question. I do not have an actual need to inject a method. I just wanted to know is this possible in Java since I could not find any guide online about how to do it. – KRISTIJAN TOMASINI Oct 20 '21 at 11:04

1 Answers1

0

This is possible to do during runtime. But the simple answer is that you do not want to do that. Bytecode modification is extremely fragile and complex. Let alone that your code also won't compile if you directly call the new method you have injected, meaning you have to resort to expensive methods such as Reflection.

Your best alternative would be to make a utility class for Strings that has this multiply method. Something like this:

public class StringUtil {
    public static String multiply(String s1, int i) { ... }
}

For non-final classes (so, not the String class), you can also choose to extend the class and add your methods to the newly created class:

public class MyType extends Type {
    public static SomeType someMethod() { ... }
}

This will require you to replace all types of type Type with MyType

I do not fully understand your example, but it sounds like String#repeat(int) might do the same as what you are trying to achieve.

bramhaag
  • 628
  • 1
  • 9
  • 24
  • 1
    OP was asking about something different. In JavaScript, C# and more languages you have extension methods which you can define before runtime and use like `"HelloWorld".yourMethod()` – Gaskoin Oct 20 '21 at 10:45
  • I answered that in my first paragraph. They are possible but very fragile to implement. – bramhaag Oct 20 '21 at 10:48
  • Thank you. Yes, ```repeat``` method is the one which I would use. I just took ```multiply``` as an example of a method name and functionality. I would not inject this method, I would, of course, use ```repeat``` as you suggested for this purpose. This was just a hypothetical question. – KRISTIJAN TOMASINI Oct 20 '21 at 11:00
  • 1
    @bramhaag this is not valid answer since extending a runtime is not the same as JS/C# works as you can work only with reflection. Proper answer would be that this is possible via annotation processing so the method can be available during compilation. Look for example here https://projectlombok.org/features/experimental/ExtensionMethod – Gaskoin Oct 20 '21 at 11:12