Annotations to the Java basics

Source: Internet
Author: User

1.JDK Common annotations: @Overload, @Deprecated, @Suppvisewarnings

2. Annotation classification: source annotations, compile-time annotations, runtime annotations, meta annotations (annotations), identifying annotations (annotations without members)

3. Meta-annotations

@Target ({elementtype.method, elementtype.type})//scope
@Retention (Retentionpolicy.runtime)//Annotated Safety lifecycle classification
@Inherited//Allow subclass inheritance
@Documented//Generate doc with annotated information

4. Custom notation:

1. Defining Annotations with @interface
2. Members must be declared with no arguments or exceptions
3. The member type must be the original class or String, class, Annotation, enumeration
4. When the annotation has only one member, the member name must be value (), and the member name and "=" can be ignored when the value is assigned
5. You can assign a default value to a member with default
6. The annotated class can only inherit the annotated class in effect and the interface is invalid.

5. Definition of annotations

@Target ({elementtype.method, elementtype.type})//Scope @retention (Retentionpolicy.runtime)//Annotated Security lifecycle classification @inherited// Allow subclasses to inherit @documented//information that contains annotations when the doc is generated public @interface Demo {//Use @interface to define annotations         String desc ();//Members must be declared without arguments or exceptions        //object haha ();//The member type must be the original class or String, class, Annotation, Enumeration    //    string value () default "hah";// When the annotation has only one member, the member name must be value (), the member name can be assigned and the "="        int age () default 18;//can be assigned defaults for the member with default     } 

6. Use of custom annotations

  

@Demo (desc= "Annotations on Analytic Classes") classpassann{@Demo (desc= "Annotation on parsing method", age=20Public void test () {}} @Demo (desc= "haha", age =) private static void test1 () { Try {Class forname = Class.forName ("Com.itcast.test.test4$passann"); Boolean isexist = Forname.isannotationpresent (Demo.class); if (isexist) {Demo demo1 = (demo) forname.getannotation (Demo.class ); SYSTEM.OUT.PRINTLN ("desc:" +demo1.desc () + ", Age:" +demo1.age ());} Method[] methods = forname.getmethods (); for (Method method:methods) {annotation[] annotations = Method. Getannotations (); For (Annotation annotation:annotations) {if (Annotation instanceof Demo) {Demo Demo2 = (demo) annotatio N SYSTEM.OUT.PRINTLN ("desc:" +demo2.desc () + ", Age:" +demo2.age ());}}}} Catch (Exception e) {e.printstacktrace ();}}            

Annotations of Java Basics

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.