Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 353085

Summary: [1.7] Cannot cast from Object to boolean
Product: [Eclipse Project] JDT Reporter: Volker Berlin <volker.berlin>
Component: CoreAssignee: 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:
Description Flags
proposed fix
none
proposed fix v2.0
none
Proposed fix + regression tests
none
fix v2.1 + more tests + comments
none
patch v2.2 none

Description Volker Berlin CLA 2011-07-26 05:32:58 EDT
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;
	}
}
Comment 1 Volker Berlin CLA 2011-07-26 05:52:10 EDT
In my compiler error/warnings settings I have no errors. I have only ignore and warnings.
Comment 2 Ayushman Jain CLA 2011-07-26 06:08:55 EDT
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. :(
Comment 3 Ayushman Jain CLA 2011-07-26 06:10:20 EDT
Olivier/Srikanth, you know of any section in the spec that talks about why this cast is allowed now?
Comment 4 Olivier Thomann CLA 2011-07-26 15:31:12 EDT
This seems to have changed. Check the table 5.1 in the latest JLS in chapter 5.5.
Comment 5 Olivier Thomann CLA 2011-07-26 15:35:32 EDT
We should use the source level to decide if this is legal or not.
Comment 6 Ayushman Jain CLA 2011-07-27 09:50:16 EDT
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?
Comment 7 Olivier Thomann CLA 2011-07-27 10:08:48 EDT
(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.
Comment 8 Ayushman Jain CLA 2011-07-27 12:07:07 EDT
(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
Comment 9 Olivier Thomann CLA 2011-07-27 12:43:04 EDT
I think we need to handle more cases in the generation of implicit conversion.
Comment 10 Ayushman Jain CLA 2011-07-27 13:11:51 EDT
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.
Comment 11 Olivier Thomann CLA 2011-07-27 21:30:19 EDT
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.
Comment 12 Ayushman Jain CLA 2011-07-28 02:44:26 EDT
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.
Comment 13 Olivier Thomann CLA 2011-07-28 10:33:02 EDT
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.
Comment 14 Ayushman Jain CLA 2011-07-28 14:06:12 EDT
Created attachment 200548 [details]
patch v2.2

Done above mentioned changes.
Comment 15 Ayushman Jain CLA 2011-08-01 01:41:57 EDT
Released in HEAD for 3.8M1 and in R3_7_maintenance for 3.7.1
Comment 16 Srikanth Sankaran CLA 2011-08-25 05:10:21 EDT
Verified for 3.7.1 RC2 using build id: M20110824-0800
Comment 17 Olivier Thomann CLA 2011-09-14 11:18:22 EDT
Verified for 3.8M1.