为每个包启用断言

Enable assertions per-package

是否可以为每个包启用 java 断言?我想在我的代码中使用断言,但与我的代码一起运行的其中一件事破坏了一些不相关的断言,作者拒绝修复它(至少现在,这不是严重错误)。

有可能,见http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html#enable-disable

启用和禁用断言

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

您可以为所有包启用断言,然后为某些包禁用断言。或者 - 禁用所有包,然后只启用其中的一些包。