question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to rebase System.currentTimeMillis

See original GitHub issue

Hi,

I’ve spent a long time trying to figure out how to rebase System.currentTimeMillis. What I wanted is override this bootstrap class’s method with another customized clock, which means I still need to call the original System.currentTimeMillis to get the actual time and have some transformation on it. I have already successfully redefine the method with following code on Java agent mode:-

final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
new AgentBuilder.Default()
	.enableNativeMethodPrefix("test_native")
	.with(byteBuddy)
	.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
	.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
	.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
	.ignore(none())
	.type(ElementMatchers.named("java.lang.System"))
	.transform(new AgentBuilder.Transformer() {
		@Override
		public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
					return builder.method(ElementMatchers.named("currentTimeMillis")).intercept(
						MethodDelegation.to(SystemTimeInterceptor.class));
				}
			}).installOn(inst);

Can you please kindly advise if it is ever possible to rebase that method? Any suggestion will be appreciated. Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
huangyojecommented, Jan 21, 2021

Just ran into the same problem, put my solution here, hoping to help others. If you need redefine System. currentTimeMillis(), there is another way to get the real time.

   Process process = Runtime.getRuntime().exec("date +%s");
   BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
   System.out.println(reader.readLine());
0reactions
RockyLOMOcommented, Nov 1, 2022

My solution is to call nanoTime and currentTimeMillis once each before injecting and save the result, so that the current time can be calculated using

 (System.nanoTime() - savedNanoTime) / 1000000L + savedCurrentTimeMillis

, and then use ntp synchronized time to calculate the offset and change the savedCurrentTimeMillis value.

 (System.nanoTime() - savedNanoTime) / 1000000L + savedCurrentTimeMillis + ntpOffset
Read more comments on GitHub >

github_iconTop Results From Across the Web

Redefine/Rebase native method - java - Stack Overflow
After trying with different combinations I finally get it works now. Actually the above code is correct. I think the possible reason not...
Read more >
[#CASSANDRA-14092] Max ttl of 20 years will overflow ...
It turns out that next year the localDeletionTime will start overflowing with the maximum ttl of 20 years (System.currentTimeMillis() + ...
Read more >
org.apache.jackrabbit.mk.model.StagedNodeTree.reset java code ...
public Id rebase(Id baseId, Id fromId, Id toId, PutToken token) throws Exception { // reset staging area to new base revision reset(baseId); StoredNode ......
Read more >
Repository xref - Eclipse archive
46 */ 47 48 package org.eclipse.jgit.lib; 49 50 import java.io.BufferedOutputStream; 51 import java.io. ... 105 * <p> 106 * This class is thread-safe....
Read more >
How to Git rebase master onto any branch by example
If other developers are currently working on branches that stem from those deleted commits, they will not be able to push their changes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found