Community
Participate
Working Groups
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); } } }
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.
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
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.
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.
Changes in the ForEachStatement and ForEachStatementTest.
*** Bug 72754 has been marked as a duplicate of this bug. ***
Verified in I200409240100.