Java Pearls: assertNotNull() – the expert version
Posted: January 18, 2011 Filed under: Java, Java Pearls Leave a comment »Recently a colleague stumbled upon this pearl doing a production code review:
/**
* Assert not null.
* @param object the object
* @param element type
* @return the not null object
*/
public static T assertNotNull(T object) {
if (object == null) {
assertNotNull(object, "");
throw thisLineShouldNeverBeReached();
}
return object;
}
/**
* Assert not null.
* @param object the object
* @param msg the msg
*/
public static void assertNotNull(Object object, String msg) {
if (object == null) {
throwRuntimeException("assertion: object is null but was expected not to be null. \n"
+ msg);
}
}
well at least it is a generic method
The commenting is also from the production code… Hell of a expert developer.
Advertisement