I’m writing a template class which should take an array as its type.
public class Foo<T> {...}
How best can I enforce that “T” is an array type? (int[], Bar[], …) Preferably compile time if possible, obviously, but otherwise what is the best way at debug run time to throw an exception or something if T is not an array?
It is not possible to do it exactly how you have asked and have the generic type specify an array or primitive.
The syntax allows for things like:
Where az09$_ is any valid identifier that may also be parameterized with an identically formatted generic type.
But az09$_ is restricted to being a java identifier, so you cannot do
public class Foo<T[]> {...}any more than you can dopublic class Foo[] {...}.Typically you would do what you are after in your usage of T, i.e: