Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 437508 - Introduce @Accessors active annotation
Summary: Introduce @Accessors active annotation
Status: CLOSED FIXED
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.6.0   Edit
Hardware: PC Mac OS X
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard: v2.7
Keywords:
Depends on:
Blocks: 381209 381498
  Show dependency tree
 
Reported: 2014-06-16 04:14 EDT by Sven Efftinge CLA
Modified: 2017-10-31 11:26 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sven Efftinge CLA 2014-06-16 04:14:27 EDT
In addition to an annotation for immutable value types, we should have one for mutable Java Beans. @Bean seems to be a good name.
Comment 1 K Kumar CLA 2014-06-25 18:36:10 EDT
Many times, @Data is not that applicable on day today basis especially creating a constructor with all fields in it.

@Bean would be nice. If that name is too generic, we can also consider @ValueBean or @ValueObject or @Pojo or @Dto (data transfer object).

I think it should do following.

1. Make all fields Read/Write properties
2. Generate ToString for the bean
3. Generate equals/hashcode based on the user's settings
Comment 2 Sven Efftinge CLA 2014-06-26 02:05:07 EDT
The newly introduced @Getter and @Setter can be used on class level as well.
Implementing value based equals/hashcode for mutable data types is a bad idea, as the contract of hashCode forbids to change it. Still, if you really want to have it there is @EqualsHashCode.
For generating 'toString' there is a dedicated annotation @ToString, which can be used independently.

Still

@Getter @Setter class Person {
}

doesn't read as nicely as 

@Bean class Person {
}

What do others think?
Should we have @Bean (w/o equals & hashcode)?

See also https://git.eclipse.org/r/#/c/28796/
Comment 3 Stefan Oehme CLA 2014-07-04 15:00:16 EDT
We found a way to harmonize @Getter, @Setter, @Property and @Bean. It will be called @Accessors. Annotated on a field, it creates a getter and setter for that field. Annotated on the class, it creates getters and setters for all fields and also a constructor that takes all final fields. The annotation will be configurable, so you can specify the visibility of the getter and the setter and even opt out of having them. So if you have @Accessors on a class, you can still exclude a single field.

@Accessors class Person {
  val int id //becomes a constructor argument and gets a public getter
  
  String firstName //public getter and setter
  
  @Accessors(PROTECTED_GETTER) 
  String lastName //no setter, protected getter
}

The @Data annotation will also respect the @Accessors, so you could for instance have a @Data class with only protected getters instead of public ones.
Comment 4 Stefan Oehme CLA 2014-07-07 07:05:59 EDT
Pushed to review
https://git.eclipse.org/r/#/c/29536/
Comment 5 Eclipse Webmaster CLA 2017-10-31 11:26:52 EDT
Requested via bug 522520.

-M.