Java.lang.Class类 isAnnotationPresent()方法有什么功能呢?

转自:

Java.lang.Class类 isAnnotationPresent()方法有什么功能呢?

下文讲述Class类中的isAnnotationPresent()方法的功能,如下所示:

isAnnotationPresent()方法的功能

java.lang.Class.isAnnotationPresent()方法的功能
用于检查对象是否存在指定注解
当存在指定注解时,则返回True,否则返回False

isAnnotationPresent()方法的语法

语法
   public boolean isAnnotationPresent(Class<T> annotationClass)
参数
   annotationClass:待获取注解的类型
返回值
   当符合注解类型,则返回true,否则返回false

例:
isAnnotationPresent()方法的示例分享

package com.java.other;
import org.junit.Test;
public class other {
	/**
	 * java265.com java.lang.Class 测试示例分享
	 * 
	 * @throws Exception
	 * 
	 */
	@Test
	public void test() throws Exception {
		Class c = Class.forName("java.util.HashMap");
		Class c2 = Class.forName("org.springframework.web.bind.annotation.Mapping");
		Class c3 = Class.forName("java.lang.annotation.Target");
		System.out.println(c2.isAnnotationPresent(c2));
		System.out.println(c2.isAnnotationPresent(c3));
	}
}
-------运行以上代码,将输出以下信息-----
false
true