Community
Participate
Working Groups
Hi folks, I've just started to play with generics, and I have a warning that I don't understand. I've reported it on the Sun Forums, and am assured it's an Eclipse bug! I'm using 3.1M3. I use the Visitor pattern extensively in my work, and I like to have my accept () methods take an additional object which gets passed to the visitor implementation as an additional argument along with the visited object. Typically, I make this parameter type Object, and so I figured I could use generics to tighten this up a bit. Here are my efforts (sorry if the formatting is bad - this is my first forum post): public interface Visitor<R, T> { public R visitBase(AbstractBase ab, T obj); public R visitDerived(Derived d, T obj); } public interface Visitable { public <R, T> R accept(Visitor<R, T> v, T obj); } public class AbstractBase implements Visitable { public <R, T> R accept(Visitor<R, T> v, T obj) { return v.visitBase(this, obj); } } public class Derived extends AbstractBase implements Visitable { public <R, T> R accept(Visitor<R, T> v, T obj) { return v.visitDerived(this, obj); } } public class Test { public static void main(String[] args) { AbstractBase ab = new AbstractBase(); Derived d = new Derived(); AbstractBase ab2 = new Derived(); Visitor<String, String> v = new MyVisitor(); System.out.println(ab.accept(v, ab.getClass().getName())); // "Visited base: AbstractBase" System.out.println(d.accept(v, d.getClass().getName())); // "Visited derived: Derived" System.out.println(ab2.accept(v, ab2.getClass().getName())); // "Visited derived: Derived" // ab2.accept(v, new Object()); // Correctly fails to compile } public static class MyVisitor implements Visitor<String, String> { public String visitBase(AbstractBase ab, String obj) { return "Visited base: " + obj; } public String visitDerived(Derived d, String obj) { return "Visited derived: " + obj; } } } The warning Eclipse gives is on both the AbstractBase and Derived classes. It highlights the return type R on the accept() method public <R, T> __R__ accept(...) and says: "Type safety: The return type R of the method accept(Visitor<R, T>, T) of type AbstractBase [or Derived] needs unchecked conversion to conform to the return type R of inherited method" Many thanks, Dan.
Added MethodVerify test 20
Reopen. There are still two warnings on the return type of the visit method: Type safety: The return type String of the method visitBase(AbstractBase, String) of type Test.MyVisitor needs unchecked conversion to conform to the return type R of inherited method Type safety: The return type String of the method visitDerived(Derived, String) of type Test.MyVisitor needs unchecked conversion to conform to the return type R of inherited method javac doesn't issue these warnings.
closed
Verified in 200412142000
3.1M4 still exhibits the bug shown in comment #2
isn't this a dup of https://bugs.eclipse.org/bugs/show_bug.cgi?id=79687 ???
Yes the warnings were taken care of with the fix for bug 81618.
Verified in I20050215-2300