Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created July 19, 2010 20:22
Show Gist options
  • Save gamlerhart/481935 to your computer and use it in GitHub Desktop.
Save gamlerhart/481935 to your computer and use it in GitHub Desktop.
// Even a cast generates a warning
List<MyClass> data = (List<MyClass>) legacyLibraryCall();
private static <TTarget,TSource> TTarget cast(TSource source){
return (TTarget)source;
}
// generates an unchecked warning
List<MyClass> data = legacyLibraryCall();
private static <T> List<T> newList(){
return new ArrayList<T>();
}
public static void main(String[] args) {
// Java infers the type T for the generic method newList
List<MyClass> data = newList();
}
Object result = anOperation();
MyClass data = (MyClass) result;
// Now we can use the cast-method. Note that this won't generate a warning
// because the return-type is inferred.
List<MyClass> data = cast(legacyLibraryCall());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment