如何以编程方式启用断言?
如何以编程方式为特定类启用断言,而不是指定命令行参数“-ea”?
public class TestAssert { private static final int foo[] = new int[]{4,5,67}; public static void main(String []args) { assert foo.length == 10; } }
这是对@ bala的好答案的评论,但它太长了。
如果你只是启用断言然后调用你的主类 – 你的主类将在启用断言之前加载,所以你可能需要一个不直接引用代码中任何其他内容的加载器。 它可以设置断言,然后通过reflection加载其余的代码。
如果在加载类时未启用断言,那么它们应立即“编译”,这样您就无法打开和关闭它们。 如果你想切换它们,那么你根本不需要断言。
由于运行时编译,这样的事情:
public myAssertNotNull(Object o) { if(checkArguments) if(o == null) throw new IllegalArgumentException("Assertion Failed"); }
应该像断言一样快速工作,因为如果代码执行很多并且checkArguments是假的并且没有改变那么整个方法调用可以在运行时编译出来,这将具有与断言相同的基本效果(此性能取决于VM)。
尝试
ClassLoader loader = getClass().getClassLoader(); setDefaultAssertionStatus(true);
要么
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
编辑:
根据评论
ClassLoader loader = ClassLoader.getSystemClassLoader(); loader.setDefaultAssertionStatus(true); Class> c = loader.loadClass("MyClass"); MyClass myObj = (MyClass) c.newInstance(); public class MyClass { private static final int foo[] = new int[]{4,5,67}; MyClass() { assert foo.length == 10; } }
最简单和最好的方法可以是:
public static void assertion(boolean condition,String conditionFailureMessage) { if(!b) throw new AssertionError(conditionFailureMessage); }
无需将-ea设置为VM参数。
调用函数如:
assertion(sum>=n,"sum cannot be less than n");
如果断言失败,代码将给出AssertionError,否则代码将安全运行。
您也可以通过编程方式启用/禁用断言:
http://download.oracle.com/docs/cd/E19683-01/806-7930/assert-5/index.html