jvm-sandbox动态修改java.util.Date

        最近在搞基于sandbox-repeator的录制回放,其中有一个重要的问题需要自己解决,就是回放的时候,代码里new Date获取的时间,应该跟采集的时候的时间一致;所以我就测试了下,基于sandbox的事件监听来搞,demo代码如下:

package org.example;

import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.ProcessController;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.Advice;
import com.alibaba.jvm.sandbox.api.listener.ext.AdviceListener;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import org.kohsuke.MetaInfServices;

import javax.annotation.Resource;

import java.util.Date;

@MetaInfServices(Module.class)
@Information(id = "date-mocker")
public class DateMockModule  implements Module {

    @Resource
    private ModuleEventWatcher moduleEventWatcher;

    @Command("repairCheckState")
    public void repairCheckState() {

        new EventWatchBuilder(moduleEventWatcher)
                .onClass(Date.class)
                .includeBootstrap()
                .onBehavior("")
                .withParameterTypes("long")
                .onWatch(new AdviceListener() {

                    protected void before(Advice advice) throws Throwable {
                        System.out.println("here");
                        //这里修改入参时间
                        advice.changeParameter(0, 1669180397000L);
                    }

                });

    }
}

你可能感兴趣的:(java,java,jvm,服务器)