Skip to content

redefine for java.time.Clock#instant not work with jdk 17 runtime #1843

@lossend

Description

@lossend

jvm runtime: openjdk17
bytebudy version: 1.14.3

i tried to interrupt " java.time.Clock#instant" method with below code, but it did not work?

   new AgentBuilder.Default()
                .disableClassFormatChanges()
                .ignore(nameStartsWith("net.bytebuddy."))
                .with(new TransformListener())
                .with(new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE))
                .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
                .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
                .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
                .type(hasSuperClass(ElementMatchers.named("java.time.Clock")))
                .transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder
                        .method(ElementMatchers.named("instant"))
                        .intercept(Advice.to(ClockInterceptor.class).wrap(StubMethod.INSTANCE)))
                .installOn(inst);

ClockInterceptor code is:

@Slf4j
public class ClockInterceptor {

    @Advice.OnMethodExit(suppress = Throwable.class)
    public static void onExit(@Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Instant result) {
        if (ClockMockDispatcher.dispatcher != null) {
            long mockMillis = ClockMockDispatcher.dispatcher.mockMillis();
            if (mockMillis > 0) {
                result = Instant.ofEpochMilli(mockMillis);
            }
        }
    }
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions