您可以assert
在ClassLoader
.
但是您如何确定它们是否已启用?
(我想采用一些代码路径,仅在启用断言的情况下调用 JVM 时执行昂贵的检查。)
public static boolean areAssertsEnabled() {
boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side effect!!!
return assertsEnabled;
}
boolean assertEnabled = false;
try {
assert false;
} catch (AssertionError e) {
assertEnabled = true;
}
ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-ea");