Issue
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
- Is the only the functionality of assertion ignored, or
- 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;
}
Solution
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);
Answered By - shmosel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.