天天看点

java开发工具j2se_Java常用开发工具

IDE

推荐Intelij IDEA,有社区版本的,可以免费使用。同时还与很多语法和编译器的支持,比如Markdown、VM、Bash、Thrift、Protobuf。

lombok

开发环境

插件安装

集成开发环境中,需要安装对应的开发插件, 插件安装参照对应的集成开发环境中插件的安装方法,搜索lombok,安装。

maven库

//如果使用了@Log相关的注解,需要添加对应的库

log4j

log4j

1.2.12

org.projectlombok

lombok

1.16.4

启用注解处理

java开发工具j2se_Java常用开发工具

注解说明

val

Finally! Hassle-free final local variables.

会自动解析变量的返回类型,并且返回的变量列行是final类型。final String foo = example.get(0); var foo = example.get(0);

@NonNull

How I learned to stop worrying and love the NullPointerException.

常用于函数参数的设置,相比Java自带的注解,不需要写任何的check代码。

public NonNullExample(@NonNull Person person) {

this.name = person.getName();

}

//相当于

public NonNullExample(@NonNull Person person) {

if (person == null) {

throw new NullPointerException("person");

}

this.name = person.getName();

}

@Cleanup

Automatic resource management: Call your close() methods safely with no hassle.

@Cleanup InputStream in = new FileInputStream(args[0]);

//当in变量超出作用域后,会调用对应的close方法。如果没有close方法,也可以指定对应的处理方法, @Cleanup("method name")

@Getter / @Setter

Never write public int getFoo() {return foo;} again.

@ToString

No need to start a debugger to see your fields: Just let lombok generate a toString for you!

@ToString(callSuper=true, includeFieldNames=true)

@EqualsAndHashCode

Equality made easy: Generates hashCode and equals implementations from the fields of your object.

@EqualsAndHashCode

@EqualsAndHashCode(exclude={"id", "shape"}) //不对id,shape属性进行比较

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

Constructors made to order: Generates constructors that take no arguments, one argument per final / non-null field, or one argument for every field.

@Data

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

@Value

Immutable classes made very easy.

@Builder

The @Builder annotation produces complex builder APIs for your classes. 可以在程序中使用 Person.builder().name(“Adam Savage”).build();这样的类似的方法来构建对应的实体。

@Builder

public class BuilderExample {

private String name;

private int age;

@Singular private Set occupations;

}

集合类型中需要@Singular注解,lombok中支持的类型包括:

java.util:

Iterable, Collection, and List (backed by a compacted unmodifiable ArrayList in the general case).

Set, SortedSet, and NavigableSet (backed by a smartly sized unmodifiable HashSet or TreeSet in the general case).

Map, SortedMap, and NavigableMap (backed by a smartly sized unmodifiable HashMap or TreeMap in the general case).

Guava's com.google.common.collect:

ImmutableCollection and ImmutableList (backed by the builder feature of ImmutableList).

ImmutableSet and ImmutableSortedSet (backed by the builder feature of those types).

ImmutableMap, ImmutableBiMap, and ImmutableSortedMap (backed by the builder feature of those types).

@SneakyThrows

To boldly throw checked exceptions where no one has thrown them before!

@Synchronized

@Getter(lazy=true)

Laziness is a virtue!使用了getter这个annotation可以在实际使用到cached的时候生成cached,同时,Lombok会自动去管理线程安全的问题,不会存在重复赋值的问题。

@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;

}

@Log

Captain’s Log, stardate 24435.7: “What was that line again?”

Configuration system

Lombok, made to order: Configure lombok features in one place for your entire project or even your workspace.

J2SE中注解