Java Reflection Annotation getAnnotationForProperty(Class cls, String name, Class anno)

Here you can find the source of getAnnotationForProperty(Class cls, String name, Class anno)

Description

get Annotation For Property

License

Open Source License

Declaration

public static <T extends Annotation> T getAnnotationForProperty(Class cls, String name, Class<T> anno) 

Method Source Code


//package com.java2s;
/*//from w ww.  j a v a2s.co  m
 * This file is copyright Bitronix Software.
 *
 * Bitronix Transaction Manager
 *
 * https://github.com/brettwooldridge/bitronix-hp/blob/master/btm/src/main/java/bitronix/tm/utils/PropertyUtils.java
 *
 * Copyright (c) 2010, Bitronix Software.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1301 USA
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static <T extends Annotation> T getAnnotationForProperty(Class cls, String name, Class<T> anno) {
        String postFix = name.toUpperCase();
        if (name.length() > 1) {
            postFix = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        Method method = null;
        try {
            method = cls.getMethod("get" + postFix);
        } catch (NoSuchMethodException e) {

        }
        if (method == null) {
            try {
                method = cls.getMethod("is" + postFix);
            } catch (NoSuchMethodException e) {

            }
        }
        if (method == null) {
            return null;
        }
        if (method.getModifiers() != Modifier.PUBLIC) {
            return null;
        }
        if (method.isAnnotationPresent(anno)) {
            return method.getDeclaredAnnotation(anno);
        }
        return null;
    }
}

Related

  1. getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class annotationElementType)
  2. getAnnotationField(Annotation annotation, String field)
  3. getAnnotationFields(Class claz, Class annotationType)
  4. getAnnotationFields(Class clazz, Class annotationClass)
  5. getAnnotationForMethodOrContainingClass(Method m, Class annotationType)
  6. getAnnotationFromAnnotation(Annotation annotation, Class annotationClass)
  7. getAnnotationFromEntityFields(Class entity, Class annotation)
  8. getAnnotationFromEntityOrInterface( Class annotationType, Class entity)
  9. getAnnotationFromField(Object object, Class annotationClass)