4

After refactoring of legacy code and start working with generics, I found that functions that looked like this:

T[] splitXXX() {
   //blah blah
}

Produces plenty of class cast excpetions, since the jdk doesn't really support arrays of generic types. And I wonder - why is this code compiles cleanly in java? Does it have something to do with backward compatibility? (it would have saved me a lot of investigation time if I could have found this errors at compile time and not at runtime). What am I missing?

axelrod
  • 3,404
  • 1
  • 16
  • 23
  • I know what I'm missing: the rest of the code, especially the point where you get the class cast exception. Generic arrays usually work fine – biziclop Dec 04 '12 at 18:36
  • 3
    I think it would be a better question if you provided a simple but complete example that compiles but fails at runtime. This way the readers would not have to guess what it is exactly that you have in mind. – NPE Dec 04 '12 at 18:37
  • possible duplicate of [Generic Arrays in Java](http://stackoverflow.com/questions/1817524/generic-arrays-in-java) – Bohemian Dec 04 '12 at 18:40
  • It should be producing unchecked conversion warnings at compile time. Probably either they are switched off or someone went around and put @SuppressWarnings("Unchecked") all over the place. – Affe Dec 04 '12 at 18:43

2 Answers2

0

If a type variable only appears in a return type, it is pretty dangerous. For example

public static <T> T foo(){ ... }

// usage
String s = foo();
Integer i = foo();

The compiler thinks, well if the programmer assigns T to String, he probably knows what he's doing, so it's pretty safe to infer that T=String. Heck, there is really no restriction at all, result of foo() can be assigned to any type.

But the programmer might not always know what he's doing; he relies a strongly typed compiler to catch some typing mistakes that he can inadvertently commit.

irreputable
  • 42,827
  • 9
  • 59
  • 89
0

Just because you cannot do new T[...] does not mean you cannot have a variable with type T[], just like just because you cannot do new T() does not mean you cannot have a variable with type T.

It's perfectly possible to have correct code that has methods that return T[]:

class Something<T> {
    T[] foo;
    Something(T[] in) { foo = in; }
    T[] splitXXX() { return foo; }
}

Produces plenty of class cast excpetions,

Well, if it produces exceptions, it means you are doing something wrong. You didn't show us what code is producing exceptions or anything, but you are probably doing casts that are not valid in your code.

newacct
  • 110,405
  • 27
  • 152
  • 217
  • But that's exactly what generics stand for- to prevent such errors at compile time. If this kind of code (array creation) can possibly produce exception at runtime, why does java allow them at all? – axelrod Dec 05 '12 at 09:37
  • @axelrod: I did not say "array creation". I don't know what you are doing in your code. Returning an array does not mean "array creation". There is nothing wrong in generics of returning an array. If you are doing array creation in your code and then casting it, then that cast is wrong. Generics does prevent type errors at compile time, but not when you are circumventing the type system by casting things. – newacct Dec 05 '12 at 09:44
  • But if you have a method with generic array return type like T[], someone had to create this array in order to return it from the method, and since generic array creation isn't allowed in java, it had to be done by casting - which can cause unsafe situations – axelrod Dec 05 '12 at 09:52
  • @axelrod: 1. if your method was written differently, it could take in an argument of type `T[]` and either return that or use that to create the array to return. 2) your `splitXXX` method is not a generic method (it does not declare `T`), so it is part of a generic class parameterized on `T`. Like I showed above, the generic class could take in a `T[]` from the outside in a constructor or another method. So it doesn't have to create it, or it could create it based on that array. – newacct Dec 05 '12 at 18:43