Hi Roger, > I guess my main motivation for doing this was seeing a lot of common > code, that is, this pattern: > if (blah == null) { > throw new IllegalArgumentException("blah is null.") > } > and wanted to make some common code for it. Because lots of times the > exception had no message, or it was slightly different. So, I also wanted > to make the messages consistent, and the replacement code easy to write. > ok
> Because of that, I didn't look for any other alternatives (like the Java > 7 Objects method). no problem, I'm happy with all solutions :-) ... > Although I did consider using an "assert"... the problem of Java assert is that thay are by default not enabled (and must be enabled with some flags), so don't know if really someone uses them ... see: http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html For example Groovy assertions are always enabled (so makes sense in many real-world cases), and even in Scala (but could be removed with a dedicated annotation, this is really great !!). > But in the > end, I just decided to make a small common method for this, mostly so that > the kind of exception that is thrown (IllegalArgumentException) would stay > the same (note that the Objects.requireNotNull throws a > NullPointerException, which I don't like for these type of checks). I agree with you :-) I could post a comment in related issue, and update the code with those methods ... > I do like the idea of the method returning the object again, but that > would require using generics, probably, or else you need a cast on every > call, which is a bit of a pain. I would actually prefer an annotation > ......... but that isn't supported in Java. yes, maybe later or some test is useful and simple ... And for the annotation see in a later release like 2.2 requiring minimum Java 8 (with Java 8 things should be simpler even for annotations). > Also, I'd like to see the message generated from text resources, so it > could be translated as required ... Which is a lot easier if all the checks > are in one place. Some time ago I wrote something like this (in a project at work) for custom Exceptions, with messages from labels decaded in resource bundles ... here we could do something like this even for this kind of controlled exceptions, maybe in a dedicated issue. Bye