<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency>
plugins { id 'net.ltgt.apt' version '0.10' } dependencies { compileOnly 'org.projectlombok:lombok:1.16.18' apt "org.projectlombok:lombok:1.16.18" }
Title | Description | Lombok Example | Java sample |
---|---|---|---|
@NonNull | processing variables which should not receive null | | |
@Getter / | easy creation of getters and setters | | |
@ToString | defining annotation before class, to implement the standard toString method | | |
@EqualsAndHashCode | easy creation of Equals and HashCode methods | | |
@NoArgsConstructor, | creating an empty constructor, constructor including all final fields or constructor including all possible fields | | |
@Data | generation of all utility methods immediately replaces the @ToString, @EqualsAndHashCode, Getter , Setter , @RequiredArgsConstructor | | Lot of code |
@Value | creating immutable classes analogue of Data, but for immutable classes | | Lot of code |
@Builder | implementation of the bulder pattern, Singular - used for objects in single copy (add item in collections, etc.) | | a lot of code |
@SneakyThrows | wrapper of checked exceptions | @SneakyThrows ( UnsupportedEncodingException.class) public String utf8ToString (byte [] bytes) { return new String (bytes, "UTF-8"); } | |
@Synchronized | easy creation of synchronized blocks | | Lot of code |
@Log | adding logging initiation, also allows you to select the type of logger: @CommonsLog, @JBossLog, Log , @ Log4j, @ Log4j2, @ Slf4j, @ XSlf4j | | |
Val | simple creation of the final variable with type inference, that is, the val of which argued | | |
@Cleanup | simple definition of resources so that they automatically close after the end of the code. (not so relevant when using try with resources) | | |
public Example(@NonNull P p) { super("Hello"); this.name = p.getName(); }
Common Java code: public Example(@NonNull P p) { super("Hello"); if (p == null) { throw new NullPointerException("p"); } this.name = p.getName(); }
@Getter @Setter private int age = 10;
Common Java code: private int age = 10; public int getAge() { return age; } public void setAge(int age) { this.age = age; }
@ToString(exclude="f") public class Example
Common Java code: public class Example { @Override public String toString() { return ...; }
@EqualsAndHashCode( exclude={"id1", "id2"}) public class Example {
Common Java code: public class Example { ... @Override public boolean equals(Object o) { ... } @Override public int hashCode() { ... }
@RequiredArgsConstructor( staticName = "of" ) @AllArgsConstructor( access = AccessLevel.PROTECTED ) public class E<T> {
Common Java code: public class E<T> { private E(T description) { ... } public static <T>E<T> of( T description ) { return new E<T>(description); }
@Data public class Example { private final String name; private int age; }
public class Example { private final String name; private int age; public Example( String name ) { this.name = name; } public String getName() { return this.name; } void setAge(int age) { this.age = age; } public int getAge() { return this.age; } @Override public String toString() { return ...; } @Override public boolean equals( Object o ) { .... } @Override public int hashCode() { ... }
@Value public class Example { private final String name; private int age; }
public class Example { private final String name; private final int age; public Example( String name, int age ) { this.name = name; this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } @Override public String toString() { return ...; } @Override public boolean equals( Object o ) { .... } @Override public int hashCode() { ... }
@Builder public class Example { private String name; private int age; @Singular private Set<String> occupations; }
public class Example { private String name; private int age; private Set<String> occupations; Example( String name, int age, Set<String> occupations ) { this.name = name; this.age = age; this.occupations = occupations; } public static ExampleBuilder builder() { return new ExampleBuilder(); } public static class ExampleBuilder { private String name; private int age; private ArrayList<> occupations; ExampleBuilder() { } public ExampleBuilder name( String name ) { this.name = name; return this; } public ExampleBuilder age( int age ) { this.age = age; return this; } public ExampleBuilder occupation( String occupation ) { if (this.occupations == null) { this.occupations = new ArrayList<String>(); } this.occupations.add(occupation); return this; } ... public Example build() { Set<String> occupations = ...; return new Example(name, age, occupations); } @java.lang.Override public String toString() { ... } } }
public String utf8ToString(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { throw Lombok.sneakyThrow(e); } }
private final Object readLock = new Object(); @Synchronized public static void hello() { ...; } @Synchronized public int answerToLife() { ... } @Synchronized("readLock") public void foo() { ... }
private static final Object $LOCK = new Object[0]; private final Object $lock = new Object[0]; private final Object readLock = new Object(); public static void hello() { synchronized($LOCK) { ... } } public int answerToLife() { synchronized($lock) { ... } } public void foo() { synchronized(readLock) { ... } }
@Slf4j public class Example { public static void main(String... args) { log.error("error"); }
Common Java code: public class Example { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class); public static void main(String... args) { log.error("error"); }
val map = new HashMap<Integer, String>(); for (val entry : map.entrySet()) { ... }
final HashMap<Integer, String> map = new HashMap<Integer, String>(); ... for (final Map.Entry<Integer, String> entry : map.entrySet()) { ... }
@Cleanup InputStream in = new FileInputStream(args[0]); @Cleanup OutputStream out = new FileOutputStream(args[1]); ...
Common Java code: InputStream in = new FileInputStream(args[0]); try { OutputStream out = new FileOutputStream(args[1]); try { ... } finally { if (out != null) { out.close(); } } } finally { if (in != null) { in.close(); } }
Source: https://habr.com/ru/post/345520/
All Articles