3 Replies Latest reply: Jun 28, 2010 8:05 AM by 796386 RSS

    IDE support for Generics

    796386
      Hi,
      All IDEs have a very useful feature where you can see what references what.
      Suppose I have a method:
      public void add(Person person){
      ..
      }
      I can easily see who calls it.

      However, suppose I am thinking of typing the method so I'd have:
      public <T extends Person> void add(T person) {
      ..
      }
      Does anyone know if any of the main IDEs support a search whereby I can search for methods calls of add() for this type Person (e.g. a Worker) and for that type Person (e.g. an Employer).

      Thanks
        • 1. Re: IDE support for Generics
          796386
          Hi,
          Just wondering can anyone answer this?
          Thanks
          • 2. Re: IDE support for Generics
            843793
            Wow, you've been waiting a long time! ;-)

            Yep, Eclipse will do this. I'll explain it using this example:
            public class Scratchpad {
                public static void main(String[] args) {
                    a(); b(); c();
                }
                
                private static void a() { method("hello"); }
                private static void b() { method(Integer.valueOf(5)); }
                private static void c() { method(new Object()); }
                
                private static <T> void method(T t) {}
            }
            The normal way to get references to method() is using CTRL-ALT-H while having the cursor on a declaration or reference to method(). This brings up the call heirarchy and will show matches to a, b, and c.

            Instead, what we want to do is put your cursor on method() and then use CTRL-H to bring up the search dialog. It should come up with the Java Search tab selected, with the default search text "Scratchpad.method(T)", and it should be automatically limited to References (that's what we want).

            If I were to run it like that, I would again get a(), b() and c() as matches. However, if I instead replace "T" with "String", I would only get a(). Likewise for "Integer" and b(), etc. You can use fully-qualified names to make it more precise.
            • 3. Re: IDE support for Generics
              796386
              That's pretty clever stuff. Thanks.