Bug 157389 - [1.4][compiler] assertionStatus is not set properly for nested types
Summary: [1.4][compiler] assertionStatus is not set properly for nested types
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-14 15:47 EDT by Olivier Thomann CLA
Modified: 2006-09-19 00:26 EDT (History)
0 users

See Also:


Attachments
Proposed fix for 3.2 maintenance branch (1019 bytes, patch)
2006-09-14 15:49 EDT, Olivier Thomann CLA
no flags Details | Diff
Regression tests for 3.2 maintenance branch (1.54 KB, patch)
2006-09-14 17:19 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix for 3.2 maintenance branch (1.47 KB, patch)
2006-09-14 22:23 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix (1.59 KB, patch)
2006-09-14 23:04 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2006-09-14 15:47:25 EDT
Using latest, the assertion status is checked against the current class instead of the most enclosing type.
From http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10:

An assert statement is enabled if and only if the top-level class (§8) that lexically contains it enables assertions. Whether or not a top-level class enables assertions is determined by its defining class loader before the class is initialized (§12.4.2), and cannot be changed thereafter.

Try this test case:

public class X {
	static class Y {
		public static void test() {
			assert false;
			System.out.println("SUCCESS");
		}
	}
	public static void main(String[] args) {
		ClassLoader classLoader = new X().getClass().getClassLoader();
		// enable assertion for X.Y
		classLoader.setClassAssertionStatus("X$Y", true);
		X.Y.test();
	}
}

Then run it without enabling assertion at runtime:
java X

You will get an AssertionError. If the assertionStatus of the nested type would be retrieved from the most enclosing type, the assert statement should be ignored and the result should be:
"SUCCESS".
Comment 1 Olivier Thomann CLA 2006-09-14 15:49:45 EDT
Created attachment 50199 [details]
Proposed fix for 3.2 maintenance branch
Comment 2 Olivier Thomann CLA 2006-09-14 17:19:07 EDT
Created attachment 50208 [details]
Regression tests for 3.2 maintenance branch
Comment 3 Olivier Thomann CLA 2006-09-14 22:23:05 EDT
Created attachment 50224 [details]
Proposed fix for 3.2 maintenance branch

Better patch for 1.4.
It doesn't change anything for 1.6 or 1.5 mode.
Comment 4 Olivier Thomann CLA 2006-09-14 22:39:14 EDT
The first patch is the one to keep.
This case needs more work in 1.4 mode, but it is fine in 1.5 and 1.6 mode.
In 1.5 or 1.6 the ldc bytecode can be used to retrieve the class. In 1.4, we need to do some macro expansion.
Comment 5 Olivier Thomann CLA 2006-09-14 23:04:59 EDT
Created attachment 50225 [details]
Proposed fix

The first patch is allright in the 1.4 case. It puts the synthetic field on the enclosing type that uses the assert statement. The check for the desiredAssertionStatus is done on the most enclosing type anyway.
However we can move the search for the sourceType inside the then block of the if since it is used only if the condition is true. It doesn't change the behavior of the previous patch.
Comment 6 Olivier Thomann CLA 2006-09-14 23:07:19 EDT
Released for 3.3M2.
Regression test added org.eclipse.jdt.core.tests.compiler.regression.AssertionTest#test013.
Comment 7 Philipe Mulet CLA 2006-09-15 07:02:41 EDT
+1 for 3.2 maintenance (3.2.1)
Comment 8 Olivier Thomann CLA 2006-09-15 07:06:04 EDT
Released for 3.2.1.
Same regression test added.
Comment 9 Frederic Fusier CLA 2006-09-18 13:42:47 EDT
Verified for 3.2.1 using build M20060915-1045
Comment 10 Maxime Daniel CLA 2006-09-19 00:26:18 EDT
Verified for 3.3 M2 using build I20060918-0010.