JDK-6998697 : boolean expression deemed illegal - but only inside of an assertion
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2010-11-09
  • Updated: 2012-03-20
  • Resolved: 2010-12-01
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.1) (6b20-1.9.1-1ubuntu3)
OpenJDK 64-Bit Server VM (build 17.0-b16, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux notebook 2.6.35-22-generic #35-Ubuntu SMP Sat Oct 16 20:45:36 UTC 2010 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
a certain boolean expression involving generics (see code below) is deemed illegal when used inside of an assertion. When the expression is stored in a boolean variable instead, no error occurs.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Apply javac to the class given below.

$ javac AClass.java


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
a clean compile.
ACTUAL -
a compilation error

ERROR MESSAGES/STACK TRACES THAT OCCUR :
AClass.java:18: illegal start of expression
		assert(AClass.<Integer> mk().isX());
		                           ^
1 error


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public abstract class AClass<N> {

	public abstract boolean isX();

	public static <K> AClass<K> mk() {
		return new AClass<K> () {
			@Override
			public boolean isX() {
				return true;
			}
		};
	}

	public static void test() {
		assert(AClass.<Integer> mk().isX());
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
public abstract class AClass<N> {

	public abstract boolean isX();

	public static <K> AClass<K> mk() {
		return new AClass<K> () {
			@Override
			public boolean isX() {
				return true;
			}
		};
	}

	public static void test() {
		boolean x = AClass.<Integer> mk().isX();
		assert x;
	}
}