Java generics and varargs

后端 未结 6 1950
走了就别回头了
走了就别回头了 2020-12-05 00:19

I\'d like to implement a function with both generics and varargs.

public class Question {
    public static  void doNastyThingsToClasses(Class

        
6条回答
  •  被撕碎了的回忆
    2020-12-05 00:56

    Jon Skeet's answer is (of course) correct; I'll expand on it a little by pointing out that you CAN get rid of this warning, with a big 'if'. You can avoid this warning IF you're willing to commit to having your project build using Java 7.

    Bob Lee wrote a proposal to let this warning be suppressed at method-declaration site, rather than usage site, as part of Project Coin.

    This proposal was accepted for JDK7 (though the syntax changed slightly, to @SuppressWarnings("varargs")); you can, if you're curious, look at the commit that added this support to the JDK.

    Not necessarily helpful for you, but I thought I'd make this a separate answer so it lives on for future readers, who may be lucky enough to live in a post-Java-7 world.

提交回复
热议问题