| Summary: | [1.7] Cannot cast from Object to boolean | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Volker Berlin <volker.berlin> | ||||||||||||
| Component: | Core | Assignee: | Ayushman Jain <amj87.iitr> | ||||||||||||
| Status: | VERIFIED FIXED | QA Contact: | |||||||||||||
| Severity: | normal | ||||||||||||||
| Priority: | P3 | CC: | amj87.iitr, Olivier_Thomann, remy.suen, srikanth_sankaran | ||||||||||||
| Version: | 3.7 | ||||||||||||||
| Target Milestone: | 3.7.1 | ||||||||||||||
| Hardware: | PC | ||||||||||||||
| OS: | Windows 7 | ||||||||||||||
| Whiteboard: | |||||||||||||||
| Attachments: |
|
||||||||||||||
In my compiler error/warnings settings I have no errors. I have only ignore and warnings. I think this is the same as bug http://bugs.sun.com/view_bug.do?bug_id=7038363. Fixed in javac 7b147, but not in ECJ. :( Olivier/Srikanth, you know of any section in the spec that talks about why this cast is allowed now? This seems to have changed. Check the table 5.1 in the latest JLS in chapter 5.5. We should use the source level to decide if this is legal or not. Created attachment 200449 [details]
proposed fix
The patch makes the primitive to Object conversions possible. Also works fine for all legal casts such as
public class Conversion {
public static void main ( String[] args ) {
Object a = new Integer (1);
int abc = (int) a;
System.out.println (abc);
}
}
However, for
public class Conversion {
public static void main ( String[] args ) {
Object a = new Float (1.123);
int abc = (int) a;
System.out.println (abc);
}
}
I get a verifyError on running the code. While in java command line, i get a CCE. Olivier, does something more need to be done for codegen?
(In reply to comment #6) > I get a verifyError on running the code. While in java command line, i get a > CCE. Olivier, does something more need to be done for codegen? I'll take a look at the patch. You probably need to insert a checkcast bytecode. (In reply to comment #7) > (In reply to comment #6) > > I get a verifyError on running the code. While in java command line, i get a > > CCE. Olivier, does something more need to be done for codegen? > I'll take a look at the patch. > You probably need to insert a checkcast bytecode. I think there's more. We may also need an unboxing conversion code here I think we need to handle more cases in the generation of implicit conversion. Created attachment 200462 [details]
proposed fix v2.0
This patch adds more implicit conversion id's in TypeIds.java ie. Object2X where X = int, byte, short, etc... as per the table 5.1 in JLS 5.5
A check cast and an unboxing conversion is generated for any such conversion.
Created attachment 200482 [details]
Proposed fix + regression tests
Patch based on the previous one with a fix for char, short and byte and some cleanup.
Ayushman, please review.
Created attachment 200492 [details]
fix v2.1 + more tests + comments
Thanks Olivier.
Same fix as above, added more tests to check negative cases where JVM issues a CCE, and added a test to check bytecode gen.
A few comments. Try to catch the CCE in the code and print something predictable. Printing the stacktrace directly can create failures on different VMS. Also remove the new test from ConformTest and move the check for disassembled code into one of the cast test. Everything else is fine. Thanks for the patch. Created attachment 200548 [details]
patch v2.2
Done above mentioned changes.
Released in HEAD for 3.8M1 and in R3_7_maintenance for 3.7.1 Verified for 3.7.1 RC2 using build id: M20110824-0800 Verified for 3.8M1. |
The follow small test case show the compiler error "Cannot cast from Object to boolean". But it compile with the Java 7 compiler. The same problem occur also with all other native types like int and char. This construct is used in the Java 7 sources. For example in sun.invoke.util.ValueConversions public class CompilerTest { public static void main(String[] args) { Object x = Boolean.TRUE; boolean y = (boolean)x; } }