Thoughts on JAVA annotations

Source: Internet
Author: User


Since JDK1.5 introduced @ override, @ Deprecated, @ SuppressWarnings, the three annotations and custom annotations have started to develop in full swing. Now many frameworks support annotations, annotation can make our code look more concise, and to some extent, it solves the coupling between the original features of the class and the Extended features.

When @ Override is added, the current method is defined to overwrite the methods in the superclass. If not, an error is reported during compilation?

Why is @ Deprecated added? The compiler will issue a warning. This is a discarded code?

Why does the compiler stop issuing warnings when @ SuppressWarnings is added?


Why do I say: Annotations can make our code look more concise, and to some extent, resolve the coupling between the original features of the class and the Extended features?
I don't know when I hate posting large pieces of code on my blog or don't want to post it, but I still have to use code to explain these issues.
Let's write an annotation and you will understand what's going on.
Let's follow @ Deprecated and write a simple @ MyDeprecated, which achieves the same effect as @ Deprecated.
package com.jialin.test.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * @Author : JiaLin * @Group : TGB * @Date : 2014/6/9 * @Description : */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyDeprecated {    public String description() default "Warning:This method is Deprecated!";}

A brief description of the above meta annotation (meta annotation, which is the annotation)

@ Target

It indicates where the annotation can be used. Possible ElementType parameters include:

CONSTRUCTOR: CONSTRUCTOR Declaration

FIELD: FIELD Declaration (including enum instances)

LOCAL_VARIABLE: local variable Declaration

METHOD: METHOD declaration

PACKAGE: PACKAGE Declaration

PARAMETER: PARAMETER Declaration

TYPE: Class, interface (including annotation TYPE), or enum Declaration

@ Retention

Indicates the level at which the annotation information needs to be saved. Optional RetentionPolicy parameters include:

SOURCE: annotation will be discarded by the compiler

CLASS: Annotations are available in the class file, but are discarded by the VM.

RUNTIME: the VM retains the annotation during running, so the annotation information can be read through the reflection mechanism.

@ Document

Include annotation in Javadoc

@ Inherited

Allow subclass to inherit the annotation in the parent class



package com.jialin.test.annotation;import java.lang.reflect.Method;/** * @Author : JiaLin * @Group : TGB * @Date : 2014/6/9 * @Description : */public class MyClass {    @MyDeprecated    public String sayHelloWorld()    {        return "Hello,World!";    }    public String sayHelloJiaLin()    {        return "Hello,Jialin!";    }    public static void main(String[] args) {        testMyDeclared(MyClass.class);    }    public static void testMyDeclared(Class
  myClass) {        for (Method method : myClass.getDeclaredMethods()) {            MyDeprecated myDeprecated = method.getAnnotation(MyDeprecated.class);            if (myDeprecated != null) {                System.out.println(myDeprecated.description()+method.getName());            }        }    }}

Output result: Warning: This method is Deprecated! SayHelloWorld
After reading the code, I believe that there is no need to explain too many questions at the beginning of the article. In fact, the reason why I will study annotations is also because I have been thinking about how to decouple many parts of the project recently. Annotations are a good method, such as the same query method, with different annotations, the table to be queried is different or the results are different. Combined with enumeration, reflection, and interceptor, we can achieve this effect. The idea of Delayed Injection is as follows: we open up a static region as a container, store our enumerated values, and use the Interceptor to intercept the method to be executed, obtain the annotation of this method through reflection, and then match the annotation property with our enumeration value. If the matching succeeds, modify the parameter of the method.
Writing this, I suddenly realized that the IOC container of Spring is probably the same idea. It looks like it's time to read the source code of spring. Welcome to discuss it.



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.