2 Replies Latest reply on Aug 13, 2010 2:39 AM by 843793

    Are bridge methods generated for generic methods?

      I've recently come across the notion of bridge methods. They provide type safety while allowing for erasure. However, the only places where they've been mentioned is with your class extending a parameterized type. That is the only case mentioned with bridge methods in the JLS as well as:
      http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#Under which circumstances is a bridge method generated?

      So I've been wondering about generic methods. For example, Collections.max's signature looks like the following in the source:

      public static <T extends Object & Comparable<? super T>> T max(Collection<T> coll)

      and after erasure:

      public static Object max(Collection coll)

      However, one cannot simply pass any type of Collection to the max method, it must implement Comparable and whatnot. Therefore, my question is how does it do that? Mustn't there be some sort of bridge method or generic information in the class file? Else how can the compiler, by just looking at the erasure of generic methods, check type safety, do capture conversion, type inference, etc?