[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-hotspot-dev
Subject:    Assertions in static blocks ?
From:       Keith.McGuigan () Sun ! COM (Keith McGuigan)
Date:       2010-02-12 15:12:58
Message-ID: 4B756FFA.9000806 () sun ! com
[Download RAW message or body]


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 ?
>     }
> }
> 
> 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic