Last active
September 13, 2017 20:37
-
-
Save kouzouigh/f374c4b5041df45dcf54f84b8c74846d to your computer and use it in GitHub Desktop.
Intercept Date Constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws Exception { | |
ByteBuddyAgent.install(); | |
new AgentBuilder.Default() | |
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) | |
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE) | |
.with(AgentBuilder.TypeStrategy.Default.REDEFINE) | |
.ignore(new AgentBuilder.RawMatcher.ForElementMatchers(nameStartsWith("net.bytebuddy.").or(isSynthetic()), any(), any())) | |
.with(new AgentBuilder.Listener.Filtering( | |
new StringMatcher("java.util.Date", StringMatcher.Mode.EQUALS_FULLY), | |
AgentBuilder.Listener.StreamWriting.toSystemOut())) | |
.type(named("java.util.Date")) | |
.transform((builder, type, classLoader, module) -> | |
builder.visit(Advice.to(Interceptor.class).on(isConstructor())) | |
) | |
.installOnByteBuddyAgent(); | |
Date date = new Date(); | |
System.out.println(date) | |
} | |
public class Interceptor { | |
@Advice.OnMethodExit | |
public static void onExit(@Advice.This Date obj) { | |
Calendar instance = Calendar.getInstance(); | |
instance.set(2017, Calendar.SEPTEMBER, 13, 12, 00); | |
obj.setTime(instance.getTimeInMillis()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment