lombok使用
lombok通过增加简单的注解就可以在class中增加对应的方法。不同于作用范围为runtime的注解,lombok注解会改变生成的class。
idea中使用lombok,不仅要下载lombok插件,还需要以pom等方式引入lombok的jar包。按快捷键 ctrl+F12可以查看生成的方法。
lombok实现了JSR 269 API规范,只要程序实现了该API,就能在javac运行的时候得到调用。
@Getter/@Setter
@Getter @Setter private int age = 10; @Setter(AccessLevel.PROTECTED) private String name; private int age = 10; public void setAge(int age) { this.age = age; } public int getAge() { return age; } protected void setName(String name) { this.name = name; }
@NonNull
@NonNull private String description;
public NonNullExample(@NonNull Person person) {
this.description = person.getName();
}
public NonNullExample(@NonNull Person person) {
if (person == null) {
throw new NullPointerException("person");
}
this.name = person.getName();
}
@Cleanup
@Cleanup InputStream in = new FileInputStream(); //try with try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); }
@ToString
@ToString(exclude={"id","address"}) public class ToStringExample {
@EqualsAndHashCode
@EqualsAndHashCode(exclude={"id", "shape"}) public class EqualsAndHashCodeExample {
@NoArgsConstructor
@RequiredArgsConstructor ,@AllArgsConstructor
@RequiredArgsConstructor(staticName = "of") @AllArgsConstructor(access = AccessLevel.PROTECTED) public class ConstructorExample { private int x, y; @NonNull private String description; public static ConstructorExample of(String description) { return new ConstructorExample(description); } protected ConstructorExample(int x, int y, String description) { if (description == null) throw new NullPointerException("description"); this.x = x; this.y = y; this.description = description; }
@RequiredArgsConstructor会生成一个包含常量(final),和标识了@NotNull的变量 的构造方法。非final,非@NotNull不作为参数。
@Data
等于@ToString,@EqualsAndHashCode,所有字段的 @Getter 所有非final字段的@Setter ,@RequiredArgsConstructor
@Data public class DataExample { private final String name; @Setter(AccessLevel.PACKAGE) private int age; private double score; private String[] tags;
@Value
@value是@data的不可变对象 。所有字段都是私有的,默认情况下是final的,并且不会生成setter。默认情况下,类本身也是final的,因为不可变性不能强制转化为子类。与@data一样,有用toString()、equals()和hashCode()方法也是生成的,每个字段都有一个getter方法,并且一个覆盖每个参数的构造器也会生成。
@SneakyThrows
@SneakyThrows(UnsupportedEncodingException.class) public String utf8ToString(byte[] bytes) { return new String(bytes, "UTF-8"); } try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { throw Lombok.sneakyThrow(e); }
@Synchronized
private final Object readLock = new Object();
@Synchronized
public static void hello() {
System.out.println("world");
}
@Synchronized("readLock")
public void foo() {
System.out.println("bar");
}
private final Object readLock = new Object();
private static final Object $LOCK = new Object[0];
public static void hello() { synchronized($LOCK) { System.out.println("world"); } } public void foo() { synchronized(readLock) { System.out.println("bar"); } }
@Getter(lazy=true)
如果getter方法计算值需要大量CPU,或者值占用大量内存,第一次调用这个getter,它将一次计算一个值,然后从那时开始缓存它
public class GetterLazyExample { @Getter(lazy=true) private final double[] cached = expensive(); private double[] expensive() { double[] result = new double[1000000]; for (int i = 0; i < result.length; i++) { result[i] = Math.asin(i); } return result; } } public class GetterLazyExample { private final java.util.concurrent.AtomicReference<java.lang.Object> cached = new java.util.concurrent.AtomicReference<java.lang.Object>(); public double[] getCached() { java.lang.Object value = this.cached.get(); if (value == null) { synchronized(this.cached) { value = this.cached.get(); if (value == null) { final double[] actualValue = expensive(); value = actualValue == null ? this.cached : actualValue; this.cached.set(value); } } } return (double[])(value == this.cached ? null : value); } private double[] expensive() { double[] result = new double[1000000]; for (int i = 0; i < result.length; i++) { result[i] = Math.asin(i); } return result; } }
@CommonsLog
@Log public class LogExample { public static void main(String... args) { log.error("Something's wrong here"); } } @Slf4j public class LogExampleOther { public static void main(String... args) { log.error("Something else is wrong here"); } } @CommonsLog(topic="CounterLog") public class LogExampleCategory { public static void main(String... args) { log.error("Calling the 'CounterLog' with a message"); } }
public class LogExample { private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); public static void main(String... args) { log.error("Something's wrong here"); } } public class LogExampleOther { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class); public static void main(String... args) { log.error("Something else is wrong here"); } } public class LogExampleCategory { private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog"); public static void main(String... args) { log.error("Calling the 'CounterLog' with a message"); }