微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

参数化类 'ABC'

如何解决参数化类 'ABC'

我有以下界面:

public interface AsynchronousJobRunner<T extends AsynchronousJob> extends Runnable {
    public void kill();
    public void addJobExecutionListener(JobExecutionListener listener);
    public void removeJobExecutionListener(JobExecutionListener listener);
    public AsynchronousJobRunner withJob(T job);
}

AsynchronousJob一个抽象类,可以扩展为使用以下抽象方法表示其他作业:

public abstract class AsynchronousJob implements JSONSerializable,HasId {...}

    /**
     * Returns the class of the {@link AsynchronousJobRunner} that runs this type of job.
     * @return The appropriate class of the job runner for this class.
     */
    public abstract Class<? extends AsynchronousJobRunner> jobRunnerClass();

我还有以下 ExportJob 扩展基类并具有以下签名和方法

public class ExportJobRunner extends BaseJobRunner<ExportJob> { ...}

    @Override
    public Class<? extends AsynchronousJobRunner> jobRunnerClass() {
        return ExportJobRunner.class;
    }

两个 jobRunnerClass() 方法都有 Raw use of parameterized class 'AsynchronousJobRunner' 警告。

使警告消失的简单解决方案是:

public abstract Class<? extends AsynchronousJobRunner<?>> jobRunnerClass();


@Override
public Class<? extends AsynchronousJobRunner<?> jobRunnerClass() {
    return ExportJobRunner.class;
}

但是正确的解决方案是什么?为什么/如何?

编辑

我最终只是将界面的代码更改为:

public interface AsynchronousJobRunner<T extends AsynchronousJob<T>> extends Runnable {
    void kill();
    void addJobExecutionListener(JobExecutionListener listener);
    void removeJobExecutionListener(JobExecutionListener listener);
    AsynchronousJobRunner<T> withJob(T job);
}

AsynchronousJob 类的更改:

public abstract class AsynchronousJob<T> implements JSONSerializable,HasId { ...
 /**
     * Returns the class of the {@link AsynchronousJobRunner} that runs this type of job.
     * @return The appropriate class of the job runner for this class.
     */
    public abstract Class<? extends AsynchronousJobRunner<? extends AsynchronousJob<T>> jobRunnerClass();

}

ExportJob 类:

public public class ExportJob extends AsynchronousJob<ExportJob> {...
    @Override
    public Class<? extends AsynchronousJobRunner<? extends AsynchronousJob<ExportJob>>> jobRunnerClass() {
        return ExportJobRunner.class;
    }
}

ExportJobRunner 类保持不变。

我还忘了提到有一些注入魔法正在进行,因为作业被序列化到数据库

    /**
     * Instantiates an {@link AsynchronousJobRunner} instance for the provided job.
     * <p>
     * In order for the creation of the runner to succeed,the {@link AsynchronousJob#jobRunnerClass()}
     * method of the job must specify the appropriate class for its runner.
     *
     * @param job job to create runner for
     * @return job runner configured for the specified {@code job} parameter
     */
    private <T extends AsynchronousJob<T>> AsynchronousJobRunner<T> createJobRunner(T job) {
        return ((AsynchronousJobRunner<T>)injector.getInstance(job.jobRunnerClass())).withJob(job);
    }

我接受了@Andrew Vershinin 的回答,因为他让我走上了正确的思路。

解决方法

我认为,您可以使用 AsynchronousJob 的递归声明:

public abstract class AsynchronousJob<T extends AsynchronousJob<T>> 
    implements JSONSerializable,HasId {

    public abstract Class<? extends AsynchronousJobRunner<T>> jobRunnerClass();
//...
}

不要忘记相应地更新 AsynchronousJobRunner 定义:

public interface AsynchronousJobRunner<T extends AsynchronousJob<T>> //...

然后,在您的工作类中,您可以在返回类型中使用类本身:

public class ExportJob extends AsynchronousJob<ExportJob> {
    @Override
    public Class<? extends AsynchronousJobRunner<ExportJob>> jobRunnerClass() {
        return ExportJobRunner.class;
    }
}

通过这种方式可以确保类型一致性:T 类型的作业会返回运行此类类型的作业的运行器类型 - 您无法通过在 ? extends AsynchronousJob 签名中使用 jobRunnerClass 来确保这一点,因为你可以返回任何跑步者类型,它会编译。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。