Java Create you own Annotations and Using Them

如何创建一个Annotation呢?其实也是很简单的。

 

有几个关键词

    @Target(ElementType.PACKAGE), package header
    @Target(ElementType.TYPE), class header
    @Target(ElementType.CONSTRUCTOR), constructor header
    @Target(ElementType.METHOD), method header
    @Target(ElementType.FIELD), for class fields only
    @Target(ElementType.PARAMATER), for method parameters only
    @Target(ElementType.LOCAL_VARIABLE), for local variables only

 

@Retention – specifies the retention level of this annotation.

    RetentionPolicy.SOURCE—Retained only at the source level and will be ignored by the compiler
    RetentionPolicy.CLASS—Retained by the compiler at compile time, but will be ignored by the VM
    RetentionPolicy.RUNTIME—Retained by the VM so they can be read only at run-time 

 

@Documented – by default annotations are mentioned in java doc, this meta-annotation will make this annotation to be mentioned.

 

@Inherited – Indicates that the annotation will automatically be inherited (take a look at the example attached to this post). 

 

下面给一个完整的例子:

package com.java.chenhailong;

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 InvokeMultiple {
    int numberOfTimesToInvoke() default 1;
}

 

package com.java.chenhailong;

public class TestObject
{

    private String firstName;

    private String lastName;

    public TestObject(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @InvokeMultiple(numberOfTimesToInvoke = 3)
    public void printFirstName()
    {
        System.out.println(firstName);
    }

    @InvokeMultiple(numberOfTimesToInvoke = 6)
    public void printLastName()
    {
        System.out.println(lastName);
    }

    @InvokeMultiple
    public void printMessage()
    {
        System.out.println("printed only once");
    }

    public void printSecret()
    {
        System.out.println("this will not be printed");
    }
}

 

package com.java.chenhailong;

import java.lang.reflect.Method;

public class MainOther
{

    public static void main(String[] args)
    {
        TestObject obj = new TestObject("chen", "chen");
        invokeThis(obj);
    }

    public static void invokeThis(Object theObject)
    {
        try
        {
            Method[] methods = Class.forName(theObject.getClass().getName())
                    .getMethods();

            for (int i = 0; i < methods.length; i++)
            {
                InvokeMultiple invokeMultiple = methods[i]
                        .getAnnotation(InvokeMultiple.class);
                if (invokeMultiple != null)
                {
                    int numberOfTimesToInvoke = invokeMultiple
                            .numberOfTimesToInvoke();
                    for (int j = 0; j < numberOfTimesToInvoke; j++)
                    {
                        methods[i].invoke(theObject, null);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

 

你可能感兴趣的:(Annotations)