如何避免使用Java泛型进行未选中的强制转换警告 [英] How to avoid unchecked cast warnings with Java Generics

查看:1453
本文介绍了如何避免使用Java泛型进行未选中的强制转换警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知怎的,我的旧问题已关闭,所以我打开一个新的:

Somehow my old question was closed, so I open a new one:

我使用Java泛型从SQL查询实现一个通用双向哈希贴图。它应该能够映射任何字符串,整数对的组合来回。它应该像这样使用:

I am using Java Generics to implement a generic bidirectional Hash Map out of an SQL Query. It should be able to map any combination of String, Integer pairs back and forth. It should be used like this:

String sql = "SELECT string_val, int_val FROM map_table";
PickMap<String, Integer> pm1 = new PickMap<String, Integer>(sql);

String key1 = "seven";
Integer value1 = pm1.getLeft2Right(key1);

Integer key2 = 7;
String value2 = pm1.getRightToLeft(key2);

当然,应该可以创建一个pm(Integer,Integer)

Of course it should be possible to create an pm (Integer, Integer) and so on...

我的实现Pick Map看起来像这样(没有getter ...):

My implementation of Pick Map looks like this (without the getter...):

public class PickMap<L, R> {

    private final HashMap<L, R> left2Right = new HashMap<L, R>();
    private final HashMap<R, L> right2Left = new HashMap<R, L>();

    public PickMap(String sql) throws OException {
        DTable d = new DTable(sql);
        int colTypeL = d.t.getColType(1);
        int colTypeR = d.t.getColType(2);
        Extractor<L> extLeft  = (Extractor<L>) getInstance(colTypeL);
        Extractor<R> extRight = (Extractor<R>) getInstance(colTypeR);    
        int numRows = d.t.getNumRows();
        for(int i=1;i<=numRows;i++) {
            L leftVal = extLeft.extract(d, i);
            R rightVal = extRight.extract(d, i);
            this.left2Right.put(leftVal, rightVal);
            this.right2Left.put(rightVal, leftVal);
        }
    }

    private Extractor<?> getInstance(int type) {
        if(type == 1)
            return new IntExtractor();
        else
            return new StringExtractor();
    }
}

interface Extractor<E> {
    E extract(DTable source, int row);
}

class IntExtractor implements Extractor<Integer> {

    @Override
    public Integer extract(DTable source, int row) {
        int value = 5;
        return new Integer(value);
    }
}

class StringExtractor implements Extractor<String> {

    @Override
    public String extract(DTable source, int row) {
        String retVal = "hello";
        return retVal;
    }
}



我没有编译器错误, ,它将工作这种方式。但我得到unchecked在getInstance方法上投放警告我在哪里我提取器(E)到Extractor(L)...

I have no compiler errors and I'm pretty sure, that it will work this way. BUT I'm getting unchecked cast warnings on the "getInstance" methods Where I cast Extractor(E) to Extractor(L)...

我应该如何正确投?或者我缺少什么?还是我应该禁止那些警告?

How should I cast properly? Or what am I missing? Or should I just suppress those warnings?

推荐答案

您收到警告,因为您所做的不能证明是安全的。您假定 getInstance(colTypeL)将返回提取器< L> 但不能在编译时或执行时验证。

You're getting warnings because what you're doing can't be proved to be safe. You're assuming that getInstance(colTypeL) will return an Extractor<L> - but that can't be verified at either compile-time or execution time.

您可以使用 @SuppressWarnings(unchecked)如其他人所提到的,但我会尝试重新思考设计有点。

You can use @SuppressWarnings("unchecked") as mentioned by others, but I would try to rethink the design somewhat.

这篇关于如何避免使用Java泛型进行未选中的强制转换警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆