Assertions in static blocks ?
Keith McGuigan
Keith.McGuigan at Sun.COM
Fri Feb 12 07:12:58 PST 2010
Hi Ulf -
Accessing a constant static field in a class does not trigger class
initialization, so your initializer is probably just not being run.
See
http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#19075
--
- Keith
Ulf Zibis wrote:
> Hi,
>
> in the following example, I have an assert statement in a static block
> of my class.
>
> If accessing the static final constants from another class, the static
> block is not executed.
> This causes the assertions to remain un-proofed, even if -ea -esa is set.
>
> Is that correct ?
>
> IMO, assertions should always run, if -ea -esa is set.
>
> -Ulf
>
>
>
> package sun.nio.cs.ext;
>
> import static sun.nio.cs.CharsetMapping.*;
>
> /**
> *
> * @author Ulf Zibis, Cologne CoSoCo.de
> */
> class EUC_TWMapping3 extends EUC_TWMapping {
> static final short PL0_5_B2C_RANGE = 0x2300; // plane 0, 5 b2c range
> static final short PLANE_B2C_RANGE = 0x1f00; // plane 2..4, 6..15
> b2c range
>
> // TODO: file bug: static block should run, if assertions are enabled.
> static {
> // assert plane offsets and content
> for (int p=0, range, offset=0; p<b2c.length; p++) {
> range = p % 4 == 0 ? PL0_5_B2C_RANGE : PLANE_B2C_RANGE;
> for (int i=range; i<b2c[p].length(); i++)
> assert b2c[p].charAt(i) == UNMAPPABLE_DECODING;
> // static block should run, if assertions are enabled. For test
> uncomment following line
> // System.out.printf("offset: %d, range: %d, b2c[p].length():
> %d%n", offset, range, b2c[p].length());
> assert (offset += range) <= Character.MAX_VALUE + 1;
> }
> }
>
> // WORKAROUND:
> // static int offset = 0; // assert from calling context to force
> static block to process
> // static {
> // // assert plane offsets and content
> // for (int p=0, range; p<b2c.length; p++) {
> // range = p % 4 == 0 ? PL0_5_B2C_RANGE : PLANE_B2C_RANGE;
> // for (int i=range; i<b2c[p].length(); i++)
> // assert b2c[p].charAt(i) == UNMAPPABLE_DECODING;
> //// static block should run, if assertions are enabled. For test
> uncomment following line
> //// System.out.printf("offset: %d, range: %d,
> b2c[p].length(): %d%n", offset, range, b2c[p].length());
> // assert (offset += range) <= Character.MAX_VALUE + 1;
> // }
> //// static block should run, if assertions are enabled. For test
> uncomment following line
> //// assert false;
> // }
> }
>
>
> package sun.nio.cs.ext;
>
> /**
> *
> * @author Ulf Zibis, Cologne CoSoCo.de
> */
> public class AssertTest {
>
> public static void main(String... args) {
> // static block in EUC_TWMapping3 should run, if assertions are enabled.
> System.out.println(EUC_TWMapping3.PLANE_B2C_RANGE);
> // WORKAROUND: For test uncomment following line
> // assert EUC_TWMapping3.offset > 0; // force assertion, TODO:
> JDK bug ?
> }
> }
>
>
More information about the compiler-dev
mailing list