Chapter 8. Effective Generics

This chapter contains advice on how to use generics effectively in practical coding. We consider checked collections, security issues, specialized classes, and binary compatibility. The title of this section is an homage to Joshua Bloch’s book, Effective Java (Addison-Wesley).

Take Care when Calling Legacy Code

As we have seen, generic types are checked at compile time, not run time. Usually, this is just what we want, since checking at compile time reports errors earlier and incurs no runtime overhead. However, sometimes this may not be appropriate, either because we can’t be sure that compile-time checks are adequate (say, because we are passing an instance of a parameterized type to a legacy client or to a client we don’t trust), or because we need information about types at run time (say, because we want a reifiable type for use as an array component). A checked collection will often do the trick, and when that will not do, we can create a specialized class. We consider checked collections in this section, security issues in the next section, and specialized classes in the section after that.

Consider a legacy library that contains methods to add items to a given list and to return a new list containing given items:

class LegacyLibrary {
  public static void addItems(List list) {
    list.add(new Integer(1)); list.add("two");
  }
  public static List getItems() {
    List list = new ArrayList();
    list.add(new Integer(3)); list.add("four");
    return list;
  }
}

Now consider ...

Get Java Generics and Collections now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.