2

I am currently upgrading my RCP project to Neon and have hit the following problem.

It seems that generics have been added to the JFace databinding which has resulted in new method signatures.

Previously I was able to do

List<AbstractTestModule> modules = getModules();
IObservableList obs = Properties.selfList(AbstractTestModule.class).observe(modules);
viewer.setInput(obs);

I get a compile error because the observe method now expects List<Object>and modules cannot be automatically cast from List<AbstractTestModule> to List<Object>.

The docs are here: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore%2Fdatabinding%2Fproperty%2FProperties.html

Is there a way to do such a cast or is there a different strategy I could use?

paul
  • 13,312
  • 23
  • 81
  • 144
  • `List objects = new ArrayList<>(modules);`. – Andy Turner Sep 02 '16 at 12:53
  • Is `observe` in one of your classes? Can you make it accept `List>` instead of `List`? – Andy Turner Sep 02 '16 at 12:54
  • I think loading the list into a new ArrayList will prevent it from being observable – paul Sep 02 '16 at 13:01
  • no, `observe` is a method in JFace databinding – paul Sep 02 '16 at 13:04
  • I'm having surprising difficulty finding the API docs for the current release of JFace, or indeed for any release that uses generics. I find only archived docs for non-generic versions. Inasmuch as an answer to your question may depend on details of the API you're using, it would be helpful if you can post the documented signatures for all the JFace methods involved. Also, present the exact text of the error message -- the details are sometimes key. – John Bollinger Sep 02 '16 at 13:15

1 Answers1

1

You need to specify the generic class to use as the compiler can't infer it:

IObservableList obs = Properties.<AbstractTestModule>selfList(AbstractTestModule.class).observe(modules);
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • It really can't just infer that from the parameter...? – Andy Turner Sep 02 '16 at 13:01
  • @AndyTurner No because the parameter is just declared as `Object`, there is nothing to say it has anything to do with the generic type. They could have done it by breaking the backwards compatibility. – greg-449 Sep 02 '16 at 13:02
  • Thanks - that solved it. I have never seen that construction before. Can you point me at the documentation for that? – paul Sep 02 '16 at 13:21
  • 1
    It is called a 'type witness'. [This page](https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html) of the Java tutorial mentions it. – greg-449 Sep 02 '16 at 13:29