Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics and varargs

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?

like image 971
Chris Avatar asked Jun 22 '10 20:06

Chris


People also ask

Is it good to use varargs in Java?

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.

What is Java Varargs?

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 }

How do you handle varargs in Java?

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.

Is Java Varargs an array?

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.


2 Answers

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 :(

like image 71
Jon Skeet Avatar answered Sep 21 '22 13:09

Jon Skeet


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.

like image 32
Cowan Avatar answered Sep 19 '22 13:09

Cowan