Java Annotations Annotation Learning notes

Source: Internet
Author: User

Tag: string representing value equal retention type document SRC round

One, custom Note 1. Use keyword @interface2. By default, annotations can be decorated with classes, methods, interfaces, and so on. 3. The following is an example of a basic annotation:
  
  1. //注解中的成员变量非常像定义接口
  2. public @interface MyAnnotation {
  3. //不带有默认值
  4. String name();
  5. //带有默认值
  6. int age() default 20;
  7. }
4. Interpretation of 3 common methods in reflect
    • Getannotion (class<t> annotationclass) returns an annotation of the specified type that exists on the element, if no null is returned
    • Annotation[] Getannotations () returns all annotations on this element.
    • Boolean isannotationpersent (class<? extends Annotation> Annotationclass) determines whether the court contains notes on the type of formulation.
Two, Yuan annotation
  
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface ValueBind {
  4. filedType type();
  5. String value() default "SUSU";
  6. enum filedType {
  7. STRING, INT
  8. }
  9. }
Where @target and @Retention are the meta annotations.
    • There are several values @Retention:

    • These are usually retentionpolicy.runtime.

There are several values for @Target:
@Inherited indicates that the annotation is inherited. Third, Demo
1. Valuebind annotations
  
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface ValueBind {
  5. filedType type();
  6. String value() default "SUSU";
  7. enum filedType {
  8. STRING, INT
  9. }
  10. }


2. Person class
  
  1. public class Person implements Serializable {
  2. private String name;
  3. private int age;
  4. public String getName() {
  5. return name;
  6. }
  7. @ValueBind(type = ValueBind.filedType.STRING, value = "LCF")
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. @ValueBind(type = ValueBind.filedType.INT, value = "21")
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. @Override
  19. public String toString() {
  20. return "Person{" +
  21. "name=‘" + name + ‘\‘‘ +
  22. ", age=" + age +
  23. ‘}‘;
  24. }
  25. }

3. Main method
  
  1. import java.lang.reflect.Method;
  2. public class Main {
  3. public static void main(String[] args) throws Exception {
  4. Object person = Class.forName("Person").newInstance();
  5. //以下获取的是:类的注解
  6. // Annotation[] annotations = person.getClass().getAnnotations();
  7. Method[] methods = person.getClass().getMethods();
  8. for (Method method : methods) {
  9. //如果该方法上面具有ValueBind这个注解
  10. if (method.isAnnotationPresent(ValueBind.class)) {
  11. //通过这个注解可以获取到注解中定义的
  12. valuebind annotation = method getannotation valuebind class
  13. if (annotation.type().equals(ValueBind.filedType.STRING)) {
  14. method.invoke(person, annotation.value());
  15. } else {
  16. method.invoke(person, Integer.parseInt(annotation.value()));
  17. }
  18. }
  19. }
  20. System.out.println(person);
  21. }
  22. }
Output: Person{name= ' LCF ', age=21}






Java Annotations Annotation Learning notes

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.