Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically enable assert?

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;     } } 
like image 538
Saideira Avatar asked Apr 05 '11 21:04

Saideira


1 Answers

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;     } } 
like image 85
Bala R Avatar answered Sep 30 '22 15:09

Bala R