get Annotation Value For Field of Class - Java java.lang.annotation

Java examples for java.lang.annotation:Field Annotation

Description

get Annotation Value For Field of Class

Demo Code


//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Main {

    private static String getAnnotationValueForFieldofClass(Class clazz,
            String field, String key) {
        String s = "";
        Field[] fields = clazz.getDeclaredFields();

        for (Field f : fields) {
            String theField = f.toString();
            if (field.equalsIgnoreCase(theField.substring(theField
                    .lastIndexOf(".") + 1))) {
                Annotation[] annotations = f.getDeclaredAnnotations();
                for (Annotation a : annotations) {
                    s = getValueFromAnntotationToString(key, a.toString());
                    if (s != null && !s.isEmpty()) {
                        break;
                    }//from   w w  w  . ja  va2 s.  co  m
                }
            }
        }
        return s;
    }

    /**
     * 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;
    }
}

Related Tutorials