lombok使用手册
lombok使用手册:
1、安装插件
1.1、idel:
elipse:下载地址https://projectlombok.org/download
运行jar文件
1.2、使用lombok还需要引用相关jar包
参考网址:https://projectlombok.org/features/all
2、注解说明
2.1、@Data
@Data
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!
Lombok代码:
@Data public class Lombok { private String name; private String age; }
编译后代码:
public class Lombok { private String name; private String age; public Lombok() { } public String getName() { return this.name; } public String getAge() { return this.age; } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof Lombok)) { return false; } else { Lombok other = (Lombok)o; if(!other.canEqual(this)) { return false; } else { String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name != null) { return false; } } else if(!this$name.equals(other$name)) { return false; } String this$age = this.getAge(); String other$age = other.getAge(); if(this$age == null) { if(other$age != null) { return false; } } else if(!this$age.equals(other$age)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Lombok; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null?43:$name.hashCode()); String $age = this.getAge(); result1 = result1 * 59 + ($age == null?43:$age.hashCode()); return result1; } public String toString() { return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")"; } }
2.2、@val
编译前代码:
val example = new ArrayList<String>(); example.add("Hello, World!"); val foo = example.get(0); return foo.toLowerCase(); val map = new HashMap<Integer, String>(); map.put(0, "zero"); map.put(5, "five"); for (val entry : map.entrySet()) { System.out.printf("%d: %s\n", entry.getKey(), entry.getValue()); }
编译后代码:
final ArrayList<String> example = new ArrayList<String>(); example.add("Hello, World!"); final String foo = example.get(0); return foo.toLowerCase(); final HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(0, "zero"); map.put(5, "five"); for (final Map.Entry<Integer, String> entry : map.entrySet()) { System.out.printf("%d: %s\n", entry.getKey(), entry.getValue()); }
2.3、@var 用法参考@val
2.4、@NonNull
编译前代码:
public NonNullExample(@NonNull Person person) { this.name = person.getName(); }
编译后代码:
public NonNullExample(@NonNull Person person) { if (person == null) { throw new NullPointerException("person is marked @NonNull but is null"); } this.name = person.getName(); }
2.5、@Cleanup
编译前代码:
@Cleanup InputStream in = new FileInputStream(args[0]); @Cleanup OutputStream out = new FileOutputStream(args[1]); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); }
编译后代码:
InputStream in = new FileInputStream(args[0]); try { OutputStream out = new FileOutputStream(args[1]); try { byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } finally { if (out != null) { out.close(); } } } finally { if (in != null) { in.close(); } }
2.6、@Getter、@Setter
编译前代码:
@Getter @Setter private int age = 10; @Setter private String name;
编译后代码:
public int getAge() { return age; } public void setAge(int age) { this.age = age; } protected void setName(String name) { this.name = name; }
2.7、@ToString
编译前代码:
@ToString public class Lombok { @Getter @Setter String name; @Getter @Setter private String age; @ToString.Exclude String id; }
编译后代码:
public class Lombok { String name; private String age; String id; public Lombok() { } public String toString() { return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")"; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getAge() { return this.age; } public void setAge(String age) { this.age = age; } }
2.8、@EqualsAndHashCode
编译前代码:
@EqualsAndHashCode public class Lombok { @Getter @Setter String name; @Getter @Setter private String age; @EqualsAndHashCode.Exclude String id; }
编译后代码:
public class Lombok { String name; private String age; String id; public Lombok() { } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof Lombok)) { return false; } else { Lombok other = (Lombok)o; if(!other.canEqual(this)) { return false; } else { String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name != null) { return false; } } else if(!this$name.equals(other$name)) { return false; } String this$age = this.getAge(); String other$age = other.getAge(); if(this$age == null) { if(other$age != null) { return false; } } else if(!this$age.equals(other$age)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Lombok; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null?43:$name.hashCode()); String $age = this.getAge(); result1 = result1 * 59 + ($age == null?43:$age.hashCode()); return result1; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getAge() { return this.age; } public void setAge(String age) { this.age = age; } }
2.9、@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor构造器
编译前代码:
@RequiredArgsConstructor(staticName = "of") @AllArgsConstructor(access = AccessLevel.PROTECTED) public class ConstructorExample<T> { private int x, y; @NonNull private T description; @NoArgsConstructor public static class NoArgsExample { @NonNull private String field; } }
编译后代码:
public class ConstructorExample<T> { private int x, y; @NonNull private T description; private ConstructorExample(T description) { if (description == null) throw new NullPointerException("description"); this.description = description; } public static <T> ConstructorExample<T> of(T description) { return new ConstructorExample<T>(description); } @java.beans.ConstructorProperties({"x", "y", "description"}) protected ConstructorExample(int x, int y, T description) { if (description == null) throw new NullPointerException("description"); this.x = x; this.y = y; this.description = description; } public static class NoArgsExample { @NonNull private String field; public NoArgsExample() { } } }
2.10、@Value用法参考@Data默认设置为final,会被其他注解显示生效
2.11、@Builder
编译前代码:
@Builder public class BuilderExample { @Builder.Default private long created = System.currentTimeMillis(); private String name; private int age; @Singular private Set<String> occupations; }
编译后代码:
public class BuilderExample { private long created; private String name; private int age; private Set<String> occupations; private static long $default$created() { return System.currentTimeMillis(); } BuilderExample(long created, String name, int age, Set<String> occupations) { this.created = created; this.name = name; this.age = age; this.occupations = occupations; } public static BuilderExample.BuilderExampleBuilder builder() { return new BuilderExample.BuilderExampleBuilder(); } public static class BuilderExampleBuilder { private boolean created$set; private long created$value; private String name; private int age; private ArrayList<String> occupations; BuilderExampleBuilder() { } public BuilderExample.BuilderExampleBuilder created(long created) { this.created$value = created; this.created$set = true; return this; } public BuilderExample.BuilderExampleBuilder name(String name) { this.name = name; return this; } public BuilderExample.BuilderExampleBuilder age(int age) { this.age = age; return this; } public BuilderExample.BuilderExampleBuilder occupation(String occupation) { if(this.occupations == null) { this.occupations = new ArrayList(); } this.occupations.add(occupation); return this; } public BuilderExample.BuilderExampleBuilder occupations(Collection<? extends String> occupations) { if(this.occupations == null) { this.occupations = new ArrayList(); } this.occupations.addAll(occupations); return this; } public BuilderExample.BuilderExampleBuilder clearOccupations() { if(this.occupations != null) { this.occupations.clear(); } return this; } public BuilderExample build() { Set occupations; switch(this.occupations == null?0:this.occupations.size()) { case 0: occupations = Collections.emptySet(); break; case 1: occupations = Collections.singleton(this.occupations.get(0)); break; default: LinkedHashSet occupations1 = new LinkedHashSet(this.occupations.size() < 1073741824?1 + this.occupations.size() + (this.occupations.size() - 3) / 3:2147483647); occupations1.addAll(this.occupations); occupations = Collections.unmodifiableSet(occupations1); } long created$value = this.created$value; if(!this.created$set) { created$value = System.currentTimeMillis(); } return new BuilderExample(created$value, this.name, this.age, occupations); } public String toString() { return "BuilderExample.BuilderExampleBuilder(created$value=" + this.created$value + ", name=" + this.name + ", age=" + this.age + ", occupations=" + this.occupations + ")"; } } }
2.12、@SneakyThrows
编译前代码:
@SneakyThrows(UnsupportedEncodingException.class) public String utf8ToString(byte[] bytes) { return new String(bytes, "UTF-8"); } @SneakyThrows public void run() { throw new Throwable(); }
编译后代码:
public String utf8ToString(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { throw Lombok.sneakyThrow(e); } } public void run() { try { throw new Throwable(); } catch (Throwable t) { throw Lombok.sneakyThrow(t); } }
2.13、@Synchronized
编译前代码:
public class SynchronizedExample { private final Object readLock = new Object(); @Synchronized public static void hello() { System.out.println("world"); } @Synchronized public int answerToLife() { return 42; } @Synchronized("readLock") public void foo() { System.out.println("bar"); } }
编译后代码:
public class SynchronizedExample { 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) { System.out.println("world"); } } public int answerToLife() { synchronized($lock) { return 42; } } public void foo() { synchronized(readLock) { System.out.println("bar"); } } }
2.14、@With 必须要包含全参数构造器
编译前代码:
@AllArgsConstructor public class Lombok { @With private Integer x; @With private Integer y; @With private Integer z; }
编译后代码:
public class Lombok { private Integer x; private Integer y; private Integer z; public Lombok(Integer x, Integer y, Integer z) { this.x = x; this.y = y; this.z = z; } public Lombok withX(Integer x) { return this.x == x?this:new Lombok(x, this.y, this.z); } public Lombok withY(Integer y) { return this.y == y?this:new Lombok(this.x, y, this.z); } public Lombok withZ(Integer z) { return this.z == z?this:new Lombok(this.x, this.y, z); } }
2.15、@Getter(lazy=true)缓存
编译前代码:
public class Lombok { @Getter(lazy=true) private final int[] cached = expensive(); private int[] expensive() { int[] result = new int[1000000]; for (int i = 0; i < result.length; i++) { result[i] = i; } return result; } }
编译后代码:
public class Lombok { private final AtomicReference<Object> cached = new AtomicReference(); public Lombok() { } private int[] expensive() { int[] result = new int[1000000]; for(int i = 0; i < result.length; result[i] = i++) { ; } return result; } public int[] getCached() { Object value = this.cached.get(); if(value == null) { AtomicReference var2 = this.cached; synchronized(this.cached) { value = this.cached.get(); if(value == null) { int[] actualValue = this.expensive(); value = actualValue == null?this.cached:actualValue; this.cached.set(value); } } } return (int[])((int[])(value == this.cached?null:value)); } }
2.16、日志@Log (and friends)
@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
@CustomLog
Creates private static final com.foo.your.Logger log = com.foo.your.LoggerFactory.createYourLogger(LogExample.class);
@Log public class Lombok { @Getter(lazy=true) private final int[] cached = expensive(); private int[] expensive() { int[] result = new int[1000000]; for (int i = 0; i < result.length; i++) { result[i] = i; } log.severe(result.toString()); return result; } }
2.17、额外的注解见链接https://projectlombok.org/features/all