JDK-5088624 : cannot find symbol message should be more intelligent
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-18
  • Updated: 2010-10-13
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b114Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description

Name: dk106046			Date: 08/18/2004

Testcase :

bash-2.05b$ cat Test.java
import java.util.List;
import java.util.LinkedList;

public class Test
{
        public static void main(String [] args)
        {
                List<Integer> list = new LinkedList<Integer>();
                list.add(1);
                list.add(2);
                list.add(3);
                list.add("4");
                for(int i : list){
                        System.out.println(i);
                };
        }
}

	On compilation, I get :

bash-2.05b$ javac Test.java
Test.java:12: cannot find symbol
symbol  : method add(java.lang.String)
location: interface java.util.List<java.lang.Integer>
                list.add("4");
                    ^
1 error

That compilation fails, is as expected. However, this is a "C++ style" message. In C++, doing List<Integer> ends up creating a new datatype of a List container with Integer elements. So, when trying to stick a String into the container, the compiler would complain that the variable isnt of the right type (or in this case, that the symbol in question doesnt exist where the symbol being searched for is the container for a String). 
In Java, however, type instantiation does not happen : at least so claim the docs. Hence, a more intelligent message is expected along the lines of "type mismatch" or something like that. After all, isn't that the point of generics - to introduce type safety to containers?

======================================================================

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/77cc34d5e548
18-09-2010

EVALUATION I'm working on a solution that lists all the inapplicable methods that are found by javac during the resolution process. This is part of the reworking of javac diagnostic system. Listing all possible alternatives makes the diagnostic way better as it gives the feeling that the compiler did found something, but that that something was not applicable to actual args.
02-07-2008

EVALUATION In general the error messages are confusing when overloading is involved. While it may appear to be a simple type mismatch since the programmer apparently intented invoking: boolean add(E o) How can you in general tell if the programmer didn't intent to invoke: boolean add(int index, E element) I don't see how Java can do a better job than C++, the only way to improve this error message would be to go the way of C++ and list all the methods with a similar name in the error message. However, we will consider if this can be improved for a future release. Please add comments about any suggestions you might have. ###@###.### 2004-08-18 In reply to JDC Comments: No points are missed here. The problem is that more than one method with the name add is found in the List interface. Replace E with Integer: boolean add(Integer o); boolean add(int index, Integer element); So how is the compiler (in general) expected to know which one of these methods the programmer intended to call? Let's change the example a bit: List<String> l = null; int i = 0; l.add(i); How should the compiler diagnose this? Did the programmer intend to call boolean add(String o) or boolean add(int index, String element). It is very hard to tell, which is why the GNU C++ compiler reports all possible matches in such a case. I find that style confusing so I'm in favor of keeping the existing diagnostic while looking for something better. ###@###.### 2004-08-20
20-08-2004