1

I am using a JAR file that uses System.currentTimeMillis() to get the current system time. I want to call my current time method (nanoTime()) when a function of JAR file calls System.currentTimeMillis().

Any suggestion how I can cover this?

Nihamata
  • 57
  • 1
  • 11
  • Without the source, easiest is to decompile the relevant class, change the call and then rebuild the jar. – pvg Dec 20 '15 at 06:26
  • JAR file is encrypted and not possible to decompile and then rebuild that. – Nihamata Dec 20 '15 at 06:33
  • Of course it's possible, otherwise the JVM would not be able to load it. What is not possible is to dynamically change the calls a class is making at runtime. If you can't change the JAR, you can't change what the classes call. – pvg Dec 20 '15 at 06:39
  • 1
    Are you asking something similar to [this](http://stackoverflow.com/questions/11749409/replace-content-of-some-methods-at-runtime)? – Sabir Khan Dec 20 '15 at 07:48
  • Not exactly. One way to replace call to time method is to replace all methods that uses that. I know how to do that using Javassit. However, the problem is then I need to override more than 100 methods. So, not good idea. On the other hand, I am not sure if it is possible to override System.currentTimeMillis() method with Javassit. Let me know if possible? – Nihamata Dec 20 '15 at 08:09
  • You do understand that these methods return completely different values, both in units and in origin/zero? – Oleg Estekhin Dec 20 '15 at 08:12
  • Yes. I know, and that is the reason I want to use nanoTime. – Nihamata Dec 20 '15 at 08:46
  • I do not see the problem with javassist? Yes this will modify the bytecode of each class calling `System.currentTimeMillis()` (replacing this call) but the framework does it for you. And you can limit the replacements to the classes you want. Besides: Why do you expect that changing currentTimeMillis to nanoTime does not break the modified code? – CoronA Dec 20 '15 at 13:52

1 Answers1

1

Well... implementing you own java.lang.System comes into mind. But then you'd also need set your bootclasspath and who knows what else to make this work. You might end up patching rt.jar.

It will be tricky though to reuse the "real" System class for all other purposes.

And I hope you plan to do this on one machine only - I can't imagine the trouble to deploy such a system.

Jan
  • 13,738
  • 3
  • 30
  • 55