I'd like to implement a function with both generics and varargs.
public class Question { public static <A> void doNastyThingsToClasses(Class<A> parent, Class<? extends A>... classes) { /*** something here ***/ } public static class NotQuestion { } public static class SomeQuestion extends Question { } public static void main(String[] args) { doNastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK doNastyThingsToClasses(Question.class, SomeQuestion.class); // OK doNastyThingsToClasses(Question.class, Object.class, SomeQuestion.class); // compilation failure } }
The intention here is to assert that all parameters passed to this function are Class objects extending the Class given as the first parameter. So the two first lines of main method would compile and the 3rd one generates an error.
My question is: Why I get "Type safety : A generic array of Class is created for a varargs parameter" message for the first two lines?
Am I missing something here?
Additional question: how to redesign it to prevent this warning from being shown on every line calling "doNastyThingsToClasses" function? I can change it to "doNastyThingsToClasses(Class<A> parent, Class<?>... classes)" and get rid of the warnings but this also removes the compilation-time type checking --- not so good if I wanted to assure the right use of this function. Any better solution?
Varargs are useful for any method that needs to deal with an indeterminate number of objects. One good example is String. format . The format string can accept any number of parameters, so you need a mechanism to pass in any number of objects.
Varargs is a short name for variable arguments. In Java, an argument of a method can accept arbitrary number of values. This argument that can accept variable number of values is called varargs. The syntax for implementing varargs is as follows: accessModifier methodName(datatype… arg) { // method body }
Important Points regarding Varargs Before JDK 5, variable length arguments could be handled in two ways: One was using overloading, other was using array argument. There can be only one variable argument in a method. Variable argument (Varargs) must be the last argument.
Every time we use varargs, the Java compiler creates an array to hold the given parameters. In this case, the compiler creates an array with generic type components to hold the arguments. The varargs usage is safe if and only if: We don't store anything in the implicitly created array.
As almost always, Angelika Langer's Java generics FAQ explains it in great detail. (Scroll to "Why does the compiler sometimes issue an unchecked warning when I invoke a "varargs" method?" - the ID doesn't work well.)
Basically, you end up losing information in a worse way than normal. Yet another little pain point in Java generics :(
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With