Results 1 to 2 of 2
  1. #1
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Practical use of annotaions

    My own reply on this post here got me thinking.
    I can imagine annotations being useful as meta information on a class. Sometimes that could be handier/cleaner than implementing an interface that does the same.

    I added two code fragments that show the code with an annotation and an interface. If there is no need to change the value of min and max at runtime, I must admit I like to looks of the annotation. My Controllers are not polluted with meta information.

    Does any one of you use home-brew annotations in real life projects? And if yes, for what purpose?

    With annotation:
    Java Code:
    @Retention(RetentionPolicy.RUNTIME)
    @interface Output {
        int min();
        int max();
    }
    
    interface Controller {
        void doSomething();
    }
    
    @Output(min = 1, max = 1)
    class SmallOutputController implements Controller {
        @Override
        public void doSomething() {}
    }
    
    @Output(min = 10, max = 100)
    class MediumOutputController implements Controller {
        @Override
        public void doSomething() {}
    }
    
    @Output(min = 100, max = Integer.MAX_VALUE)
    class BigOutputController implements Controller {
        @Override
        public void doSomething() {}
    }
    With an interface:
    Java Code:
    interface Controller {
        void doSomething();
        int min();
        int max();
    }
    
    class SmallOutputController implements Controller {
        @Override
        public void doSomething() {}
    
        @Override
        public int min() {
            return 1;
        }
    
        @Override
        public int max() {
            return 1;
        }
    }
    
    class MediumOutputController implements Controller {
        @Override
        public void doSomething() {}
    
        @Override
        public int min() {
            return 10;
        }
    
        @Override
        public int max() {
            return 100;
        }
    }
    
    class BigOutputController implements Controller {
        @Override
        public void doSomething() {}
    
        @Override
        public int min() {
            return 100;
        }
    
        @Override
        public int max() {
            return Integer.MAX_VALUE;
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Practical use of annotaions

    How would the meta info be used? And I assume you are not using annotations in lieu of documentation but would provide
    an annotation processor of some sort.

    For me personally, I haven't found homegrown annotations very useful. There doesn't seem to be much documentation on how
    to really apply and process them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Practical question from a newbie
    By fander1980 in forum New To Java
    Replies: 8
    Last Post: 12-21-2013, 10:36 AM
  2. Replies: 0
    Last Post: 10-06-2011, 08:00 AM
  3. Best Jsp book with practical approach
    By Saurabh321 in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •