Bug 72560 - [1.5] VerifyError in nested generics loop
Summary: [1.5] VerifyError in nested generics loop
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.1 M2   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 72754 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-08-24 18:02 EDT by Joan Puig CLA
Modified: 2004-09-23 07:19 EDT (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 Joan Puig CLA 2004-08-24 18:02:28 EDT
I am trying to compile some code that uses nested generics. The editor finds no
problem while creating the method, and it compiles without any problem. The
issue is that once I try to run it, I get the following error:

Exception in thread "main" java.lang.VerifyError: (class:
pete/brokers/GlobalBroker, method: testFor signature: ()V) Incompatible object
argument for function call

if the line of code (System.out.println(s);) is removed, it works.


public void testFor(){
    	ArrayList<ArrayList<String>> slist = new ArrayList<ArrayList<String>>();
    	
    	slist.add(new ArrayList<String>());
    	slist.get(0).add("1 s1");
    	slist.get(0).add("1 s2");
    	slist.get(0).add("1 s3");
    	
    	slist.add(new ArrayList<String>());
    	slist.get(1).add("2 s1");
    	slist.get(1).add("2 s2");
    	slist.get(1).add("2 s3");
    	
    	for (int i=0; i<slist.size(); i++){
    		for (String s : slist.get(i)){
    			System.out.println(s);
    		}
    	}
    }
Comment 1 Olivier Thomann CLA 2004-08-24 20:56:09 EDT
The problem comes from the fact that the slist.get(i) expression is not cast
into a  ArrayList. So on the stack we have an Object. This is why we get the
exception.
Comment 2 Joan Puig CLA 2004-08-24 21:19:27 EDT
I just introduced the cast there and it works. This rises the question, is my
original code proper java code and compiler support is on the way? Is it bad
syntax in which case, an error should be issued, or just a bad practice in which
case maybe a warning would be better?

Thanks
Joan 
Comment 3 Olivier Thomann CLA 2004-08-25 14:25:05 EDT
I meant not cast in the bytecodes. Your code is perfectly fine without casting.
This is the idea of using generics. The problem is at the code generation level.
When a generic method is used, we should cast the result to the "expected type"
without needing to have an explicit cast in the code.
I will investigate if this could already be done.
Comment 4 Olivier Thomann CLA 2004-08-25 15:51:27 EDT
I found the problem. I missed to call the computeConversion on the slist.get(i)
in the foreach statement.
Fixed and released in HEAD.
Regression test added.
Comment 5 Olivier Thomann CLA 2004-08-25 16:19:04 EDT
Changes in the ForEachStatement and ForEachStatementTest.
Comment 6 Olivier Thomann CLA 2004-08-27 14:22:12 EDT
*** Bug 72754 has been marked as a duplicate of this bug. ***
Comment 7 David Audel CLA 2004-09-23 07:19:14 EDT
Verified in I200409240100.