简体   繁体   English

如何使用Java Generics避免未经检查的强制转换警告

[英]How to avoid unchecked cast warnings with Java Generics

Somehow my old question was closed, so I open a new one: 不知怎的,我的旧问题已经关闭,所以我开了一个新问题:

I am using Java Generics to implement a generic bidirectional Hash Map out of an SQL Query. 我正在使用Java Generics从SQL查询中实现通用双向哈希映射。 It should be able to map any combination of String, Integer pairs back and forth. 它应该能够来回映射String,Integer对的任意组合。 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);

Of course it should be possible to create an pm (Integer, Integer) and so on... 当然应该可以创建一个pm(整数,整数)等等......

My implementation of Pick Map looks like this (without the getter...): 我对Pick Map的实现看起来像这样(没有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;
    }
}

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)... 但是我在“getInstance”方法上得到了未经检查的强制转换警告,其中我将Extractor(E)转换为Extractor(L)......

How should I cast properly? 我该如何正确演员? Or what am I missing? 或者我错过了什么? Or should I just suppress those warnings? 或者我应该压制这些警告?

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. 假设 getInstance(colTypeL)将返回Extractor<L> - 但无法在编译时或执行时验证。

You can use @SuppressWarnings("unchecked") as mentioned by others, but I would try to rethink the design somewhat. 您可以使用其他人提到的@SuppressWarnings("unchecked") ,但我会尝试重新考虑一下这个设计。

You can use the following annotation to make the compiler not output those warnings: 您可以使用以下注释使编译器不输出这些警告:

@SuppressWarnings("unchecked")

See this related question which deals with the same issue. 请参阅此相关问题 ,该问题涉及同一问题。 The answer there will explain everything you need to know. 答案将解释您需要知道的一切。

If you are using the Spring Framework, you can use CastUtils: 如果您使用的是Spring Framework,则可以使用CastUtils:

import static org.springframework.data.util.CastUtils.cast;
obj.setString(cast(someObject));
String bob = cast(someObject);

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

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