Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lombokの紹介

 Lombokの紹介

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

onozaty

July 20, 2017
Tweet

More Decks by onozaty

Other Decks in Programming

Transcript

  1. 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; } }
  2. @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; } }
  3. @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 + ")"; } }
  4. @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() { … } }
  5. @AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor • ֤छίϯετϥΫλΛ࡞੒ • @AllArgsConstructorͰશͯͷϑΟʔϧυΛҾ਺ ͱͨ͠ίϯετϥΫλ • @NoArgsConstructorͰҾ਺ͳ͠ίϯετϥΫλ

    • @RequiredArgsConstructorͩͱɺfinal͕෇͍ͯ ͍ΔҾ਺ͷΈͷίϯετϥΫλ(final͕͍͍ͭͯΔ ͷ͕ͳ͚Ε͹ɺҾ਺ͳ͠ίϯετϥΫλʹͳΔ)
  6. @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; } }
  7. @Data • ԼهͷΞϊςʔγϣϯΛશͯࢦఆͨ͠ͷͱಉ͡ • @ToString • @EqualsAndHashCode • @Getter •

    @Setter • @RequiredArgsConstructor • γϯϓϧͳσʔλΫϥεʹର͢Δఆܕతͳ΋ ͷΛ·ͱΊͯੜ੒͢Δ͜ͱ͕Ͱ͖Δ
  8. @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() { … } }
  9. @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() { … } }
  10. @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; }
  11. @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."); } }
  12. @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; } }
  13. 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); }
  14. @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(); } } }
  15. @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"); } } }
  16. @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); } } }