How to get annotation (class, method, domain, parameter, constructor)

Source: Internet
Author: User

Paste the code First:

Import java.lang.annotation.Annotation;
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;

public class Annotationtest {

/**
* @param args
* @throws nosuchmethodexception
* @throws SecurityException
* @throws nosuchfieldexception
*/
public static void Main (string[] args) throws SecurityException,
Nosuchmethodexception, Nosuchfieldexception {

class<?> userannclass = Usemyannotation.class;
if (Userannclass.isannotationpresent (Myannotation.class)) {
System.out.println (Userannclass.getannotation (Myannotation.class). value ());
}

constructor<?>[] constructorlist = Userannclass.getconstructors ();
for (constructor<?> c:constructorlist) {
if (C.isannotationpresent (Myannotation.class)) {
System.out.println (C.getannotation (Myannotation.class). value ());
} else {
System.out.println ("This constructor has no annotations");
}
}

method = Userannclass.getdeclaredmethod ("GetName");
if (Method.isannotationpresent (Myannotation.class)) {
System.out.println (Method.getannotation (Myannotation.class). value ());
}

Method method1 = Userannclass.getdeclaredmethod ("SetName", String.class);
annotation[][] Parann = Method1.getparameterannotations ();
For (annotation[] Par:parann) {
for (Annotation P:par) {
Myannotation my = (myannotation) p;
System.out.println (My.value ());
}
}
Field field = Userannclass.getdeclaredfield ("name");
if (Field.isannotationpresent (Myannotation.class)) {
System.out.println (Field.getannotation (Myannotation.class). value ());
}

}

}

/*
* To save space, write all the methods together. Elementtype.constructor acting on a constructor Elementtype.field action on a domain/property
* Elementtype.method action on Method Ele Menttype.parameter parameters used for methods Elementtype.type
* are used to describe classes, interfaces (including annotation types) or enum declarations, most commonly
*
* elementtype.package are used to describe package Pac Kage annotations must is in file
* Package-info.java
*//elementtype.local_variable is a local variable in the method. However, the current Javac does not save the annotation information in the local
* variable in bytecode,
* So it is not possible to get the annotaion at runtime. This means that elementtype.local_variable can only be used in Retentionpolicy
*. The source case. The local variable in the Elementtype.local_variable method
*/
@Documented
@Retention (retentionpolicy.runtime)
@Target ({ Elementtype.package, Elementtype.type, Elementtype.constructor,
Elementtype.method, ElementType.PARAMETER, Elementtype.field})
@Inherited
@interface myannotation {
String value () default "";
}

@MyAnnotation ("I came from type annotations")
Class Usemyannotation {
@MyAnnotation ("I'm from Field notes")
Private String name = "Apple";
private int price = 0;

@MyAnnotation ("I'm from constructor (without reference)")
Public Usemyannotation () {
}

@MyAnnotation ("I'm from Constructor (annotated)")
Public usemyannotation (String name) {
THIS.name = name;
}

Public Usemyannotation (String name, int price) {
THIS.name = name;
This.price = Price;
}

@MyAnnotation ("I am from method annotations")
Public String GetName () {
return name;
}

@MyAnnotation ("I am from method annotations")
public void SetName (@MyAnnotation ("I came from parameter annotations") String name) {
THIS.name = name;
}

public int GetPrice () {
return price;
}

public void Setprice (int. price) {
This.price = Price;
}
}

Execution Result:

I'm from type annotations
I'm from constructor (no reference) annotations
I am from constructor (with reference) annotations
This constructor has no comments
I'm from the method annotation
I'm from parameter annotation.
I'm from Field notes.

Description: Java annotations are metadata equivalent to labeling Java methods, classes, and domains. If it is not handled then the equivalent of no, if processed, can be configured according to annotations can be reflected on the original class, method, domain processing.

Retentionpolicy.runtime annotations exist in the class bytecode file and can be obtained by reflection at run time, especially when the system architecture

Retentionpolicy.class The default retention policy, annotations exist in the CLASS bytecode file, but are not available at runtime

Retentionpolicy.source annotations only exist in the source code, in the class bytecode file does not contain @override, etc.

How to get annotation (class, method, domain, parameter, constructor)

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.