5 Replies Latest reply: Oct 22, 2010 8:01 AM by 805654 RSS

    java do not compile generics

    805654
      declaration:

      ListUtil.java
      public static <T, F extends T, E extends T> List<T> addAll(List<F> list1, List<E> list2) {
      List<T> ret = new ArrayList<T>();
      if (list1 != null) {
      for (F f : list1) {
      ret.add(f);
      }
      }
      if (list2 != null) {
      for (E e : list2) {
      ret.add(e);
      }
      }
      return ret;
      }

      usage:
      List&lt;B&gt; bb = new ArrayList&lt;B&gt;();
      List&lt;C&gt; cc = new ArrayList&lt;C&gt;();
      List&lt;AAbstract&gt; ret = ListUtil.addAll(compounds, sensors);

      error:
      [javac] SensorInstanceMgmt.java:1113: <T,F,E>addAll(java.util.List<F>,java.util.List<E>) in util.ListUtil cannot be applied to (java.util.List<entity.B>,java.util.List<entity.C>)
      [javac] List<AAbstract> ret = ListUtil.addAll(compounds, sensors);

      It works in eclipse, but compiling by ant do not. I suspect some switch of javac - but do not know which.
      Of course eclipse and ant uses same javac, it has set same java, javac.target, javac.source as well.

      So why I compile with ant? I mention about ant but same situation will be with invoking javac from outside eclipse.

      Edited by: 802651 on 2010-10-14 23:19

      Edited by: 802651 on 2010-10-22 06:03
        • 1. Re: java do not compile generics
          791266
          Hi,

          Eclipse is not using the JDK compiler. Eclipse uses a compiler of its own. There's no javac switch that does what you are looking for.
          • 2. Re: java do not compile generics
            802316
            Different compilers handle generics slightly differently. Sun's compiler tends to a bit dumb when it comes to inferred types. I suggest you try the following which may help, or get a clearer error message.

            List<AAbstract> ret = ListUtil.<AAbstract>addAll(compounds, sensors);

            BTW: what is wrong with the List.addAll() method?
            public static <T, F extends T, E extends T> List<T> addAll(List<F> list1, List<E> list2) {
                List<T> ret = new ArrayList<T>();
                if (list1 != null) ret.addAll(list1);
                if (list2 != null) ret.addAll(list2);
                return ret;
            }
            • 3. Re: java do not compile generics
              800268
              Also the method can be further simplified to:
              public static <T> List<T> createCombinedList(
                Collection<? extends T> list1,
                Collection<? extends T> list2) {
              
                  List<T> result = new ArrayList<T>();
                  if (list1 != null) result.addAll(list1);
                  if (list2 != null) result.addAll(list2);
                  return result;
              }
              • 4. Re: java do not compile generics
                YoungWinston
                Walter Laan wrote:
                Also the method can be further simplified to:
                public static <T> List<T> createCombinedList(
                Collection<? extends T> list1,
                Collection<? extends T> list2) {
                ...
                Or even
                public static <T> List<T> combine(Collection<? extends T>... lists) {...
                Nice one Walter.

                Winston
                • 5. Re: java do not compile generics
                  805654
                  thx for replies but your optimization does not work because (of course except addAll which is correct)

                  Type mismatch: cannot convert from List<AAbstract&Comparable<?>> to List<AAbstract>, i forget to mention and I was not aware that derived class B,C implements Comparable, but parent class AAbstract does not

                  Edited by: 802651 on 2010-10-22 06:00