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