Java annotation practice

Source: Internet
Author: User
  • Class Using annotaion
package me.andy.practice.annotation;import oracle.jrockit.jfr.openmbean.RecordingType;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface ClassAnnotation {    String name();}

  • How to Use Annotation
package me.andy.practice.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface MethodAnnotation {    String value();    Class<?> type() default java.lang.String.class;}

  • Attribute use Annotation
package me.andy.practice.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface FiledAnnotation {    String value();}

  • Code Description

@ Interface indicates that this is an annotation type

@ Retention three policies are provided

Retentionpolicy. runtime: not only in the source code stage, but also in the JVM where the annotation confidence is added during the runtime.

Retentionpolicy. Class: in the source code stage, the annotation information is saved during the compilation stage. Annotation @ retention default value.

Retentionpolicy. Source: saves annotation information in the source code stage.

@ Target indicates what elements in the Java structure use annotations

Type, field, method, parameter, constructor, local_variable, package ), the type can be used in the class, interface, Enum, and annotation types.

  • Practice, object initialization
package me.andy.practice.annotation;import java.lang.reflect.InvocationTargetException;@ClassAnnotation(name = "andy")public class Home {    public Home() throws InvocationTargetException, IllegalAccessException {        super();        AnnotationUtils.init(this);    }    @FiledAnnotation(value = "name")    private String name;    @FiledAnnotation(value = "7")    private int count;    public String getName() {        return name;    }    @MethodAnnotation(value = "andy")    public void setName(String name) {        this.name = name;    }    public int getCount() {        return count;    }    @MethodAnnotation(type=java.lang.Integer.class,value = "7")    public void setCount(int count) {        this.count = count;    }}
package me.andy.practice.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class AnnotationUtils {    public static void init(Object o) throws InvocationTargetException, IllegalAccessException {        Class<? extends Object> aClass = o.getClass();        Method[] declaredMethods = aClass.getDeclaredMethods();        for (Method method : declaredMethods) {            if (method.isAnnotationPresent(MethodAnnotation.class)) {                System.out.println(method.getName());                MethodAnnotation annotation = method.getAnnotation(MethodAnnotation.class);                Class<?> type = annotation.type();                System.out.println(type);                System.out.println(annotation.value());                if (type.equals(java.lang.Integer.class)) {                    method.invoke(o, Integer.valueOf(annotation.value()));                } else {                    method.invoke(o, annotation.value());                }            }        }    }}

package me.andy.practice.annotation;import org.junit.Test;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import static junit.framework.Assert.assertEquals;public class AnnotationTest {    @Test    public void should_test_utils() throws InvocationTargetException, IllegalAccessException {        Home home = new Home();        AnnotationUtils.init(home);        assertEquals("andy", home.getName());        assertEquals(7, home.getCount());    }}

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.