我从非常令人印象深刻的字节码操作库 byte-buddy 开始。它工作正常,但我对抽象的参数化类进行子类化时遇到问题:
public interface Task<DTO extends IDatabaseObject> {
void execute(DTO input);
Class<DTO> getDataObjectClass();
}
这是抽象类:
public abstract class AbstractTask<T extends IDatabaseObject> implements Task<T> {
protected Class<T> dataObjectClass = /* Call to an external method which retrieves the class from T */;
@Override
public Class<T> getDataObjectClass() {
return dataObjectClass;
}
}
我想创建一个具体的类扩展
public abstract class AbstractTask<T extends IDatabaseObject> implements Task<T> {
protected String SUCCESS_MESSAGE_PREFIX = "task.mess.";
protected Class<T> dataObjectClass;// = Introspector.getParameterizedTypeClass(this, AbstractTask.class, 0);
@Override
public Class<T> getDataObjectClass() {
return dataObjectClass;
}
@Override
public String getSuccessMessage(IDatabaseObject t) {
final String messageKey = SUCCESS_MESSAGE_PREFIX + this.getClass().getSimpleName();
final MessagesFactory messagesFactory = MessagesFactory.getInstance();
return messagesFactory.isPresent(messageKey) ? messagesFactory.get(messageKey) : "";
}
}
我想创建一个具体的AbstractTask
, 类来满足以下断言:
createConcreteImplementation(Person.class).getDataObjectClass() == Person.class
该createConcreteImplementation
方法通过 Byte Buddy 创建一个子类。即使这是不可能的,我也欢迎提供替代方法或近似此行为的建议。