Bug 365818 - Generics compilation: error reported in javac, but not in Eclipse
Summary: Generics compilation: error reported in javac, but not in Eclipse
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7.1   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.8 M5   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-06 17:55 EST by Aled Sage CLA
Modified: 2012-01-23 08:29 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Aled Sage CLA 2011-12-06 17:55:59 EST
Build Identifier: 20110916-0149

I wrote some horrible generics (when using guava) that compiled in the Eclipse IDE. However, from javac at the command line, it fails to compile with the error message:

GenericsBug.java:9: <T>ofInstance(T) in mypackage.GenericsBug cannot be applied to <mypackage.GenericsBug.Cache<java.lang.Integer,? extends java.lang.String>>(mypackage.GenericsBug.Cache<java.lang.Object,java.lang.String>)
      Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance(
                                                                         ^
1 error

I would expect Eclipse to give me this same error.

Below is a simplified class with stripped down copies of the guava interfaces.

====
package mypackage;

import java.util.Map;

public class GenericsBug {
   public static void main(String[] args) {
      String image = "abc";
      Map<Integer, String> imageMap = GenericsBug.of(1, image);
      Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance(
               CacheBuilder.newBuilder().build(GenericsBug.from(GenericsBug.forMap(imageMap))));
   }

   public static <T> Supplier<T> ofInstance(T instance) {
      return null;
   }

   public interface Supplier<T> {
   }

   public abstract class CacheLoader<K, V> {
   }

   public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
      return null;
   }

   public interface Function<F, T> {
   }

   public interface Cache<K, V> extends Function<K, V> {
   }

   public static <K, V> Function<K, V> forMap(Map<K, V> map) {
      return null;
   }

   public static <K, V> Map<K, V> of(K k1, V v1) {
      return null;
   }

   public static class CacheBuilder<K, V> {
      public static CacheBuilder<Object, Object> newBuilder() {
         return null;
      }
      public <K1 extends K, V1 extends V> Cache<K1, V1> build(CacheLoader<? super K1, V1> loader) {
         return null;
      }
   }
}
====
Version: Indigo Service Release 1
Build id: 20110916-0149
javac 1.6.0_29


Reproducible: Always

Steps to Reproduce:
1. Paste this class in Eclipse; it compiles
2. Compile it using javac at the command line; it fails
Comment 1 Aled Sage CLA 2011-12-06 18:04:25 EST
the magic to get it to compile with javac is to add <Integer,String> for the call to build:

   public static void main(String[] args) {
      String image = "abc";
      Map<Integer, String> imageMap = GenericsBug.of(1, image);
      Supplier<Cache<Integer, ? extends String>> imageCache = GenericsBug.<Cache<Integer, ? extends String>> ofInstance(
               CacheBuilder.newBuilder().<Integer,String>build(GenericsBug.from(GenericsBug.forMap(imageMap))));
   }
Comment 2 Ayushman Jain CLA 2011-12-07 02:16:17 EST
This a javac bug which has been fixed now in java 7 (javac7b147). The code is legal, however horrible it looks :)
Comment 3 Satyam Kandula CLA 2012-01-23 08:29:35 EST
Verified for 3.8M5 using build I20120122-2000