简体   繁体   English

避免在参数化类型转换中未选中的强制转换警告

[英]Avoid Unchecked Casting Warnings in Parameterized Type Casting

Is it possible to avoid the unchecked warning when typecasting a parameterized type object? 在对参数化类型对象进行类型转换时是否可以避免unchecked警告? For example, below is the actual situation I face, var1 is of type JComboBox<RandomType> I wish to store it in a Map and then retrieve it forcing me into the following situation. 例如,下面是我面临的实际情况, var1的类型是JComboBox<RandomType>我希望将它存储在Map然后检索它,迫使我进入以下情况。

    JComboBox<RandomType> var1 = new JComboBox<RandomType>();

    Object varRaw = var1;

    JComboBox<RandomType> var2 = (JComboBox<RandomType>) varRaw;

    JComboBox<RandomType> var3;
    if (JComboBox.class.isAssignableFrom(varRaw.getClass())) {
        var3 = JComboBox.class.cast(varRaw);
    }

Both the regular cast and the conditional casting results in the same warning. 常规演员表和条件演员表都会产生相同的警告。

Is it even possible to avoid that warning in this situation without having to use the @SuppressWarnings annotation? 在不必使用@SuppressWarnings注释的情况下,甚至可以避免在这种情况下发出警告吗?

My answer is NO, there is not a better alternative (that i know). 我的答案是否定的,没有更好的选择(我知道)。 But I am not an authoritative source and seems unwieldy to prove that EVERY other alternative does not arise a 'false' warning. 但我并不是一个权威的来源,并且似乎难以证明每个其他选择都不会出现“错误”警告。

I have come to a point of assuming the lesser evil of having some warnings around (yes there is @SupressWarnings unchecked but I would like to know if something noteworthy arises). 我已经到了假设周围有一些警告的较小的邪恶(是的@SupressWarnings unchecked但我想知道是否有值得注意的事情)。 For the same reason i would frown upon the javac -Xlint:unchecked non-standard option 出于同样的原因,我会对javac -Xlint:unchecked的非标准选项javac -Xlint:unchecked

Other sources also say it is not possible to supress unchecked warnings in all cases (particularly when dealing with libraries not fully adapted to generics). 其他消息来源还表示,在所有情况下都不可能抑制未经检查的警告(特别是在处理不完全适应仿制药的图书馆时)。 For example in this Generics tutorial the author says 例如,在这个泛型教程中 ,作者说

Is it possible to eliminate all "unchecked" warnings? 是否有可能消除所有“未经检查”的警告? - Almost - 几乎

And then she goes into some unwieldy technical explanation that overwhelm me. 然后她进入了一些不堪重负的技术解释。 Therefore I don't know if what she says counts as proof of what. 因此,我不知道她所说的内容是否可以证明是什么。

I just know I can't prove unicorns don't exist. 我只知道我无法证明独角兽不存在。 But if I don't have notice of anyone actually registering their existence, it is fair that i assume they don't. 但如果我没有注意到任何人实际登记他们的存在,我认为他们没有。 That's Falsafiability , just take this wicked technical problem instead of stupid unicorns. 这是可靠性 ,只是采取这个邪恶的技术问题,而不是愚蠢的独角兽。

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

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