Java examples for java.lang.annotation:Annotation Attribute
returns the annotation of clazz if present otherwise returns null
//package com.java2s; import java.lang.annotation.Annotation; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String key = "java2s.com"; System.out.println(getAnnotationValueForClass(clazz, key)); }/*w w w .jav a2 s . co m*/ private static String getAnnotationValueForClass(Class clazz, String key) { String s = ""; Annotation[] annotations = clazz.getAnnotations(); for (Annotation a : annotations) { s = getValueFromAnntotationToString(key, a.toString()); if (s != null && !s.isEmpty()) { break; } } return s; } /** * returns the annotation of clazz if present otherwise returns null * * @param clazz * @param classAnnotation * @return */ public static Annotation getAnnotationValueForClass(Class clazz, Class classAnnotation) { Annotation a = clazz.getAnnotation(classAnnotation); return a; } /** * used for annotation Processing to return a value Annotation(key=value) * * @param value * this is the key used to find a value in the string * @param annotaionString * this is the .toString() method used for the annotation * @return */ private static String getValueFromAnntotationToString(String value, String annotaionString) { String string = ""; string = annotaionString.substring( annotaionString.indexOf("(") + 1, annotaionString.indexOf(")")); String[] strings = string.split(","); if (strings != null && strings.length > 0) { for (String s : strings) { if (s.contains(value)) { string = s.split("=")[1]; } } } return string; } }