简体   繁体   English

javassist和抽象通用类的java.lang.AbstractMethodError

[英]java.lang.AbstractMethodError with javassist and abstract generic classes

i want to execute a method within a generated class that extends an abstract generic one but it throws java.lang.AbstractMethodError. 我想在生成的类中执行一种方法,该方法扩展了一个抽象的泛型类,但是会引发java.lang.AbstractMethodError。

PS: the generic class is not generated PS:通用类未生成

Example: 例:

public abstract class AbstractSpecification<T> implements Specification<T>{
    public abstract boolean isSatisfiedBy(T t); 
    public Specification<T> and(final Specification<T> specification) {
        return new AndSpecification<T>(this, specification);
    }
}

The generated class is 生成的类是

ClassPool pool = new ClassPool(true);
pool.insertClassPath(new ClassClassPath(AbstractSpecification.class));
CtClass abstractClazz = pool.get("com.mycompany.AbstractSpecification");
CtClass myclass = pool.makeClass("ValidAge");
myclass.setSuperclass(abstractClazz);
.
.
.

Invoking isSatisfiedBy works but and throws java.lang.AbstractMethodError 调用isSatisfiedBy工作,但and抛出java.lang.AbstractMethodError

Javassist does not implement bridge methods for you, this remains your responsibility. Javassist不会为您实现桥接方法,这仍然是您的责任。 Therefore, you need to implement two methods when subclassing your AbstractSpecification , the actual method and the bridge of the erasure of T . 因此,在对AbstractSpecification进行子类化时,需要实现两种方法,即实际方法和T擦除的桥梁。

For example, for implementing AbstractSpecification<Foo> , you would need to implement a method isSatisfiedBy(Foo) and the bridge isSatisfiedBy(Object) where the latter method only invokes the former including a casting. 例如,要实现AbstractSpecification<Foo> ,您将需要实现一个方法isSatisfiedBy(Foo)和一个桥接器isSatisfiedBy(Object) ,其中后一种方法仅调用前者,包括转换。

If this seems to much work, have a look at Byte Buddy which handles bridge methods transparently for you. 如果这似乎有用 ,请看一下Byte Buddy ,它为您透明地处理桥接方法。

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

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