Does Project Lombok offer any benefit compared to code templates / code generation开发者_Python百科 in Eclipse? Are there any downsides (apart from including the .jar)?.
One advantage of Lombok is that once you've annotated a class with, say, the @Data annotation, you never need to regenerate the code when you make changes. For example, if you add a new field, @Data would automatically include that field in the equals, hashCode and toString methods. You'd need to manually make that change when using Eclipse generated methods. Some of the time, you may prefer the manual control but for most cases, I expect not.
The advantage of Lombok is that the code isn't actually there - i.e. classes are much more readable and are not cluttered.
Advantages:
- Very easy to use
- Classes are much cleaner ('no boilerplate code'),especially 'struct'-like inner classes shrink to a bare minimum:
@Data
private class AttrValue {
private String attribute;
private MyType value;
}
This will create both getters and setters, a toString(), and correct hash() / equals() methods including both variables.
The variant with @Value
creates an immutable structure (no setters, all fields final).
- No need to generate/remove code when you change fields (getters, setters, toString, hash, equals)
- No interference with hand-coded methods: just add you own specific setter to the class where needed. Lombok skips this and generates everything else
Disadvantages:
- No name refactoring, yet: renaming
value
above will not (yet) renamegetValue()
andsetValue()
- May slows down ecplise slightly
toString
output not as nice as, for instance, ToStringBuilder from apache commons
Very few come to mind:
- it is based on annotation, so no good for legacy project still in pre-Java5 (delombok can help). Actually, it requires using the javac v1.6 compiler.
- it still have limitations regarding multiple constructors
The dependency issue is not to be overlooked though, but you have excluded it from your question.
Eclipse EMF offers some features which are very handy which Lombock does not yet support:
- Powerful notification mechanims to get informed about changes in your instances
- Generic API without java reflection. Access and modify instances without a strong reference to the type
- Command und API based editing
- Cross references between models: Create and load model trees and EMF handles the loading by creating a proxy for the cross reference. This saves memory and boost performance in huge domain trees
- And much more...
精彩评论