Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6668271

Add a property keyword or @Property annotation to the java language

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 6
    • specification
    • x86
    • linux

      A DESCRIPTION OF THE REQUEST :
      I suggest that a
      property keyword or
      @Property annotation be added to the language

      and that the compiler generates the standard get and set (if a custom one is not present)

      JUSTIFICATION :
      I know it could be considered syntactic sugar, but if you can reduce lines of code, even if its generated automatically by the IDE you can reduce errors, the generated code obscures the handwritten code.

      It would be much less verbose to have the get and set methods automatically generated by the compiler, in much the same way as the default constructor is provided if none is defined.

      --------------------
      There is a problem that I see (the annotations for the get or set would have to be specified somehow) and it would be nice to avoid having to generate the get/set just to annotate them.
      For example if I want to use jpa and put

      private Long id;

      @Id
      public Long getId(){
      return id;
      }

      public void setId(Long id){
      this.id=id;
      }

      it could be changed to

      @Property
      @GetAnnotation(Id)
      private Long id;

      or even

      @Property @GetAnnotation(Id) private Long id;

      10 lines replaced by 1 !!!
      ----
      Another issue is that for a collection you could create methods to add items to the collection (but thats another issue). I can see that a generic framework where you define a template to be used by the compiler based on the existence of annotations could be very useful, annotations processing doesnt work as you cant change the code for the existing class, or if java were to add the "partial" keyword as c# has it would solve these types of problems.
       
      @Property @Generate(singularName=client,add=true,remove=true) private List<Client> clients;

      //get and set created as above and ..

      public void addClient(Client client)
      {
      getClients().add(client);
      }

      public void removeClient(Client client)
      {
      getClients().remove(client);
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      property Long id;

      or

      @Property private Long id;
      ACTUAL -
      private Long id;

      public Long getId(){
      return id;
      }

      public void setId(Long id){
      this.id=id;
      }

      (too many lines for something too simple!)

      CUSTOMER SUBMITTED WORKAROUND :
      using asm or bcel you could generate the code but it would have to run inside the IDE compiler for it to be useful to developers

            abuckley Alex Buckley
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: