четверг, 11 марта 2010 г.

Unexpected generics behaviour

If someone knows what the fuck please suggest!




public class Test {

  public interface EntityObject {

  }


  public interface SomeInterface {

  }


  public class BasicEntity implements EntityObject {

  }


  public interface BuisnessObject<E extends EntityObject> {

    E getEntity();

  }





  public interface ComplexObject1<V extends SomeInterface> extends BuisnessObject<BasicEntity> {

  }


  public interface ComplexObject2 extends BuisnessObject<BasicEntity> {

  }


   public void test(){

    ComplexObject1 complexObject1 = null;

    ComplexObject2 complexObject2 = null;

    EntityObject entityObject1 = complexObject1.getEntity();

    //BasicEntity entityObject1 = complexObject1.getEntity(); wtf incompatible types!!!!

    BasicEntity basicEntity = complexObject2.getEntity();

  }

}







5 комментариев:

  1. Not sure what is wrong, but this seems to fix the problem:

    ComplexObject1 complexObject1 = null;

    ОтветитьУдалить
  2. Blogspot ate up my angle brackets

    ComplexObject1<SomeInterface> complexObject1 = null;

    ОтветитьУдалить
  3. Thanks Shams, yes your solution works, but the thing I realy want is to use ComplexObject1 in most general way, and the thing I realy missed is why defined generic type(... extends BuisnessObject) is lost

    ОтветитьУдалить
  4. Another discussions
    http://stackoverflow.com/questions/2445516/unexpected-generics-behaviour

    http://community.livejournal.com/ru_java/913723.html#cutid1

    ОтветитьУдалить
  5. Finally, I have decided this is bug - compiler cleans all generics when raw type is used. Remedy is to use ComplexObject1 &lt?> complexObject1 = null;
    I posted this as bug on sun, hope they will fix problem.

    ОтветитьУдалить