get Property Annotation - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Attribute

Description

get Property Annotation

Demo Code


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

public class Main {
    public static <T extends Annotation> T getPropertyAnnotation(
            PropertyDescriptor propertyDescriptor, Class<?> beanClass,
            Class<T> annotationClass) {
        T result = null;/*from w ww  .  j a  v a  2  s .  c  om*/
        try {
            Field field = beanClass.getDeclaredField(propertyDescriptor
                    .getName());
            result = field.getAnnotation(annotationClass);
        } catch (SecurityException e) {
        } catch (NoSuchFieldException e) {
        }
        Method readMethod = propertyDescriptor.getReadMethod();
        if ((result == null) && (readMethod != null)) {
            result = readMethod.getAnnotation(annotationClass);
        }
        Method writeMethod = propertyDescriptor.getWriteMethod();
        if ((result == null) && (writeMethod != null)) {
            result = writeMethod.getAnnotation(annotationClass);
        }
        return result;
    }
}

Related Tutorials