提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
是否存在注释
//package com.nowjava; import java.lang.annotation.Annotation; /* 来自 N o w J a v a . c o m - 时 代 Java*/ public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; Class a = String.class; System.out.println(isAnnotationPresent(clazz, a)); } public static boolean isAnnotationPresent(Class<?> clazz, Class<? extends Annotation> a) { if (clazz == null) { return false; } if (clazz.isAnnotationPresent(a)) {/*来自 nowjava - 时代Java*/ return true; } if (isAnnotationPresentOnSuperClasses(clazz, a)) { return true; } if (isAnnotationPresentOnInterfaces(clazz, a)) { return true; } return false; } private static boolean isAnnotationPresentOnSuperClasses( Class<?> clazz, Class<? extends Annotation> a) { if (clazz == null) { return false; } Class<?> c = clazz.getSuperclass(); while (c != null && !c.equals(Object.class)) { if (c.isAnnotationPresent(a)) { return true; } if (isAnnotationPresentOnInterfaces(c, a)) { return true; } c = c.getSuperclass(); } if (isAnnotationPresentOnInterfaces(clazz.getSuperclass(), a)) { return true; } return false; } private static boolean isAnnotationPresentOnInterfaces(Class<?> clazz, Class<? extends Annotation> a) { if (clazz == nul