java自定义注解

1.@interface 关键字定义注解

2.元注解:

 就是定义注解的注解,包含@Target、@Retention、@Inherited、@Documented

 2.1 @Target

  注解使用目标,取值有:
  ElementType.PACKAGE 注解作用于包
  ElementType.TYPE 注解作用于类型(类,接口,注解,枚举)
  ElementType.ANNOTATION_TYPE 注解作用于注解
  ElementType.CONSTRUCTOR 注解作用于构造方法
  ElementType.METHOD 注解作用于方法
  ElementType.PARAMETER 注解作用于方法参数
  ElementType.FIELD 注解作用于属性
  ElementType.LOCAL_VARIABLE 注解作用于局部变量

 2.2 @Retention
  描述注解的生命周期,取值有:
  RetentionPolicy.SOURCE 源码中保留,编译期可以处理
  RetentionPolicy.CLASS Class文件中保留,Class加载时可以处理(默认)
  RetentionPolicy.RUNTIME 运行时保留,运行中可以处理

2.3 @Inherited
  标记注解,使用@Inherited修饰的注解作用于一个类,则该注解将被用于该类的子类

2.4 @Documented
  描述注解可以文档化,是一个标记注解。在生成javadoc的时候,生成的文档就包含该注解

3.案例:

package autoannotation;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;

@Target(ElementType.TYPE) // 注解作用于类型(类,接口,注解,枚举)
@Retention(RetentionPolicy.RUNTIME) // 运行时保留,运行中可以处理
@Inherited // 使用@Inherited修饰的注解作用于一个类,则该注解将被用于该类的子类
@Documented // 描述注解可以文档化,是一个标记注解
@interface setClassMsg {
	String DEFAULT_MSG = "classmsg";

	String msg() default DEFAULT_MSG;
}

@Target(ElementType.METHOD) // 注解作用于方法
@Retention(RetentionPolicy.RUNTIME) // 运行时保留,运行中可以处理
@Inherited // 使用@Inherited修饰的注解作用于一个类,则该注解将被用于该类的子类
@Documented // 描述注解可以文档化,是一个标记注解
@interface setMethodMsg {
	String DEFAULT_MSG = "methodmsg";

	String value() default DEFAULT_MSG;
}

@Target(ElementType.FIELD) // 注解作用于属性
@Retention(RetentionPolicy.RUNTIME) // 运行时保留,运行中可以处理
@Inherited // 使用@Inherited修饰的注解作用于一个类,则该注解将被用于该类的子类
@Documented // 描述注解可以文档化,是一个标记注解
@interface setFiledMsg {
	String DEFAULT_MSG = "fieldmsg";

	String name() default DEFAULT_MSG;
}

@Target(ElementType.PARAMETER) // 注解作用于参数
@Retention(RetentionPolicy.RUNTIME) // 运行时保留,运行中可以处理
@Inherited // 使用@Inherited修饰的注解作用于一个类,则该注解将被用于该类的子类
@Documented // 描述注解可以文档化,是一个标记注解
@interface setParamMsg {
	String DEFAULT_MSG = "parammsg";

	String value() default DEFAULT_MSG;
}

@setClassMsg
public class test {
	@setFiledMsg(name = "myname")
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@setMethodMsg(value = "mymsg")
	public String getMsg(@setParamMsg(value = "自定义值") String value) {

		return value;
	}

	public static void main(String[] args) {
		test t = new test();
		Class tClass = t.getClass();

		// 1.获取指定类型的注解
		setClassMsg msg = (setClassMsg) tClass.getAnnotation(setClassMsg.class);
		System.out.println(msg.msg());

		Method[] methods = tClass.getDeclaredMethods();
		for (Method method : methods) {
			// 2.获取方法注解
			setMethodMsg methodInfo = method.getAnnotation(setMethodMsg.class);
			if (methodInfo != null) {
				System.out.println(methodInfo.value());
				System.out.println(t.getMsg(methodInfo.value()));
			}
			// 3.获取方法参数注解
			Annotation[][] ps = method.getParameterAnnotations();
			for (Annotation[] a : ps) {
				if (a.length > 0) {
					setParamMsg aa = (setParamMsg) a[0];
					System.out.println(aa.value());
				}
			}
		}

		// 4.获取属性注解值
		Field[] fields = tClass.getDeclaredFields();
		for (Field field : fields) {
			setFiledMsg fieldInfo = field.getAnnotation(setFiledMsg.class);
			if (fieldInfo != null) {
				t.setName(fieldInfo.name());
			}
		}
		System.out.println(t.getName());
	}

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值