Bug 78232 - [1.5][generics]Erroneous warning implementing generic method from interface
Summary: [1.5][generics]Erroneous warning implementing generic method from interface
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: 3.1 M5   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-09 16:56 EST by Danny Yates CLA
Modified: 2005-02-16 05:58 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Danny Yates CLA 2004-11-09 16:56:55 EST
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.
Comment 1 Kent Johnson CLA 2004-11-16 15:32:54 EST
Added MethodVerify test 20
Comment 2 Olivier Thomann CLA 2004-12-14 11:14:39 EST
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.
Comment 3 Olivier Thomann CLA 2004-12-15 12:02:15 EST
closed
Comment 4 Olivier Thomann CLA 2004-12-15 12:02:24 EST
Verified in 200412142000
Comment 5 Danny Yates CLA 2004-12-18 18:00:17 EST
3.1M4 still exhibits the bug shown in comment #2
Comment 6 Jed Wesley-Smith CLA 2005-01-11 01:16:54 EST
isn't this a dup of https://bugs.eclipse.org/bugs/show_bug.cgi?id=79687 ???
Comment 7 Kent Johnson CLA 2005-01-11 12:02:19 EST
Yes the warnings were taken care of with the fix for bug 81618.
Comment 8 David Audel CLA 2005-02-16 05:58:06 EST
Verified in I20050215-2300