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

Bug 560024

Summary: [Compiler] "Cannot convert from Object to boolean" with Eclipse compiler, but no errors with javac
Product: [Eclipse Project] JDT Reporter: Juergen Baier <baier>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.14   
Target Milestone: 4.15 M3   
Hardware: PC   
OS: Mac OS X   
Whiteboard:

Description Juergen Baier CLA 2020-02-11 09:45:09 EST
Here is a small test program which can be compiled with javac:

$ javac --version
javac 11.0.5

but prints a compilation error with Eclipse 4.14.0:


public class CastToBoolean {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Foo foo = new FooImpl(Boolean.TRUE);

		Boolean value = foo.getAttribute();
		if (value) {
			System.out.println("this works");
		}

		if (foo.getAttribute()) { // <== Eclipse: Type mismatch: Cannot convert from Object to boolean 
			System.out.println("this does NOT work");
		}
	}

	static interface Foo {
		<R> R getAttribute();
	}

	static class FooImpl implements Foo {

		private final Object value;

		public FooImpl(Object value) {
			this.value = value;
		}

		@SuppressWarnings("unchecked")
		@Override
		public <R> R getAttribute() {
			return (R) value;
		}

	}
}

Expected behavior: The Eclipse compiler should not show an error message.
Comment 1 Stephan Herrmann CLA 2020-02-11 10:06:34 EST

*** This bug has been marked as a duplicate of bug 521939 ***