barteks2x
barteks2x

Reputation: 1355

Enable assertions per-package

Is it possible to enable java assertions per-package? I want to use assertions in my code, but one of the things that runs alongside of my code breaks some unrelated assertion and authors refuse to fix it (at least for now, and it's not a critical error).

Upvotes: 8

Views: 1832

Answers (1)

Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

It is possible, see http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html#enable-disable

Enabling and Disabling Assertions

To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch:

  • packageName...
    Enables or disables assertions in the named package and any subpackages.
  • ...
    Enables or disables assertions in the unnamed package in the current working directory.
  • className
    Enables or disables assertions in the named class

For example, the following command runs a program, BatTutor, with assertions enabled in only package com.wombat.fruitbat and its subpackages:

java -ea:com.wombat.fruitbat... BatTutor

You could enable assertions for all packages, then disable them for some of the packages. Or otherwise- disable for all packages, then enable only for some of them.

Upvotes: 9

Related Questions