Here you can find the source of getAnnotationForProperty(Class cls, String name, Class
public static <T extends Annotation> T getAnnotationForProperty(Class cls, String name, Class<T> anno)
//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; } }