1 2 Previous Next 21 Replies Latest reply: Jun 4, 2009 9:19 AM by 791266 Go to original post RSS
      • 15. Re: Generate getter/setter methods with annotations?
        jschellSomeoneStoleMyAlias
        >
        My whole point of generating getter/setter methods
        was to make the source code cleaner and, as a result,
        simpler and easier to maintain. Completely dumb,
        obvious getter/setter method logic occupies
        thousands lines of code in our company's
        projects, cluttering the real logic with noise. It
        also take some time to create AND maintain all those
        getter/setter methods (yes, IDE helps but still...)
        Presumably that applications are represented by say 100s of thousands of lines of code because otherwise there is a design problem (data is being exposed for to many classes.)

        Other than that code generation has existed for tens of years. It doesn't require annotations to implement. But nothing stops you from using annotations as the key which drives the code generation.
        • 16. Re: Generate getter/setter methods with annotations?
          dcminter
          Other than that code generation has existed for tens
          of years. It doesn't require annotations to
          implement.
          For me it's a matter of aesthetics. For me Java is a better language for eschewing preprocessors. If one assumes that (customisable) preprocessors are undesirable, then which code would you prefer to write?
          @get @set
          private int foo;
          
          @get(PROTECTED) @set
          private int bar;
          Or
          private int foo;
          private int bar;
          
          public int getFoo() {
             return foo;
          }
          
          protected int getBar() {
             return bar;
          }
          
          public void setFoo(int foo) {
             foo = foo;
          }
          
          public void setBar(int bar) {
             this.bar = bar;
          }
          I would prefer (with quite a lot of caveats) the former version, simply because the latter version is more likely to end up suffering from cut'n'paste inheritance (spot the deliberate bug).

          Dave.
          • 17. Re: Generate getter/setter methods with annotations?
            jschellSomeoneStoleMyAlias
            Other than that code generation has existed for
            tens
            of years. It doesn't require annotations to
            implement.
            For me it's a matter of aesthetics. For me Java is a
            better language for eschewing preprocessors. If one
            assumes that (customisable) preprocessors are
            undesirable, then which code would you prefer to
            write?
            Are you saying that there is no way to add anything to the current java compiler (via a annotation plugin) to allow you to take source code A and produce gen'd file B?
            • 18. Re: Generate getter/setter methods with annotations?
              608410
              Are you saying that there is no way to add anything
              to the current java compiler (via a annotation
              plugin) to allow you to take source code A and
              produce gen'd file B?
              It is quite hard. To make it work Bgen needs to be a super or subclass of A.

              IF Bgen is the superclass of A then the fields in A shadow those in Bgen which causes problems (Bgen must have a copy of the fields because it cannot access the fields in the subclass A).

              IF Bgen is a subclass of A, that problem disappears, but you then have to treat A as effectively a template for Bgen and all the rest of your application has to ignore the presence of A and use Bgen which can seem awkward. Also code in A cannot use the accessors because they are in the subclass, that shouldn't be too much of a problem, because if they are generated, then the accessors prpbably wont have anything other than the simplistic implementation so direct field access in A will probably work.

              Another option I have thought about but haven't explored fully, is that if the getters and setters are only there for bean style reflective access (rather than called directly from your code), then it might be possible to generate a BeanInfo which supplies getter and setter methods. I think the problem is that the Methods returned from PropertyDescriptor will be invoked on the bean itself, not via some dynamic proxy that could pick the thing up and modify the field.


              So I think generating accessors is possible, but not worthwhile.

              Bruce
              • 19. Re: Generate getter/setter methods with annotations?
                jschellSomeoneStoleMyAlias
                I would prefer (with quite a lot of caveats) the
                former version, simply because the latter version is
                more likely to end up suffering from cut'n'paste
                inheritance (spot the deliberate bug).
                I should add that typically my source starts with a SQL schema like the following....
                     create mytable
                     (
                           my_field varchar(32)
                           my_other_field varchar(64)
                      )
                 
                And from that source I might typically generate all of the following....
                1. A primitive (DTO)
                2. A DAO
                3. SQL CRUD stored procs
                4. SQL custom query procs
                5. Unit test code.
                6. Optionally various files for ant, make and J2EE containers.

                And yes I would prefer to maintain the SQL schema, despite the fact that there are no annotations nor even java, involved.

                Sometimes I even start with something like the following....
                    table: mytable
                             field: myField string 32
                             field: myOtherField string 64
                 
                And the output of that besides all of the above would also create the schema for different targetted databases.
                • 20. Re: Generate getter/setter methods with annotations?
                  843793
                  I know it is a conflict of my own interest to ask this(as I am a student and this thread is highly relevant to my current assignment) but, why not just use the protected and public modifiers? for example, if your class has a variable which you are going to make a public getter and setter for, why not do it the "old school" C way and make the variable public, hence allowing the same level of access either way? The only benefit of writing a getter/setter I can see is that you can ensure thread safety.

                  Looking forward to hearing the answer as I work on my assignment...
                  • 21. Re: Generate getter/setter methods with annotations?
                    791266
                    Maybe_Factor wrote:
                    I know it is a conflict of my own interest to ask this(as I am a student and this thread is highly relevant to my current assignment) but, why not just use the protected and public modifiers? for example, if your class has a variable which you are going to make a public getter and setter for, why not do it the "old school" C way and make the variable public, hence allowing the same level of access either way? The only benefit of writing a getter/setter I can see is that you can ensure thread safety.

                    Looking forward to hearing the answer as I work on my assignment...
                    Hi,

                    The thread is 3 years old. I'm locking it. Create a new thread if you have a specific question that is related to annotation processing, and do in that case also post a description of your problem.

                    Kaj
                    1 2 Previous Next