简体   繁体   English

Bytebuddy - 最后一课的子类

[英]Bytebuddy - subclass a final class

I'm trying to write a generic method that looks like this: 我正在尝试编写一个如下所示的泛型方法:

private static <T> Class<? extends T> immutableVersionOfClass(Class<T> clazz) {
    return new ByteBuddy()
            .subclass(clazz)
            .method(not(returns(VOID)))
            .intercept(to(GetterInterceptor.class))
            .method(returns(VOID))
            .intercept(to(SetterInterceptor.class))
            .make()
            .load(clazz.getClassLoader())
            .getLoaded();
}

but when final class is passed as an argument I get an exception: java.lang.IllegalArgumentException: Cannot subclass primitive, array or final types I'd like my method to be able to subclass also final classes. 但是当最终类作为参数传递时,我得到一个异常: java.lang.IllegalArgumentException: Cannot subclass primitive, array or final types我希望我的方法能够java.lang.IllegalArgumentException: Cannot subclass primitive, array or final types最终类。 Is there any workaround for this problem? 这个问题有解决方法吗?

There is no way to subclass a final class. 没有办法继承最终的类。 It's forbidden by the language spec . 它被语言规范所禁止。

ByteBuddy largely respects the language specification, so even if you wanted to extend a final class, you couldn't unless you were able to manipulate the byte code of the class you wanted to override to be not final , although at that point you're messing with things you really shouldn't be. ByteBuddy在很大程度上尊重语言规范,所以即使你想扩展一个最终的类,除非你能够操纵你想要覆盖的类的字节代码而不是 final ,否则你不能 ,尽管那时你是弄乱你真的不应该做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM