0

I have the following code:

public class DEF implements Set<ABC> {

   private EnumSet<ABC> xyz=EnumSet.noneOf(ABC.class);

   @Override
   public <T> T[] toArray(T[] a) {
       return xyz.toArray(a);
   }

}

Which gives me the following warning:

Array of type 'ABC[]' expected at line 43

Is this dangerous? Or can I ignore it? Why is this warning?

JohnyTex
  • 3,323
  • 5
  • 29
  • 52

1 Answers1

2

The warning is completely correct; your code makes no sense.

By writing <T> T[] toArray(), you've made a function that can be called with any type parameter and will return an array of that type.
Always returning an array of ABCs violates what you claimed it will do.

In short, that function should not be generic.


None of this is applicable to your case, because the base Set<E> interface requires this.

I have no idea why Set<E> is declared that way, but there is nothing you can do about the warning.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • But if I override the method, then I can not change the method signature, can I? – JohnyTex Feb 19 '14 at 17:32
  • 1
    @user2254314: You're right; I have no idea why the base version is generic. In this case, you have no choice but to ignore the warning. – SLaks Feb 19 '14 at 17:33
  • If you re-edit your answer I will upvote it and check it as an answer. – JohnyTex Feb 19 '14 at 17:37