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();
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值