Alex Rivera | Logout

Java generics and varargs

Asked 2010-06-22T20:04:36.593
38

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?

Edit
Report

3 Answers

42

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

answered 2010-06-22T20:12:32.237
6

My solution to this problem was to

  1. create a class Nastier
  2. remove ... from doNastyThingsToClasses
  3. make doNastyThingsToClasses none static method
  4. make the name short, like do
  5. return this
  6. move repetitive args to class properties

    class Nastier {
      private final Class<A> parent;
    
      public Nastier(Class<A> parent) {
         this.parent = parent;
      }
    
      public <A, C extends A> Nastier do(Class<? extends A> clazz) {
         System.out.println(clazz);
         return this;
      }
    }
    
    public static void main(String[] args) {   
      Nastier nastier = new Nastier(Object.class);
      nastier.do(Question.class).do(SomeQuestion.class).do(NotQuestion.class);
    }
    

I believe the code looks clean and I am happy.... :)

answered 2011-09-25T17:05:44.383
-1

The second argument Class<? extends A>... that must extend the class that the first argument is (ex. argument one is a Question so the second argument be something that extends Question.

The Breakdown:
NastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK
Everything extends Object so the second argument is correct.

NastyThingsToClasses(Question.class, SomeQuestion.class); // OK
SomeQuestion extends Question so thats fair game.

NastyThingsToClasses(Question.class, Object.class, SomeQuestion.class);
Object does not extend Question hence error.


hopefully that cleared things up.

-Brett

answered 2010-06-22T20:15:38.907

Your Answer