Java/Language Basics/Assert
Содержание
- 1 A Program with Assertions
- 2 Assert with an informative message
- 3 Catch assert exception with message
- 4 Class for checking syntax and comments for the assert
- 5 Enabling Assertions from the Command Line: -ea and -da enable and disable assertion in a package subtree or in a class.
- 6 Handling an Assertion Error
- 7 Non-informative style of assert
- 8 Using the class loader to enable assertions
A Program with Assertions
<source lang="java">
public class Main {
public static void main(String[] args) { assert args.length > 0; }
}
</source>
Assert with an informative message
<source lang="java">
//: c15:Assert2.java // {JVMArgs: -ea} // {ThrowsException} // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. public class Assert2 {
public static void main(String[] args) { assert false: "Here"s a message saying what happened"; }
}
</source>
Catch assert exception with message
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { try { assert argv.length > 0 : "my message"; } catch (AssertionError e) { String message = e.getMessage(); System.out.println(message); } }
}
</source>
Class for checking syntax and comments for the assert
<source lang="java">
/*
* file: Assertions.java * package: oreilly.hcj.review * * This software is granted under the terms of the Common Public License, * CPL, which may be found at the following URL: * http://www-124.ibm.ru/developerworks/oss/CPLv1.0.htm * * Copyright(c) 2003-2005 by the authors indicated in the @author tags. * All Rights are Reserved by the various authors. *
- DO NOT EDIT ABOVE THIS LINE ########## */
import java.awt.event.MouseEvent; import java.util.Arrays; import java.util.Iterator; import java.util.List; /**
* Class for checking syntax and comments for the assert section of the review chapter. * * @author */
public class Assertions {
/** * Main method. * * @param args Command line arguments. */ public static void main(final String[] args) { helperParseArgs(args); Iterator iter = System.getProperties() .keySet() .iterator(); for (String key = null; iter.hasNext(); key = (String)iter.next()) { assert (key != null); System.out.println(key + "=" + System.getProperty(key)); } } /** * __UNDOCUMENTED__ * * @param event __UNDOCUMENTED__ */ public void mouseClicked(final MouseEvent event) { Object source = event.getSource(); assert (source != null); // ... do code using source } /** * __UNDOCUMENTED__ * * @param args __UNDOCUMENTED__ * * @throws IllegalArgumentException __UNDOCUMENTED__ */ protected static void helperParseArgs(final String[] args) { assert (args != null); assert (!Arrays.asList(args) .contains(null)); // -- List arglist = Arrays.asList(args); Iterator iter = arglist.iterator(); for (String argument = null; iter.hasNext(); argument = (String)iter.next()) { if (argument.startsWith("-D")) { if (argument.length() < 3) { int idx = arglist.indexOf(argument); throw new IllegalArgumentException("Argument" + idx + " is not a legal property argument."); } int valueIdx = argument.indexOf("="); System.setProperty(argument.substring(2, valueIdx), argument.substring(valueIdx + 1)); assert (System.getProperty(argument.substring(2, valueIdx)).equals(argument .substring(valueIdx + 1))); } } } /** * __UNDOCUMENTED__ * * @param args __UNDOCUMENTED__ * * @throws IllegalArgumentException __UNDOCUMENTED__ */ protected static void helperParseArgs2(final String[] args) { assert (args != null); assert (!Arrays.asList(args) .contains(null)); // -- List arglist = Arrays.asList(args); Iterator iter = arglist.iterator(); for (String argument = null; iter.hasNext(); argument = (String)iter.next()) { if (argument.startsWith("-D")) { if (argument.length() < 3) { int idx = arglist.indexOf(argument); throw new IllegalArgumentException("Argument" + idx + " is not a legal property argument."); } int valueIdx = argument.indexOf("="); String key = argument.substring(2, valueIdx); String value = argument.substring(valueIdx + 1); System.setProperty(key, value); assert (System.getProperty(key).equals(value)); } } }
} /* ########## End of File ########## */
</source>
Enabling Assertions from the Command Line: -ea and -da enable and disable assertion in a package subtree or in a class.
<source lang="java">
java -ea MyApp java -ea:... MyApp java -ea:com.mycompany... MyApp java -ea:com.mycompany... -da:com.mycompany.MyClass MyApp
</source>
Handling an Assertion Error
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { try { assert argv.length > 0; } catch (AssertionError e) { String message = e.getMessage(); System.out.println(message); } }
}
</source>
Non-informative style of assert
<source lang="java">
//: c15:Assert1.java // Non-informative style of assert // Compile with: javac -source 1.4 Assert1.java // {JVMArgs: -ea} // Must run with -ea // {ThrowsException} // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. public class Assert1 {
public static void main(String[] args) { assert false; }
} ///:~
</source>
Using the class loader to enable assertions
<source lang="java">
// : c15:LoaderAssertions.java //Using the class loader to enable assertions //Compile with: javac -source 1.4 LoaderAssertions.java //{ThrowsException} //From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 //www.BruceEckel.ru. See copyright notice in CopyRight.txt. public class LoaderAssertions {
public static void main(String[] args) { ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); new Loaded().go(); }
} class Loaded {
public void go() { assert false: "Loaded.go()"; }
} ///:~
</source>