有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

最终量化方法的java方法拦截器

我正在尝试某种方法拦截器,它可以截获类中的方法。我试着用cglib,bytebuddy。我不能使用普通的Java代理类,因为它是一个类。有没有办法拦截我的final方法。 这就是我这么做的目的

//我的目标类

public class Hello {
    public final  String sayHello(){
    //return lower case hello
    return "hello";
    }
}

//主应用程序

public class InterApp {
    public static void main(String[] d) throws Exception {
        new InterApp().loadclassDD();
    }

    public void loadclassDD() throws Exception {
       //Byte-buddy
        Hello helloObject1 = new ByteBuddy()
                .subclass(Hello.class)
                .method(named("sayHello"))
                .intercept(MethodDelegation.to(LoggerInterceptor.class))
                .make()
                .load(getClass().getClassLoader(),
                        ClassLoadingStrategy.Default.WRAPPER).getLoaded()
                .newInstance();
        System.out.println(helloObject1.sayHello());
       //CGLIB
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Hello.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object arg0, Method arg1, Object[] arg2,
                    MethodProxy arg3) throws Throwable {
                System.out.println("method name " + arg1.getName());
                return arg3.invokeSuper(arg0, arg2).toString().toUpperCase();
            }
        });
        Hello proxy = (Hello) enhancer.create();
        System.out.println(proxy.sayHello());
    }
}

//LoggerInterceptor-字节伙伴实现

public class LoggerInterceptor {
public static String log(@SuperCall Callable<String> zuper)throws Exception {
    System.out.println("Method intercepted");
    return zuper.call().toUpperCase();
}
}

如果我从方法中删除最后一个量词,这两个量词都可以工作(提供大写HELLO作为输出)。原因是什么。?有没有办法做到这一点?如果我的理解有误,请纠正我


共 (2) 个答案

  1. # 1 楼答案

    您可以引入一个由最终类实现的接口。这样,代理方法将成为一个选项,因为代理可以委托给原始类,同时仍然保留实现的最终方面

    Byteman将失败,因为您试图对最终方法进行子类化和重写。不管您是手工编码还是通过字节码操作库来实现这一点。它只是违反了类定义的契约。因此,即使它成功了,也可能是个坏主意,因为原始类的作者定义最终方面是有原因的

  2. # 2 楼答案

    与上面的答案相反,使用Byteman改变方法sayHello的定义似乎是/可能的,即使它是最终的。例如,以下Byteman规则:

    规则更改返回值 同学们好 方法说你好 出口处 如果是真的 DO$!=$!+“来自拜特曼” ENDRULE

    将返回的字符串更新为“Hello from Byteman”