java注解示例程序

时间:2022-05-04
本文章向大家介绍java注解示例程序,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

MyAnno.java

package com.yawn.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;

@Target({
	ElementType.METHOD, 
	ElementType.TYPE, 
	ElementType.FIELD, 
	ElementType.PARAMETER})
// 注解的生命周期(源码、编译时、运行时)
@Retention(RetentionPolicy.RUNTIME)
// 允许继承
@Inherited
// 生成javadoc时包含的注解的信息
@Documented
public @interface MyAnno {
	
	String desc() default "";
	String name();
	int age() default 18;
	
}

TestAnno.java

package com.yawn.annotation;

@MyAnno(name="yawn", desc="good boy")
public class TestAnno {
	
	@MyAnno(name="tempreture", desc="")
	private double tempreture;
	
	@MyAnno(name="iii")
	public String iii;
	
	@MyAnno(name="ageee")
	private Integer ageee;
	
	
	@MyAnno(name="method ", desc="desc")
	public void test(@MyAnno(name="id")String id, @MyAnno(name="size")int size){
		
		
	}

}

Test.java

package com.yawn.annotation;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import org.junit.Before;

public class Test {
	
	Class<?> clazz;
	
	@Before
	public void before() throws ClassNotFoundException{
		clazz = Class.forName("com.yawn.annotation.TestAnno");
	}
	
	/*
	 * 类上的注解
	 */
	@org.junit.Test
	public void test(){
		printAnnoInfo(clazz);
	}
	
	/*
	 * 成员变量上的注解
	 */
	@org.junit.Test
	public void test2() throws ClassNotFoundException{
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			printAnnoInfo(field);
		}
	}
	
	/*
	 * 方法上的注解
	 */
	@org.junit.Test
	public void test3() throws ClassNotFoundException{
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			printAnnoInfo(method);
		}
	}
	
	/*
	 * 方法参数上的注解
	 */
	@org.junit.Test
	public void test4() throws ClassNotFoundException{
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			Parameter[] params = method.getParameters();
			for (Parameter parameter : params) {
				printAnnoInfo(parameter);
			}
		}
	}
	
	
	private void printAnnoInfo(AnnotatedElement element){
		if (element.isAnnotationPresent(MyAnno.class)) {
			MyAnno myAnno = element.getAnnotation(MyAnno.class);
			System.out.println("==> "+element.getClass());
			System.out.println("[name="+myAnno.name()+", age="+myAnno.age()+", desc="+myAnno.desc()+"]");
			System.out.println();
		}
	}

}

测试结果:

==> class java.lang.Class
[name=yawn, age=18, desc=good boy]

==> class java.lang.reflect.Field
[name=tempreture, age=18, desc=]

==> class java.lang.reflect.Field
[name=iii, age=18, desc=]

==> class java.lang.reflect.Field
[name=ageee, age=18, desc=]

==> class java.lang.reflect.Method
[name=method , age=18, desc=desc]

==> class java.lang.reflect.Parameter
[name=id, age=18, desc=]

==> class java.lang.reflect.Parameter
[name=size, age=18, desc=]