Menu

Work in progress

Recently I started to develop with Eclipse Kepler (version 4.2). I was quite astonished how much warnings it showed up for my code. Usually I trusted on the default configuration of Netbeans. Now I learned that for Netbeans I have to enable additional warnings. Like the javac compiler warnings which I found to be switched off.

I enabled these and moreover I checked my code with PMD and FindBugs. That brought more interesting hints. I improved one weakness after another. Now Findbugs doesn't show up weak points - in the default configuration. I have to learn about this.

One weakness I fixed was that clone() need to call super.clone() and therefore it needs to throw an CloneNotSupportedException.

This let me change the way some objects are created. Some classes had constructors which throw an IllegalArgumentException. I (hopefully) did it in a safer way along the CERT Coding Standards but ... If I attempt to create an object an get an exception - what does it mean? Maybe wrong arguments, technically. But domain-specific? I want a new car => boom! Exception. What was my fault? That I need a new car?

So I decided to provide a protected constructor with no checks of argument values and some public factory methods which will check the argument values and throw an IllegalArgumentException if necessary.

If there is a clone() method one of these factory methods will accept an object of this class as argument (objectToClone) and return objectToClone.clone as result. If the object is null we get an IllegalArgumentException. This factory method consumes the CloneNotSupportedException which may thrown by clone() and raises an RuntimeException instead. If a CloneNotSupportedException is thrown the we inherit from a class there cloning is not supported. This would be a fault of the programmer who build this library (me) and should fall out a testing. So this is nothing the client programmer needs to catch and raise an error message to the user or log it.

Posted by Michael Groß 2014-03-05

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.