• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sensing Assertions - the case for programmatically testing whether assertions + no AssertionErrors

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can programmatically test whether assertions have been enabled without throwing an AssertionError

This is from OCP Exam 3 Question 24...

Apprently, this is a true statement. I have searched the interwebs shallow and deep for something to grok, but nothing makes since.

Can some kind programmer explain this to me with:
  • text-based explanation
  • program example
  • psuedo-code


  •  
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I do suggest you to look at Assertions
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    and Ted North : if you want to become a good man, you should at least taste what a bad man do. so please avoid that feeling...
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Seetharaman Venkatasamy wrote:I do suggest you to look at Assertions



    Uh, thank-you I guess...but I am studying for 6 not 1.4
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    assertion introduced in 1.4 and i think there wont be a major change
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Is this a way to test if assertions are enabled programmatically? This does throw an AssertionError though so not all the requirements are met.

    programming with assertions info - scroll to the bottom to number 7...

    Respectfully,

    Ted North - powerhouse of industry and IT
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Does changing the assertsEnabled variable to true in the assert statement fit the bill for programmatically checking if assertions are enabled without throwing any? tl:dr - does this provide an example for the initial question found in OCP Exam 3?

    Respectfully,

    TN - 99 percenter
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ted North wrote:

    Is this a way to test if assertions are enabled programmatically? This does throw an AssertionError though so not all the requirements are met.

    programming with assertions info - scroll to the bottom to number 7...

    Respectfully,

    Ted North - powerhouse of industry and IT


    if the condition failed the detailed message[ "shock to the system... + " + " assertsEnabled = " + assertsEnabled] would go to user if you metion -ed as an argument as in java -ea ClassName

    if there is no such a sentence after assert boolean as in assert b; then if b is not true AssertionError will be thrown
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do you think proves how the statement found in the OCP exam books is true? That was my initial question.


    Thank-you for the feedback. I did run this program with the -ea flag and saw the output.

     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ted North wrote:Do you think proves how the statement found in the OCP exam books is true? That was my initial question.


    no idea. i just share my experience ...
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    author
    Posts: 9050
    21
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I just rewrote the program and it works fine!
     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bert Bates wrote:I just rewrote the program and it works fine!



    Hi Bert,

    Thank-you for the response. This means a lot to me. My colleague was impressed you responded as well. <quick hello to work-colleague />

    //Fan-boy mode off

    What do you mean the program works fine?



    Which one? Do you think that the program answers the question about whether, "You can programmatically test whether assertions have been enabled without throwing an AssertionError"?

    Thank-you again for the reply.

    Respectfully,

    TN

     
    Ranch Hand
    Posts: 924
    1
    Netbeans IDE Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    maybe this will help http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=3. the classloader maintains mapping for the status of assertion enabled on package and classes. when it loads the class, it checks its mapping and sets a special assertion enabled flag in the class.

    Maybe you can extract that "special assertion enabled flag" or the mappings programmatically.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    gurpeet singh wrote:maybe this will help http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=3. the classloader maintains mapping for the status of assertion enabled on package and classes. when it loads the class, it checks its mapping and sets a special assertion enabled flag in the class.

    Maybe you can extract that "special assertion enabled flag" or the mappings programmatically.



    I think you missed this (to be included next) post of this topic. Looks like the OP found the section in the assertion document that explains on how to test for assertions without triggering an assertion. See this ...

    Ted North wrote:

    Does changing the assertsEnabled variable to true in the assert statement fit the bill for programmatically checking if assertions are enabled without throwing any? tl:dr - does this provide an example for the initial question found in OCP Exam 3?

    Respectfully,

    TN - 99 percenter

     
    Ted North
    Ranch Hand
    Posts: 201
    1
    Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:

    Maybe you can extract that "special assertion enabled flag" or the mappings programmatically

    .

    I think you missed this (to be included next) post of this topic. Looks like the OP found the section in the assertion document that explains on how to test for assertions without triggering an assertion. See this ...



    Thanks Henry. I guess im not that incompetant. XD

    gurpeet singh - thanks for pointing to the article in java_world about assertions. I definitely know more about class loaders now. The article pointed me toward the Java Language Specification (JLS). I downloaded the .pdf for SE6 from here: http://docs.oracle.com/javase/specs/

    On pages 373-374 or pdf 407-408 there is an interesting program that has static initialization blocks and other interesting things that happen to, what I think amounts, to programatically checking to see whether assertions are enabled on the class without throwing any; which is the original question.

    The code from these pages is as follows:



    I appreciate everyone's help on this. It's an honor to have such great feedback in the java community.



    reply
      Bookmark Topic Watch Topic
    • New Topic