Type annotations for Java 8: Tools and Opportunities

Source: Internet
Author: User

In previous Java versions, developers could only write annotations (Annotation) in a declaration. For Java 8, annotations can be written anywhere using a type, such as declarations, generics, and coercion of type conversions:

@Encrypted String data;
list< @NonNull string> strings;
Mygraph = (@Immutable Graph) tmpgraph;

At first glance, the type annotation is not the most dazzling feature of the new Java version. In fact, annotations are just syntax! Tools determine the semantics of annotations (that is, their meaning and behavior). This article introduces new annotation syntax and utilities to improve productivity and build higher-quality software.

In the financial sector, our market volatility and regulatory environment have determined that time-to-market is more important than ever. But sacrificing security or quality is definitely not an option: simple percentage points and base-point chaos can have serious consequences. The same is true in all other industries.

As a Java programmer, you may have used annotations to improve software quality. Think about the @override annotations that were introduced earlier in Java 1.5. In a large project with a complex inheritance hierarchy, it is difficult to track which implementation of the method the system is running on. If you accidentally modify the declaration of a method, it may cause the subclass method to be not invoked. This approach cancels a method call and introduces a flaw or security vulnerability. To do this, Java introduces the @override annotation, which the developer can use to show that the method overrides the parent class method. If the program does not match this intent, the Java compiler will use these annotations to warn the developer. Thus, annotations act as a form of machine-checking documents.

Developers can increase productivity through technologies such as metaprogramming (metaprogramming), where annotations play a central role. The idea is to use annotations to tell the tool how to generate new code, transform code, or determine the behavior of the runtime. Take the Java Persistence API (JPA) For example, which is also a feature introduced in Java 1.5. It allows developers to specify the relationship between Java objects and database entities in a declarative manner, such as @entity. These annotations can then be used by hibernate such tools to generate mapping files and SQL queries during the run time.

In JPA and hibernate scenarios, annotations are used to support the dry (Don ' t Repeat yourself) principles. Interestingly, no matter where you look for development tools that support best practices, it's not hard to find annotations. Some notable examples include the use of dependency injection (Dependency injection) to reduce coupling, and the separation of concerns using facet-oriented programming (Aspect oriented programming).

The question is: if annotations have been used to improve quality and productivity, why do we need type annotations?

The simple answer to this question is that the type annotation provides more functionality. They help detect more bugs and provide you with more control over the productivity tools.

Syntax for type annotations

In Java 8, type annotations can be written anywhere in the use type, and here are some examples:

@Encrypted String data
list< @NonNull string> strings
mygraph = (@Immutable Graph) tmpgraph;

Introducing a new type annotation is very simple, as long as you define an annotation, and its target is elementtype.type_PARAMETER or elementtype.type_ Use, or all two contain:

@Target ({elementtype.type_parameter, elementtype.type_use}) public
@interface Encrypted {}

elementtype.type_PARAMETER indicates that the annotation can be written in the declaration statement of the type variable (for example,class MyClass {...}). ). elementtype.type_Use indicates that annotations can be written in any statement that uses a type (for example, a type in a declaration statement, a generic, and a cast statement).

Once the type in the source code has annotations, as in the declaration, it can exist in the class file at the same time and can be obtained by reflection at runtime (defining annotations using Retentionpolicy.class or Retentionpolicy.runtime Policy). There are two main differences between type annotations and previous annotations: first, type annotations in a local variable declaration can also remain in the class file, followed by a complete generic that is reserved and accessible at run time.

Although annotations can be saved in a class file, it does not affect the general operation of the program. For example, a developer might declare two File variables and one Connection variable in the method body:

File File = ...;
@Encrypted File encryptedfile = ...;
@Open Connection Connection = ...;

When the program is running, pass any one of the files to connection Send (...) method, you will invoke the same method implementation.

The following code invokes the same method
connection.send (file);
Connection.send (Encryptedfile);

As you would expect, the runtime does not differ, that is, although the type of the parameter is annotated, the method is not overloaded based on the type of the annotation:

public class connection{
     void Send (@Encrypted file file) {...} 
     Impossible:
     //Void Send (File file) {...}
     . . .
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.