Share
Embed
Transcript
36 slides
-
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; } } -
@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 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 @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 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 @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 @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 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 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 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ΛקΊΔཧ༝ • هड़͢ΔίʔυྔΛݮΒͤΔ • ίʔυͷهड़ϛεΛ͛Δ • ϑΟʔϧυ໊มΘͬͨͷʹɺgetter/setterͷ໊લม͑ ΕͨΓͱ͔ɺɺ • όά͕ೖΓࠐΈͮΒ͘ͳΔ
• ϑΟʔϧυ͕૿͑ͨ࣌ʹɺequalshashCodeͷมߋ ͕࿙ΕͨΓͱ͔ɺɺ -
LombokΛקΊΔཧ༝ • ಋೖ؆୯ͳͷͰੵۃతʹ͍͍ͨ • ࣗ͏Lombokແ͠Ͱίʔυॻ͖ͨ͘ແ͍ • ͨͩ͠ɺͳΜͰLombokͰॻ͔ͳ͚Ε͍͚ͳ ͍ͱ͍͏Θ͚Ͱͳ͘ɺLombokͰॻ͍ͨํ͕ྑ ͍ͱࢥ͏ͷΛ͑ྑ͍ͱࢥ͏ •
ݸਓతʹɺvalͱ@Cleanupɺ@SneakyThrowsࠓ· Ͱͬͨ͜ͱ͕ͳ͘ɺࠓޙΘͳͦ͏(LombokͰॻ ͘·Ͱແ͍ؾ͕͢Δ)