Java基础Demo -- Annotation注解

注解小示例,主要讲解7种方法的用法:

返回值方法说明
booleanisAnnotationPresent(MyAnno.class)钉点上是否有该注解
MyAnnogetAnnotation(MyAnno.class)获取某注解
Annotation[ ]getAnnotations()返回所有注解
MyAnno[ ]getAnnotationsByType(MyAnno.class)

获取jdk8的重复注解

兼容getAnnotation

抛开@Inherited继承性,直接钉点上就出现的注解(getDeclare*的若干方法)
MyAnnogetDeclaredAnnotation(MyAnno.class)
Annotation[ ]getDeclaredAnnotations()
MyAnno[ ]getDeclaredAnnotationsByType(MyAnno.class)
import java.lang.reflect.*;
import java.lang.annotation.*;

//源码阶段的注解
@Retention(RetentionPolicy.SOURCE)
@interface Becareful{
	int message();
}

//编译阶段的注解
@Retention(RetentionPolicy.CLASS)
@interface What{
	String description();
}

//运行阶段的注解:空注解:也即标记注解
@Retention(RetentionPolicy.RUNTIME)
@interface Id{}

//运行阶段的注解:单属性注解
@Retention(RetentionPolicy.RUNTIME)
@interface Singleton{
	String value() default "singleton";
}

//运行阶段的注解:多属性注解,有的属性携带默认值
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno{
	String str() default "12345";
	int val();
}


//运行阶段的注解:
//钉在类上的注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface ClassAnno{
	String classanno() default "classanno";
}

//运行阶段的注解:
//钉在父类上的注解:
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface ParentAnno{
	String parent() default "parent";
}


//容器
@Retention(RetentionPolicy.RUNTIME)
@interface Persons {
    Person[] value();
}

//容器内的属性数组,数组中的每个元素也是注解
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Persons.class)
@interface Person{
    char value() ;
}

//父类上的容器
@Retention(RetentionPolicy.RUNTIME)
@interface Fathers {
    Father[] value();
}

//父类上的容器内的属性数组,数组中的每个元素也是注解
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Fathers.class)
@interface Father{
    String father() default "";
}


@ParentAnno
@Father(father="father1")
@Father(father="father2")
class Parent //父类上的注解被子类继承
{
}

@ClassAnno
@Person('A')
@Person('B')
public class ChildAnnotationTest extends Parent
{
	@Id
	private int id;

	@Singleton
	private static ChildAnnotationTest instance = new ChildAnnotationTest();

	@Person('男')
	@Person('女')
	private char sex;

	public void testIsAnnotationPresent(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		System.out.println( clazz.getDeclaredField("id").isAnnotationPresent(Id.class) );
		System.out.println("clazz.getDeclaredField( id ).isAnnotationPresent(Id.class)\r\n");
		}catch(Exception e){}
	}

	public void testGetAnnotations(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		Annotation[] annos = clazz.getAnnotations() ;
		for(Annotation anno : annos)
			System.out.println( anno );
		System.out.println("clazz.getAnnotations()\r\n");
		}catch(Exception e){}
	}

	public void testGetDeclaredAnnotations(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		Annotation[] annos = clazz.getDeclaredAnnotations() ;
		for(Annotation anno : annos)
			System.out.println( anno );
		System.out.println("clazz.getDeclaredAnnotations()\r\n");
		}catch(Exception e){}
	}

	public void testGetAnnotation(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		System.out.println( clazz.getDeclaredField("instance").getAnnotation(Singleton.class) );
		System.out.println("clazz.getDeclaredField( instance ).getAnnotation(Singleton.class)\r\n");
		}catch(Exception e){}
	}

	public void testGetAnnotationsByType(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		Field f = clazz.getDeclaredField("sex");
		Person[] annos = f.getAnnotationsByType(Person.class);
		for(Person anno : annos)
			System.out.println( anno.value() );
		System.out.println("clazz.getDeclaredField( sex ).getAnnotationsByType(Persons.class)\r\n");
		}catch(Exception e){}
	}

	public void testGetDeclaredAnnotation(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		System.out.println( clazz.getDeclaredAnnotation(ParentAnno.class) );
		System.out.println("clazz.getDeclaredAnnotation(ParentAnno.class)\r\n");
		}catch(Exception e){}
	}

	public void testGetDeclaredAnnotationsByType(){
		try{
		Class<?> clazz = ChildAnnotationTest.class ;
		Father[] annos = clazz.getDeclaredAnnotationsByType(Father.class) ;
		for(Father anno : annos)
			System.out.println( anno );
		System.out.println("clazz.getDeclaredAnnotationsByType(Father.class)........empty!!!\r\n");
		Person[] as = clazz.getDeclaredAnnotationsByType(Person.class) ;
		for(Person anno : as)
			System.out.println( anno );
		System.out.println("clazz.getDeclaredAnnotationsByType(Person.class)\r\n");

		}catch(Exception e){}
	}

	public static void main(String[] args){
		
		ChildAnnotationTest child = new ChildAnnotationTest();
		try{
			Class<?> clazz = ChildAnnotationTest.class ;
			Method[] mtds = clazz.getDeclaredMethods();

			for(Method m : mtds){
				if(m.getName().equals("main")) continue;
				m.invoke(child);
			}
		}catch(Exception e){ 
			e.printStackTrace() ; 
		}
	}
}



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值