Type annotation of new features in Java 8_ Power node Java college sorting

Annotations have been widely used in many frameworks to simplify the configuration of programs. What is the controversial type annotation? Complex or convenient?

What is a type annotation

Before Java 8, annotations could only be used where they were declared, such as classes, methods, and attributes; In Java 8, annotations can be applied anywhere, such as:

It should be noted that type annotations are only syntax, not semantics, and will not affect the compilation time, loading time, and running time of Java. In other words, type annotations are not included when compiling into class files.

Role of type annotation

Take a look at the following code first

The above code is compiled through, but unsupportedoperationexception will be reported when running; NumberFormatException; NullPointerException exceptions. These are runtime errors;

Type annotations are used to support strong type checking in Java programs. With the plug-in check framework, runtime errors can be detected during compilation to improve code quality. This is what type annotations do.

check framework

The check framework is a third-party tool. When combined with Java type annotation, the effect is 1 + 1 > 2. It can be embedded into the javac compiler, used with ant and maven, or used as an eclipse plug-in. the address is http://types.cs.washington.edu/checker-framework/ 。

The check framework can find the place where the type annotation appears and check it. For example:

Compile the above class using javac

Compilation is passed, but if modified to

If you compile again, the

If you don't want to use type annotations to detect errors, you don't need a processor, just javac getstarted Java can be compiled. This is possible in Java 8 with type annotation support, but not in Java 5, 6 and 7, because the javac compiler does not know what @ nonnull is, but the check framework has a downward compatible solution, which is to annotate the type annotation nonnull with / * * /. For example, the above example is modified to

In this way, the javac compiler will ignore the comment block, but the javac compiler in the check framework can also detect nonnull errors. Through the type annotation + check framework, we can see that the runtime error can be found at compile time.

About JSR 308

JSR 308 wants to solve two problems in Java 1.5 annotations:

JSR 308 solves the above two problems by the following methods:

 expand the syntax of Java language to allow annotations to appear in more places. Including: Method receivers (e.g. public int size() @ readonly {...}), Generic parameters, arrays, type conversion, type testing, object creation, type parameter binding, class inheritance and throws clause. In fact, it is type annotation, which is now a feature of Java 8

 a more powerful annotation processor can be created by introducing pluggable type systems. The type checker analyzes the source code with type qualified annotations and generates a warning message in case of mismatches and other errors. In fact, some people object to jsr308 by the check framework, which is more complex and static, such as

Change to dynamic language

Some people agree that, in the final analysis, code is the "most fundamental" document. The comments included in the code clearly indicate the intention of the coder. When there is no timely update or omission, it is the intention information contained in the annotation that is most likely to be lost in other documents. Moreover, turning runtime errors to the compilation stage can not only accelerate the development process, but also save the time of checking bugs during testing.

summary

Not everyone likes this feature, especially today when dynamic languages are more popular. Fortunately, Java 8 does not force everyone to use this feature. Opponents can not use this feature. Some people or companies with high requirements for code quality can adopt JSR 308. After all, code is the "most basic" document. I agree with this sentence. Although the code will increase, it can make your code more expressive. What do you think of this feature? Let's express our opinions....

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>