Assertions in static blocks ?
Jonathan Gibbons
Jonathan.Gibbons at Sun.COM
Fri Mar 5 15:31:23 PST 2010
There is a trigger -- you're just not using it. "An assertion in a
static initializer only occurs when the class is statically initialized. "
-- Jon
Ulf Zibis wrote:
> Neal, Keith, thanks for your answer.
>
> ... but shouldn't javac claim, that there the code in the static block
> will be *never reached*, if there is no trigger to run it?
>
> -Ulf
>
>
> Am 05.03.2010 22:59, schrieb Neal Gafter:
>> On Fri, Mar 5, 2010 at 1:47 PM, Ulf Zibis <Ulf.Zibis at gmx.de
>> <mailto:Ulf.Zibis at gmx.de>> wrote:
>>
>> No response ?? :-(
>>
>>
>> Keith's response is correct. An assertion in a method only occurs
>> when the method is executed. An assertion in a constructor only
>> occurs when the constructor is called. An assertion in a static
>> initializer only occurs when the class is statically initialized.
>> Accessing a constant of the class does not initialize the class. So
>> there is no bug.
>>
>> Cheers,
>> Neal
>>
>>
>> I've filed a bug:
>>
>> internal review ID of 1730072
>>
>>
>> -Ulf
>>
>>
>>
>> Am 12.02.2010 15:44, schrieb Ulf Zibis:
>>
>> Am 12.02.2010 15:06, schrieb Ulf Zibis:
>>
>> 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 ?
>>
>>
>> If yes, javac should claim the code as never reached.
>>
>>
>> IMO, assertions should always run, if -ea -esa is set.
>>
>> -Ulf
>>
>>
>> -Ulf
>>
>>
>> >From - Fri Feb 12 16:25:03 2010
>> X-Account-Key: account2
>> X-UIDL: 5de5c7b81a751aec6aed7ffb66ca9b90
>> X-Mozilla-Status: 0000
>> X-Mozilla-Status2: 00000000
>> X-Mozilla-Keys:
>> X-Symantec-TimeoutProtection: 0
>> X-Symantec-TimeoutProtection: 1
>> X-Symantec-TimeoutProtection: 2
>> Return-Path: <Keith.McGuigan at Sun.COM>
>> X-Flags: 1001
>> Delivered-To: GMX delivery to ulf.zibis at gmx.de
>> <mailto:ulf.zibis at gmx.de>
>> Received: (qmail invoked by alias); 12 Feb 2010 15:12:59 -0000
>> Received: from brmea-mail-2.Sun.COM
>> <http://brmea-mail-2.Sun.COM> (EHLO brmea-mail-2.sun.com
>> <http://brmea-mail-2.sun.com>) [192.18.98.43]
>> by mx0.gmx.net <http://mx0.gmx.net> (mx002) with SMTP; 12
>> Feb 2010 16:12:59 +0100
>> Received: from fe-amer-09.sun.com <http://fe-amer-09.sun.com>
>> ([192.18.109.79])
>> by brmea-mail-2.sun.com <http://brmea-mail-2.sun.com>
>> (8.13.6+Sun/8.12.9) with ESMTP id o1CFCw62027512
>> for <Ulf.Zibis at gmx.de <mailto:Ulf.Zibis at gmx.de>>; Fri, 12
>> Feb 2010 15:12:58 GMT
>> MIME-version: 1.0
>> Content-transfer-encoding: 7BIT
>> Content-type: text/plain; CHARSET=US-ASCII; format=flowed
>> Received: from conversion-daemon.mail-amer.sun.com
>> <http://conversion-daemon.mail-amer.sun.com> by
>> mail-amer.sun.com <http://mail-amer.sun.com>
>> (Sun Java(tm) System Messaging Server 7u2-7.04 64bit (built
>> Jul 2 2009))
>> id <0KXQ00800HZH3I00 at mail-amer.sun.com
>> <mailto:0KXQ00800HZH3I00 at mail-amer.sun.com>> for
>> Ulf.Zibis at gmx.de <mailto:Ulf.Zibis at gmx.de>; Fri,
>> 12 Feb 2010 08:12:58 -0700 (MST)
>> Received: from [129.150.65.45] ([unknown] [129.150.65.45])
>> by mail-amer.sun.com <http://mail-amer.sun.com> (Sun Java(tm)
>> System Messaging Server 7u2-7.04 64bit
>> (built Jul 2 2009)) with ESMTPSA id
>> <0KXQ00GTLI9L1V60 at mail-amer.sun.com
>> <mailto:0KXQ00GTLI9L1V60 at mail-amer.sun.com>>; Fri,
>> 12 Feb 2010 08:12:58 -0700 (MST)
>> Date: Fri, 12 Feb 2010 10:12:58 -0500
>> From: Keith McGuigan <Keith.McGuigan at Sun.COM>
>> Subject: Re: Assertions in static blocks ?
>> In-reply-to: <4B756071.4000104 at gmx.de
>> <mailto:4B756071.4000104 at gmx.de>>
>> Sender: Keith.McGuigan at Sun.COM
>> To: Ulf Zibis <Ulf.Zibis at gmx.de <mailto:Ulf.Zibis at gmx.de>>
>> Cc: compiler-dev at openjdk.java.net
>> <mailto:compiler-dev at openjdk.java.net>, hotspot
>> <hotspot-dev at openjdk.java.net
>> <mailto:hotspot-dev at openjdk.java.net>>
>> Message-id: <4B756FFA.9000806 at sun.com
>> <mailto:4B756FFA.9000806 at sun.com>>
>> References: <4B756071.4000104 at gmx.de
>> <mailto:4B756071.4000104 at gmx.de>>
>> User-Agent: Thunderbird 2.0.0.23 (X11/20090817)
>> X-GMX-Antivirus: 0 (no virus found)
>> X-GMX-Antispam: 0 (Mail was not recognized as spam);
>> Detail=5D7Q89H36p77e5KAPs1l6v/Sb97LojnDmtyzoN37OXMt9GpYHsrWRra7o+psEYuNg/dar
>>
>> zWRIb1W0k0rd15IZBf9O4nqjKYX9PrVGG/zPEENchmY89mOrfO0W57R8iRtiMheMiqQP1ym7bl2H
>>
>> PZZzg==V1;
>> X-GMX-UID: ylpHc/1fPjlsBVAdATU22s0zMTE2Ncn9
>>
>>
>>
>> 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 ?
>> }
>> }
>>
>>
>>
>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/hotspot-dev/attachments/20100305/7489f8e6/attachment.html
More information about the hotspot-dev
mailing list