简体   繁体   English

启用每个包的断言

[英]Enable assertions per-package

Is it possible to enable java assertions per-package? 是否可以在每个包中启用java断言? 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). 我想在我的代码中使用断言,但是我的代码旁边的一个东西打破了一些无关的断言,并且作者拒绝修复它(至少现在,并不是一个严重的错误)。

It is possible, see http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html#enable-disable 有可能,请参阅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. 要以各种粒度启用断言,请使用-enableassertions或-ea ,switch。 To disable assertions at various granularities, use the -disableassertions, or -da , switch. 要禁用各种粒度的断言,请使用-disableassertions或-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: 例如,以下命令运行程序BatTutor,仅在包com.wombat.fruitbat及其子包中启用断言:

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. 或者 - 禁用所有软件包,然后仅为其中一些软件包启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM