czwartek, 23 czerwca 2011

Spice up your Java with Lombok

Verbosity of Java language has been discussed for years. Almost since inception developers keen on clean and concise code were complaining about writing resource management code (lengthy if-try-catch with JDBC) or creating getters/mutators in Java beans. Simply put, Java language forces devs to write lots of boilerplate code. Some most painful spots are being addressed by language enhancements, like Automatic Resource Management (ARM) in Java 7. Yet this process is complex, cumbersome and takes time.
Is there any better (faster) relieve then? It seems there is! Presenting Project Lombok. Nice utility to speed-up development using source-code annotations. Some most useful features are as follows:
  • @Getter/@Setter - simply annotate any field to have appropriate method generated. What's more, getter can be "lazy" without any explicit locking code.
  • @Cleanup - ARM is here, no need to wait for version 7 release.
  • @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor - used along with @NotNull instruct Lombok to generate appropriate constructors with arguments checking. Beautiful :)
Full list can be found here.

To entice you more, just take a look at following example:
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor
public class Bean {

    @NotNull @Getter private String name;
    @Getter @Setter private String value;
    @Getter @Setter private int count; 
}
Isn't that appealing? ;-)

2 komentarze:

Jimiasty pisze...

Annotated getters and setters in java looks impressive, but for .NET developers it's a daily bread:


public class Person
{
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
}

qba pisze...

Indeed, Java as a language lacks many features embraced a long time ago by other languages. On the other hand, Lombok gives much more than "standard" built-in solutions :)