JVM: easiest way to change dependency library code?

In most cases, I do not like Javascript and prefer strict and compiled languages ​​such as Scala, Java, Haskell ...

However, one thing that can be nice with Javascript is that it is easy to modify the code of external dependencies. For example, if you have a bug, and you think this is one of your dependency libraries, you can easily hack and change the library method with your own override and check if this is better. You can even add methods to Array ou String prototypes and the like ... You can even switch to node_modules and temporarily change the library code if you want.

In the JVM world, this seems like a hard process to get started:

  • Clone Dependency Sources
  • Hack it
  • Compile it
  • Post it to some maven / ivy local storage
  • Integrate the fixed version into your project

This is a pain, I just do not want to do this more than once a year. Today I tried to fix a bug in my application, and lib did not provide me with enough information. I would just like to put Logger on one line of this library to better understand what is happening, but instead I tried to crack the debugger without success (the error was not reproduced on my computer anyway ...)

Is there a simple alternative to quickly change the dependency code?

I would be interested in any solution for Scala, Java, Clojure or any other JVM language.

I am not looking for a deployment solution, just a quick solution for local use and, ultimately, deployment on a test env.

Change I am talking about internal libraries that are not intended to be modified by the author of the library. Suppose the class to be modified is final, not replaceable by the configuration of the library, and not introduced in any way into the library.

+5
source share
2 answers

In Clojure, you can relink vars, also from other namespaces, using intern . So while the code you want to change is Clojure code, this is a possible monkey way.

 (intern 'user 'inc dec) (inc 1) => 0 

This is not something that needs to be done easily, as it can and will lead to problems with other code that does not expect such behavior. During development, it may be useful to use temporary bug fixes or errors in other libraries, but not use them in published libraries or production code.

It’s best to simply unlock and fix these libraries and send a transfer request so that it is fixed in the source library.

When you write a library yourself that you expect people to expand or overload, implement it in the Clojure protocols, where these changes can only be limited to the extension / overload namespace.

+1
source

I don’t agree that AspectJ is difficult to use, this or another bytecode manipulation library is your only realistic alternative.

Boot training is a certain way around this problem. Depending on how you use the class in question, you might even be able to use a mocking library to achieve the same results, but something like AspectJ, which is specifically designed to grow and manipulate, will probably be the easiest.

+1
source

Source: https://habr.com/ru/post/1213832/


All Articles