java annotation(如何创建新的注解)小结

“注解”有三种

1:无实际参数,只有声明

2:只存在单一参数,有些像方法调用

3:有多个参数

 

标准的“注解”就先不总结了。

想总结一下《如何创建自己的注解》。有很多流行的框架都会用到,所以对以后的学习也会有帮助。

1.无实际参数,只有声明(表达某种含义)

import java.lang.annotation.Retention;
import java.lang.annotation.Target
import java.lang.annotation.RetentionPolicy;

/*
 * 独自开发的注解
 * @auther Z,wk
 * @version 1.0
 *
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SampleRequired{
}

2.有单一参数或多个参数

package sample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//
ElementType.TYPE 类的定义
@Target({ 
  ElementType.TYPE,
ElementType.FIELD,
ElementType.CONSTRUCTOR,
ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
public @interface Info {
   String value();
//此处还可以继续添加
//String value2();
}

3.使用方法

package sample;

import java.util.ArrayList;
import java.util.List;

// ElementType.TYPE :添加到类或接口的定义上 @Info(
"SampleClass1 Info") public class SampleClass1 { private List list; public SampleClass1(){ } @Override public boolean equals(Object obj){ return list.equals(obj); }   // ElmentType.Method:添加到方法声明上 @Info("hogehoge") public void initList(){ list = new ArrayList(); list.add(10); } }
package sample;

@Info("Sample2 class")
public class SampleClass2 {
    
//ElementType.FIELD : 添加到成员变量上 @Info(
"foo field") private Foo foo;
//ElementType.CONSTRUCTOR : 添加到构造方法上 @Info(
"default constract") public SampleClass2(){ foo = new Foo(); }
// ElementType.METHOD : 添加到方法声明上 @Info(
"bar method") public void bar() { foo.bar(); } }

4. 取得调用“注解”的对象(笔者理解为:很多框架使用注解的意义所在)

package sample;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Client {
    public static void main(String[] args){
        System.out.println("start annotation sample");
        SampleClass1 sc1 = new SampleClass1();
        SampleClass2 sc2 = new SampleClass2();

        //取得指定的“注解”
        Annotation annotList[] = Info.class.getAnnotations();
        System.out.println("annotation size is [" + annotList.length + "]");
        for(int i=0; i<annotList.length; i++){
            Annotation anno = annotList[i];
            System.out.println("    annotation class is [" + anno.annotationType().getName() + "]");
        }

        /****取得带有注解的CLASS****/
        //SampleClass1
        Annotation annotList1[] = SampleClass1.class.getAnnotations();
        System.out.println("SampleClass1's annotation size is [" + annotList1.length + "]");
        for(int i=0; i<annotList1.length; i++){
            Annotation anno1 = annotList1[i];
            System.out.println("    annotation class is [" + anno1.annotationType().getName() + "]");
        }
        //SampleClass2
        Annotation annotList2[] = SampleClass2.class.getAnnotations();
        System.out.println("SampleClass2's annotation size is [" + annotList2.length + "]");
        for(int i=0; i<annotList2.length; i++){
            Annotation anno2 = annotList2[i];
            System.out.println("    annotation class is [" + anno2.annotationType().getName() + "]");
        }
        System.out.println();
        System.out.println();

        /****取得带有注解的METHOD****/
        //SampleClass1
        Method methodList1[] = SampleClass1.class.getMethods();
        System.out.println("SampleClass1's method count is [" + methodList1.length + "]");
        for(Method method : methodList1){
            System.out.println("    method name is [" + method.getName() + "]");
            for(Annotation annot : method.getAnnotations()){
                System.out.println("        method annotation is [" + annot.annotationType().getName() + "]");
            }
        }
        //SampleClass2
        Method methodList2[] = SampleClass1.class.getMethods();
        System.out.println("SampleClass2's method count is [" + methodList2.length + "]");
        for(Method method : methodList2){
            System.out.println("    method name is [" + method.getName() + "]");
            for(Annotation annot : method.getAnnotations()){
                System.out.println("        method annotation is [" + annot.annotationType().getName() + "]");
            }
        }
        System.out.println();
        System.out.println();

        /****取得带有注解的FIELD(成员变量)****/
        //SampleClass1
        Field fieldList1[] = SampleClass1.class.getFields();
        System.out.println("SampleClass1 has  [" + fieldList1.length + "] fields");
        for(Field field : fieldList1){
            System.out.println("     field name is [" + field.getName() +"]");
            for(Annotation annot : field.getAnnotations()){
                System.out.println("        field annotation is [" + annot.annotationType().getName() + "]");
            }
        }
        //SampleClass1
        Field fieldList2[] = SampleClass2.class.getDeclaredFields();//getFields();
        System.out.println("SampleClass2 has  [" + fieldList2.length + "] fields");
        for(Field field : fieldList2){
            System.out.println("     field name is [" + field.getName() +"]");
            for(Annotation annot : field.getAnnotations()){
                System.out.println("        field annotation is [" + annot.annotationType().getName() + "]");
            }
        }
        System.out.println();
        System.out.println();

    }

}

本人属于初学者,有不对的地方希望过路的大神批评指正。

转载于:https://www.cnblogs.com/zwk-It-goodmorning/p/11117428.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值