Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用泛型改进类_Java_Generics - Fatal编程技术网

Java 使用泛型改进类

Java 使用泛型改进类,java,generics,Java,Generics,我有很多相同的课程。我想使用泛型来改进它: public class PingInitializer extends AbstractHandler implements DataWarehouseInitializer<PingInteraction, PingInvocation> { @Handler @Override public PingInteraction handle(Message message) throws IOExcepti

我有很多相同的课程。我想使用泛型来改进它:

public class PingInitializer extends AbstractHandler implements    DataWarehouseInitializer<PingInteraction, PingInvocation> {

    @Handler
    @Override
    public PingInteraction handle(Message message) throws IOException, MessageException {
        checkMessageIsNotNull(message);
        PingInvocation invocation = construct(message.getBody().toString());
        l.debug("handling in initializer...  {}", invocation);
        return new PingInteraction(invocation);
    }

    public PingInvocation construct(String message) throws IOException, MessageException {
        ObjectMapper mapper = new ObjectMapper();
        PingInvocation invocation;
        try {
            invocation = mapper.readValue(message, PingInvocation.class);
        } catch (IOException e) {
             throw new MessageException("Can't deserialize message", e);
        }
        return invocation;
    }
}
公共类PingInitializer扩展AbstractHandler实现DataWarehouseInitializer{
@处理者
@凌驾
公共ping交互句柄(消息消息)抛出IOException,MessageException{
checkMessageIsNotNull(消息);
PingInvocation=construct(message.getBody().toString());
l、 调试(“初始值设定项中的处理…{}”,调用);
返回新的PingInteraction(调用);
}
公共PingInvocation构造(字符串消息)引发IOException,MessageException{
ObjectMapper mapper=新的ObjectMapper();
ping调用;
试一试{
invocation=mapper.readValue(消息,PingInvocation.class);
}捕获(IOE异常){
抛出新的MessageException(“无法反序列化消息”,e);
}
返回调用;
}
}
我想创建新的抽象类AbstractInitializer,所有子类只需指定泛型类型:

public abstract class AbstractInitializer<INTERACTION, INVOCATION> extends AbstractHandler {

    @Handler
    public INTERACTION handle(Message message) throws IOException, MessageException {
        checkMessageIsNotNull(message);
        INVOCATION invocation = construct(message.getBody().toString());
        l.debug("handling in initializer...  {}", invocation);
        return **new INTERACTION(invocation)**; //HERE!
    }

    public INVOCATION construct(String message) throws IOException, MessageException {
        ObjectMapper mapper = new ObjectMapper();
        INVOCATION invocation;
        try {
            invocation = mapper.readValue(message, **INVOCATION.class** /*<- and HERE */);
        } catch (IOException e) {
            throw new MessageException("Can't deserialize message", e);
        }
        return invocation;
    }
}
公共抽象类AbstractInitializer扩展了AbstractHandler{
@处理者
公共交互句柄(消息消息)抛出IOException,MessageException{
checkMessageIsNotNull(消息);
INVOCATION INVOCATION=construct(message.getBody().toString());
l、 调试(“初始值设定项中的处理…{}”,调用);
return**new INTERACTION(invocation)**;//此处!
}
公共调用构造(字符串消息)抛出IOException,MessageException{
ObjectMapper mapper=新的ObjectMapper();
调用调用;
试一试{

invocation=mapper.readValue(消息,**invocation.class**/*Java泛型是使用类型擦除实现的,这是出于兼容性原因,这最终意味着您不能仅使用类型参数来实例化新对象

您需要的是稍微修改一下
AbstractInitializer

private final Class<INTERACTION> interactionType;
private final Class<INVOCATION> invocationType;
private final Constructor<INTERACTION> interactionConstructor;

public AbstractInitializer(final Class<INTERACTION> interactionType,
    final Class<INVOCATION> invocationType) throws NoSuchMethodException {
  this.interactionType = interactionType;
  this.invocationType = invocationType;
  interactionConstructor = interactionType.getConstructor(invocationType);
}

public INTERACTION handle(Message message) throws IOException, MessageException,
        InvocationTargetException, InstantiationException, IllegalAccessException {
  checkMessageIsNotNull(message);
  INVOCATION invocation = construct(message.getBody().toString());
  l.debug("handling in initializer...  {}", invocation);
  return interactionConstructor.newInstance(invocation);
}

public INVOCATION construct(String message) throws IOException, MessageException {
  ObjectMapper mapper = new ObjectMapper();
  INVOCATION invocation;
  try {
    invocation = mapper.readValue(message, invocationType);
  } catch (IOException e) {
    throw new MessageException("Can't deserialize message", e);
  }
  return invocation;
}

相信这一切都是可能的。我可能犯了一些愚蠢的错误,但是,因为我很快在回复框中键入了这些。

请将您的错误提供给我们。
public final class PingInitializer
    extends AbstractInitializer<PingInteraction, PingInvocation> {

  public PingInitializer() {
    super(PingInteraction.class, PingInvocation.class);
  }
}
public static GenericInitializer<A, B> createInitializer(final Class<A> a,
    final Class<B> b) {
  return new GenericInitializer<A, B>(a, b);
}

final GenericInitializer<PingInteraction, PingInvocation> pingInitializer
    = createInitializer(PingInteraction.class, PingInvocation.class);