In the form of non-existent Java annotations, here is what I would like:

class Example {
    static double sqrt(@Domain(0, Double.MAX_VALUE) x) {
        return Math.sqrt(x);

    @Range(0, Double.MAX_VALUE)
    static double square(double x) {
        return x * x;

    @Range(-Double.MAX_VALUE, Double.MAX_VALUE)
    static double add(double a, double b) {
        return a + b;

    public static void main(String [] args) {
        sqrt(square(42));   // fine
        sqrt(add(40, 2));   // not fine because `add` may return a negative number

I want an annotation parser that will do the following:

  1. Allows me to specify a Domain on parameters that are numbers (i.e. @Domain(-1, 1) double x
  2. Allows me to specify a Range on functions that return numbers (i.e.@Range(-1, 1) double foo() { ... }.
  3. Throw an error if, somewhere in the source code, a function that has domain-restricted parameters is called with range-restricted parameters whose range(s) lie outside of the domain(s).
  4. It should assume all functions that aren't annotated with a @Range will lie outside the domain of any function they're used in.
  • Did you have a look at the obvious AspectJ? – Thomas Weller Nov 19 '15 at 22:11
  • As I read it again, this is probably not an AOP kind of thing and more something for FindBugs – Thomas Weller Nov 19 '15 at 22:13
  • @ThomasWeller I have looked at AspectJ and FindBugs a little. However, I'm more interested in a specific solution than a framework I could use to write my own solution. This is only because I'm lazy, and I don't want to learn the frameworks if it isn't necessary. – michaelsnowden Nov 19 '15 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.