Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5074427
Votes 0
Synopsis Foreach seeing wrong parameterized return type
Category java:compiler
Reported Against tiger-beta2
Release Fixed
State 11-Closed, Not a Defect, bug
Priority: 3-Medium
Related Bugs 6785612
Submit Date 15-JUL-2004
Description




FULL PRODUCT VERSION :
java version "1.5.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b57)
Java HotSpot(TM) Client VM (build 1.5.0-beta3-b57, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
 customer  Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The code segment  included will compile correctly only if the base class does not accept a generic type.  If a generic type is added to the base class, the compiler reports an error on the return method.


ERROR MESSAGES/STACK TRACES THAT OCCUR :
H:\Bud\Desktop>javac -source 1.5 -target 1.5 -Xlint Base.java
Base.java:12: incompatible types
found   : java.lang.Object
required: java.lang.String
      for(String str : b.getParams().keySet()) {
                                           ^
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public abstract class Base<E> {
   public abstract Map<String, String> getParams();
}

class Child extends Base<String> {

   public static void main(String[] args) {
      Base b = new Child();

      for(String str : b.getParams().keySet()) {
         System.out.println(str + " " + b.getParams().get(str));
      }

   }

   private Map<String, String> map;

   public Child() {
      map = new HashMap<String, String>();
      map.put("Hello", "World");
   }

   public Map<String, String> getParams() {
      return map;
   }

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Save the  returned set from keySet() to a variable, then use the variable in the for loop.  This will cause a warning to be produced when compiling with -Xlint:

javac -source 1.5 -target 1.5 -Xlint Base.java
Base.java:12: warning: [unchecked] unchecked conversion
found   : java.util.Set
required: java.util.Set<java.lang.String>
      Set<String> set = b.getParams().keySet();
                                            ^
1 warning
(Incident Review ID: 285795) 
======================================================================
Work Around
N/A
Evaluation
Not a bug, the declaration of b makes it raw:

  Base b = new Child();

Please change it to:

  Base<String> b = new Child();

  xxxxx@xxxxx   2004-07-15
Comments
  
  Include a link with my name & email   

Submitted On 10-OCT-2006
This is definitely a problem.

 	    Set<String> keySet = configMap.keySet();

Gets the same warning:

ClassName.java:58: warning: [unchecked] unchecked conversion
found   : java.util.Set
required: java.util.Set<java.lang.String>
 	    Set<String> keySet = configMap.keySet();


Submitted On 14-APR-2009
ricky_clarkson
There is no reason that using a raw type should cause other generic methods of that type that do not use the type's type parameters to also return raw types.  Please read the original bug report more carefully.

Another test case, if cryptic:

import java.util.List;
class A<T> { List<A> a() { throw null; } }
class B { { A a = new A<Float>(); for (A a: a.a()); } }



PLEASE NOTE: JDK6 is formerly known as Project Mustang