Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lombokの紹介

1,794 views

Published on

Java開発においてとても便利なLombokを説明します。

Published in: Software
  • Be the first to comment

Lombokの紹介

  1. 1. Lombok 2017-07-20 onozaty
  2. 2. Lombok? • Java ( ) • • getter/setter • equals, hashCode • toString •
  3. 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. 4. Lombok? • IDE IDE
  5. 5. • Lombok ( ) • IDE • Eclipse Lombok jar Eclipse ( )
  6. 6. @Getter, @Setter • getter/setter • ( ) • ( public) • final setter ( )
  7. 7. @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; } }
  8. 8. @ToString • toString •
  9. 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. 10. @EqualsAndHashCode • equals hashCode • • Lombok
  11. 11. @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() { … } }
  12. 12. @AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor • • @AllArgsConstructor • @NoArgsConstructor • @RequiredArgsConstructor final (final )
  13. 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. 14. @Data • • @ToString • @EqualsAndHashCode • @Getter • @Setter • @RequiredArgsConstructor •
  15. 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. 16. @Value • final • @ToString • @EqualsAndHashCode • @Getter • @RequiredArgsConstructor • Immutable
  17. 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. 18. @Builder • Builder (GoF Effective Java ) Builder • Builder
  19. 19. @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; }
  20. 20. @Slf4j • log static Logger • Slf4j Apache commons log(@Log) • Logger
  21. 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. 22. @NonNull • null (null NullPointerException ) • Objects.requireNonNull
  23. 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. 24. val • • Scala val
  25. 25. 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); }
  26. 26. @Cleanup • ( ) close • try/finally • close
  27. 27. @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(); } } }
  28. 28. @Synchronized • synchronized • static Lock • Lock
  29. 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. 30. @SneakyThrows • ( ) • • • https://github.com/rzwitserloot/lombok/blob/master/src/core/ lombok/Lombok.java#L49 • Lombok Lombok jar
  31. 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. 32. delombok • delombok Lombok • delombok • https://projectlombok.org/features/delombok > java -jar lombok.jar delombok src -d src-delomboked
  33. 33. • Lombok onMethd onParam • https://projectlombok.org/features/experimental/onX
  34. 34. Lombok • • • getter/setter • • equals hashCode
  35. 35. Lombok • • Lombok • Lombok Lombok • val @Cleanup @SneakyThrows (Lombok )
  36. 36. • Lombok • https://projectlombok.org/features/all

×