How can I programmatically enable assert for particular classes, instead of specifying command line param "-ea"?
public class TestAssert { private static final int foo[] = new int[]{4,5,67}; public static void main(String []args) { assert foo.length == 10; } }
Try
ClassLoader loader = getClass().getClassLoader(); setDefaultAssertionStatus(true);
or
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
EDIT:
based on the comments
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With