Lombok
2017-07-20
onozaty
Lombok?
• Java (
)
•
• getter/setter
• equals, hashCode
• toString
•
Lombok?
•
@Getter
@Setter
public class Customer {
private int id;
private String name;
}
public class Customer {
private int id;
private String name;
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(final int id) {
this.id = id;
}
public void setName(final String name) {
this.name = name;
}
}
Lombok?
• IDE IDE
• Lombok
( )
• IDE
• Eclipse Lombok jar
Eclipse
( )
@Getter, @Setter
• getter/setter
• (
)
• (
public)
• final setter (
)
@Getter, @Setter
public class GetterSetterExample {
//
@Getter
@Setter
private int id;
//
@Setter(AccessLevel.PROTECTED)
private String name;
}
public class GetterSetterExample {
private int id;
private String name;
public int getId() {
return this.id;
}
public void setId(final int id) {
this.id = id;
}
protected void setName(final String name) {
this.name = name;
}
}
@ToString
•
toString
•
@ToString
@ToString
public class ToStringExample {
private int id;
private String name;
public String getName() {
return name;
}
}
// exclude
@ToString(exclude = "name")
class ToStringExample2 {
private int id;
private String name;
}
public class ToStringExample {
private int id;
private String name;
public String getName() {
return name;
}
@Override
public String toString() {
return "ToStringExample(id=" + this.id
+ ", name=" + this.getName() + ")";
}
}
class ToStringExample2 {
private int id;
private String name;
@Override
public String toString() {
return "ToStringExample2(id="
+ this.id + ")";
}
}
@EqualsAndHashCode
• equals hashCode
•
•
Lombok
@EqualsAndHashCode
@EqualsAndHashCode
public class EqualsAndHashCodeExample
{
private int id;
private String name;
}
//
@EqualsAndHashCode(exclude = "age")
class EqualsAndHashCodeExample2 {
private int id;
private String name;
private int age;
}
public class EqualsAndHashCodeExample {
private int id;
private String name;
@Override
public boolean equals(final Object o) { … }
@Override
public int hashCode() { … }
}
public class EqualsAndHashCodeExample2 {
private int id;
private String name;
private int age;
// age
@Override
public boolean equals(final Object o) { … }
@Override
public int hashCode() { … }
}
@AllArgsConstructor,
@NoArgsConstructor,
@RequiredArgsConstructor
•
• @AllArgsConstructor
• @NoArgsConstructor
• @RequiredArgsConstructor final
(final
)
@AllArgsConstructor,
@NoArgsConstructor,
@RequiredArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
public class ConstructorExample {
private int id;
private String name;
}
@RequiredArgsConstructor
class ConstructorExample2 {
private final int id;
private String name;
}
public class ConstructorExample {
private int id;
private String name;
public ConstructorExample(
final int id, final String name) {
this.id = id;
this.name = name;
}
public ConstructorExample() {
}
}
class ConstructorExample2 {
private final int id;
private String name;
public ConstructorExample2(final int id) {
this.id = id;
}
}
@Data
•
• @ToString
• @EqualsAndHashCode
• @Getter
• @Setter
• @RequiredArgsConstructor
•
@Data
@Data
public class DataExample {
private final int id;
private String name;
}
public class DataExample {
private final int id;
private String name;
public DataExample(final int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public boolean equals(final Object o) { … }
@Override
public int hashCode() { … }
@Override
public String toString() { … }
}
@Value
• final
• @ToString
• @EqualsAndHashCode
• @Getter
• @RequiredArgsConstructor
• Immutable
@Value
@Value
public class ValueExample {
private int id;
private String name;
}
public final class ValueExample {
private final int id;
private final String name;
public ValueExample(
final int id, final String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
@Override
public boolean equals(final Object o) { … }
@Override
public int hashCode() { … }
@Override
public String toString() { … }
}
@Builder
• Builder (GoF Effective
Java ) Builder
• Builder
@Builder
public static void main(String[] args) {
// Builder ( )
Customer customer = new Customer(
"Taro",
"Yamada",
"Tokyo",
Arrays.asList("A", "B"));
System.out.println(customer);
}
@AllArgsConstructor
@ToString
class Customer {
private String firstName;
private String lastName;
private String city;
private List<String> tags;
}
public static void main(String[] args) {
// Builder
Customer customer = Customer.builder()
.firstName("Taro")
.lastName("Yamada")
.city("Tokyo")
.tag("A")
.tag("B")
.build();
System.out.println(customer);
}
@Builder
@ToString
class Customer {
private String firstName;
private String lastName;
private String city;
@Singular
private List<String> tags;
}
@Slf4j
• log static Logger
• Slf4j Apache commons log(@Log)
• Logger
@Slf4j
@Slf4j
public class LogExample {
public static void main(String[] args) {
log.info("main.");
}
}
public class LogExample {
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(LogExample.class);
public static void main(String[] args) {
log.info("main.");
}
}
@NonNull
• null
(null
NullPointerException )
• Objects.requireNonNull
@NonNull
@RequiredArgsConstructor
public class NonNullExample {
@NonNull
private String name;
public void printLength(
@NonNull String message) {
System.out.println(
message.length());
}
}
public class NonNullExample {
@NonNull
private String name;
public void printLength(
@NonNull String message) {
if (message == null) {
throw new NullPointerException("message");
}
System.out.println(message.length());
}
public NonNullExample(
@NonNull final String name) {
if (name == null) {
throw new NullPointerException("name");
}
this.name = name;
}
}
val
•
• Scala val
val
public static void main(String[] args) {
val list = new ArrayList<String>();
list.add("a");
list.add("b");
val item = list.get(0);
System.out.println(item);
}
public static void main(String[] args) {
final ArrayList<String> list =
new ArrayList<String>();
list.add("a");
list.add("b");
final String item = list.get(0);
System.out.println(item);
}
@Cleanup
•
( )
close
• try/finally
• close
@Cleanup
public static void main(String[] args) throws IOException {
Path filePath = Paths.get(args[0]);
@Cleanup BufferedReader reader = Files.newBufferedReader(filePath);
System.out.println(reader.readLine());
}
public static void main(String[] args) throws IOException {
Path filePath = Paths.get(args[0]);
BufferedReader reader = Files.newBufferedReader(filePath);
try {
System.out.println(reader.readLine());
} finally {
if (Collections.singletonList(reader).get(0) != null) {
reader.close();
}
}
}
@Synchronized
• synchronized
• static
Lock
• Lock
@Synchronized
public class SynchronizedExample {
@Synchronized
public static void hello() {
System.out.println("hello");
}
@Synchronized
public void bye() {
System.out.println("bye");
}
}
public class SynchronizedExample {
private static final Object $LOCK = new Object[0];
private final Object $lock = new Object[0];
public static void hello() {
synchronized (SynchronizedExample.$LOCK) {
System.out.println("hello");
}
}
public void bye() {
synchronized (this.$lock) {
System.out.println("bye");
}
}
}
@SneakyThrows
• (
)
•
•
• https://github.com/rzwitserloot/lombok/blob/master/src/core/
lombok/Lombok.java#L49
• Lombok
Lombok jar
@SneakyThrows
public class SneakyThrowsExample {
@SneakyThrows
public static void main(String[] args) {
throw new Exception();
}
}
public class SneakyThrowsExample {
public static void main(String[] args) {
try {
throw new Exception();
} catch (final Throwable $ex) {
throw Lombok.sneakyThrow($ex);
}
}
}
delombok
• delombok Lombok
• delombok
• https://projectlombok.org/features/delombok
> java -jar lombok.jar delombok src -d src-delomboked
• Lombok
onMethd onParam
• https://projectlombok.org/features/experimental/onX
Lombok
•
•
• getter/setter
•
• equals hashCode
Lombok
•
• Lombok
• Lombok
Lombok
• val @Cleanup @SneakyThrows
(Lombok
)
•
Lombok
• https://projectlombok.org/features/all

Lombokの紹介

  • 1.
  • 2.
    Lombok? • Java ( ) • •getter/setter • equals, hashCode • toString •
  • 3.
    Lombok? • @Getter @Setter public class Customer{ private int id; private String name; } public class Customer { private int id; private String name; public int getId() { return this.id; } public String getName() { return this.name; } public void setId(final int id) { this.id = id; } public void setName(final String name) { this.name = name; } }
  • 4.
  • 5.
    • Lombok ( ) •IDE • Eclipse Lombok jar Eclipse ( )
  • 6.
    @Getter, @Setter • getter/setter •( ) • ( public) • final setter ( )
  • 7.
    @Getter, @Setter public classGetterSetterExample { // @Getter @Setter private int id; // @Setter(AccessLevel.PROTECTED) private String name; } public class GetterSetterExample { private int id; private String name; public int getId() { return this.id; } public void setId(final int id) { this.id = id; } protected void setName(final String name) { this.name = name; } }
  • 8.
  • 9.
    @ToString @ToString public class ToStringExample{ private int id; private String name; public String getName() { return name; } } // exclude @ToString(exclude = "name") class ToStringExample2 { private int id; private String name; } public class ToStringExample { private int id; private String name; public String getName() { return name; } @Override public String toString() { return "ToStringExample(id=" + this.id + ", name=" + this.getName() + ")"; } } class ToStringExample2 { private int id; private String name; @Override public String toString() { return "ToStringExample2(id=" + this.id + ")"; } }
  • 10.
  • 11.
    @EqualsAndHashCode @EqualsAndHashCode public class EqualsAndHashCodeExample { privateint id; private String name; } // @EqualsAndHashCode(exclude = "age") class EqualsAndHashCodeExample2 { private int id; private String name; private int age; } public class EqualsAndHashCodeExample { private int id; private String name; @Override public boolean equals(final Object o) { … } @Override public int hashCode() { … } } public class EqualsAndHashCodeExample2 { private int id; private String name; private int age; // age @Override public boolean equals(final Object o) { … } @Override public int hashCode() { … } }
  • 12.
  • 13.
    @AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor @AllArgsConstructor @NoArgsConstructor public class ConstructorExample{ private int id; private String name; } @RequiredArgsConstructor class ConstructorExample2 { private final int id; private String name; } public class ConstructorExample { private int id; private String name; public ConstructorExample( final int id, final String name) { this.id = id; this.name = name; } public ConstructorExample() { } } class ConstructorExample2 { private final int id; private String name; public ConstructorExample2(final int id) { this.id = id; } }
  • 14.
    @Data • • @ToString • @EqualsAndHashCode •@Getter • @Setter • @RequiredArgsConstructor •
  • 15.
    @Data @Data public class DataExample{ private final int id; private String name; } public class DataExample { private final int id; private String name; public DataExample(final int id) { this.id = id; } public int getId() { return this.id; } public String getName() { return this.name; } public void setName(final String name) { this.name = name; } @Override public boolean equals(final Object o) { … } @Override public int hashCode() { … } @Override public String toString() { … } }
  • 16.
    @Value • final • @ToString •@EqualsAndHashCode • @Getter • @RequiredArgsConstructor • Immutable
  • 17.
    @Value @Value public class ValueExample{ private int id; private String name; } public final class ValueExample { private final int id; private final String name; public ValueExample( final int id, final String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public String getName() { return this.name; } @Override public boolean equals(final Object o) { … } @Override public int hashCode() { … } @Override public String toString() { … } }
  • 18.
    @Builder • Builder (GoFEffective Java ) Builder • Builder
  • 19.
    @Builder public static voidmain(String[] args) { // Builder ( ) Customer customer = new Customer( "Taro", "Yamada", "Tokyo", Arrays.asList("A", "B")); System.out.println(customer); } @AllArgsConstructor @ToString class Customer { private String firstName; private String lastName; private String city; private List<String> tags; } public static void main(String[] args) { // Builder Customer customer = Customer.builder() .firstName("Taro") .lastName("Yamada") .city("Tokyo") .tag("A") .tag("B") .build(); System.out.println(customer); } @Builder @ToString class Customer { private String firstName; private String lastName; private String city; @Singular private List<String> tags; }
  • 20.
    @Slf4j • log staticLogger • Slf4j Apache commons log(@Log) • Logger
  • 21.
    @Slf4j @Slf4j public class LogExample{ public static void main(String[] args) { log.info("main."); } } public class LogExample { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); public static void main(String[] args) { log.info("main."); } }
  • 22.
  • 23.
    @NonNull @RequiredArgsConstructor public class NonNullExample{ @NonNull private String name; public void printLength( @NonNull String message) { System.out.println( message.length()); } } public class NonNullExample { @NonNull private String name; public void printLength( @NonNull String message) { if (message == null) { throw new NullPointerException("message"); } System.out.println(message.length()); } public NonNullExample( @NonNull final String name) { if (name == null) { throw new NullPointerException("name"); } this.name = name; } }
  • 24.
  • 25.
    val public static voidmain(String[] args) { val list = new ArrayList<String>(); list.add("a"); list.add("b"); val item = list.get(0); System.out.println(item); } public static void main(String[] args) { final ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); final String item = list.get(0); System.out.println(item); }
  • 26.
  • 27.
    @Cleanup public static voidmain(String[] args) throws IOException { Path filePath = Paths.get(args[0]); @Cleanup BufferedReader reader = Files.newBufferedReader(filePath); System.out.println(reader.readLine()); } public static void main(String[] args) throws IOException { Path filePath = Paths.get(args[0]); BufferedReader reader = Files.newBufferedReader(filePath); try { System.out.println(reader.readLine()); } finally { if (Collections.singletonList(reader).get(0) != null) { reader.close(); } } }
  • 28.
  • 29.
    @Synchronized public class SynchronizedExample{ @Synchronized public static void hello() { System.out.println("hello"); } @Synchronized public void bye() { System.out.println("bye"); } } public class SynchronizedExample { private static final Object $LOCK = new Object[0]; private final Object $lock = new Object[0]; public static void hello() { synchronized (SynchronizedExample.$LOCK) { System.out.println("hello"); } } public void bye() { synchronized (this.$lock) { System.out.println("bye"); } } }
  • 30.
  • 31.
    @SneakyThrows public class SneakyThrowsExample{ @SneakyThrows public static void main(String[] args) { throw new Exception(); } } public class SneakyThrowsExample { public static void main(String[] args) { try { throw new Exception(); } catch (final Throwable $ex) { throw Lombok.sneakyThrow($ex); } } }
  • 32.
    delombok • delombok Lombok •delombok • https://projectlombok.org/features/delombok > java -jar lombok.jar delombok src -d src-delomboked
  • 33.
    • Lombok onMethd onParam •https://projectlombok.org/features/experimental/onX
  • 34.
  • 35.
    Lombok • • Lombok • Lombok Lombok •val @Cleanup @SneakyThrows (Lombok )
  • 36.