Jin Kwon
Jin Kwon

Reputation: 21978

Are assert statements ignored during runtime?

With following codes,

public Some persist(final Some entity) {
    if (flag) {
        super.persist(entity);
        entity.credentials();
        update(entity);
        return entity;
    }
    assert super.persist(entity).equals(entity);
    assert entity.credentials().equals(entity);
    assert update(entity);
    return entity;
}

if the flag is false, it seems statements are ignored and not executed.

Is it normal? Or some assertion failed?

I already know how to enable assertion.

What I want to know is that the whole assert ... statement is ignored or not regardless of enabling the -ea option.

I'm trying them with my spring library and it seems that the whole statement which means the code yielding the boolean flag are ignored.

What I want to know is that

  1. Is the only the functionality of assertion ignored, or
  2. The whole statement are ignored.

I'm sharing what I learned. It's pretty shocking to me.

public Some persist(final Some entity) {

    // WORKS!
    super.persist(entity);
    entity.generate();
    update(entity);
    return entity;

    // WORKS!
    final Some persisted = super.persist(entity);
    assert persisted.equals(entity);
    final Some generated = entity.credentials();
    assert generated.equals(persisted);
    final boolean updated = update(generated);
    assert updated;
    return generated;

    // DOES NOT WORK!!! SHOULDN'T DO THIS!!!
    assert super.persist(entity).equals(entity);
    assert entity.generate().equals(entity);
    assert update(entity);
    return entity;
}

Upvotes: 4

Views: 6444

Answers (4)

Jin Kwon
Jin Kwon

Reputation: 21978

I'm adding an answer for my own question for an informative purpose.

https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html

Do not use assertions to do any work that your application requires for correct operation. Because assertions may be disabled, programs must not assume that the boolean expression contained in an assertion will be evaluated. Violating this rule has dire consequences. For example, suppose you wanted to remove all of the null elements from a list names, and knew that the list contained one or more nulls. It would be wrong to do this:

// Broken! - action is contained in assertion
assert names.remove(null);

The program would work fine when asserts were enabled, but would fail when they were disabled, as it would no longer remove the null elements from the list. The correct idiom is to perform the action before the assertion and then assert that the action succeeded:

// Fixed - action precedes assertion
boolean nullsRemoved = names.remove(null);
assert nullsRemoved;  // Runs whether or not asserts are enabled

As a rule, the expressions contained in assertions should be free of side effects: evaluating the expression should not affect any state that is visible after the evaluation is complete. One exception to this rule is that assertions can modify state that is used only from within other assertions. An idiom that makes use of this exception is presented later in this document.

Upvotes: 2

Anil Bhaskar
Anil Bhaskar

Reputation: 3978

You got to enable assertions. Assertions can be enabled or disabled when the program is started, and are disabled by default.

Disabling assertions eliminates their performance penalty entirely. Once disabled, they are essentially equivalent to empty statements in semantics and performance. You may want to test your code on fly in dev but do not want to decrease your performance in prod without changing any code.

Is the only the functionality of assertion ignored

I guess that is the whole point of assertions, that it enables you to test your assumptions about your program.

The whole statement are ignored

Yes, When Java program is compiled, with assertions enabled/disabled, it is marked in the generated class file. So when class-loader loads your file and run the instruction or line is completely ignored if assertions were disabled.

Upvotes: 4

shmosel
shmosel

Reputation: 50716

What I want to know is that the whole assert ... statement is ignored or not regardless of enabling the -ea option.

Yes, it will be ignored entirely unless assertions are enabled. This is useful as a way to detect whether they're enabled at runtime:

boolean assertionsEnabled = false;
assert assertionsEnabled = true;
System.out.println("Assertions enabled: " + assertionsEnabled);

Upvotes: 4

Infamous
Infamous

Reputation: 754

They can be activated at run-time by way of the -ea option on the java command, but are not turned on by default.

Upvotes: 3

Related Questions