SafeR - examples

Printer-friendly versionPrinter-friendly version
How to use the SafeR framework


Even in cases where we do not need to process the referent we will need to unwrap it at least at the point where we store the referent in an instance variable e.g. a field of an entity. For instance a setter might unwrap the referent and the corresponding getter might wrap the referent into a new safe reference in order to return it. Never type a field with a safe reference type! This is important, because a field may not be initialized or set to a null value, but the safe reference instance itself must never be null at no point in time! In addition, to assert that the safe reference instances itself are not null, we can write...

Safe.assertFor(foobar); a precondition at the beginning of a method or, even better, by means of AOP or generated proxies we can test the parameters of a method in a central place and not visible in the business code. The use of AOP or generated proxies is recommended for this purpose, but in places where we cannot employ AOP or generated proxies we can still put that assertion at the beginning of a method. Since it is an obvious programmatic error and very unlikely to happen, that a safe reference instance itself is null, this assertion is not required but recommended. However, if it happens without that assertion, we just get a NullPointerException instead.


There is much more we can do with SafeR - discover it!