Java examples for java.lang.annotation:Method Annotation
Call to get all the methods of a class that have the given annotation.
/**//from www . j ava2 s. co m * Copyright (C) 2011 Tom Spencer <thegaffer@tpspencer.com> * * This file is part of TAL. * * TAL is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TAL 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TAL. If not, see <http://www.gnu.org/licenses/>. * * Note on dates: Year above is the year this code was built. This * project first created in 2008. Code was created between these two * years inclusive. */ //package com.java2s; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) throws Exception { Class annotationType = String.class; Class cls = String.class; System.out.println(getAnnotatedMethods(annotationType, cls)); } /** * Call to get all the methods of a class that have the given * annotation. * * @param annotationType The annotation we want on the methods * @param cls The class to check * @return The methods and their annotation of the given type in a map (will be empty if nothing found) */ public static <T extends Annotation> Map<Method, T> getAnnotatedMethods( Class<T> annotationType, Class<?> cls) { Map<Method, T> ret = new HashMap<Method, T>(); Method[] methods = cls.getMethods(); for (Method method : methods) { T annotation = method.getAnnotation(annotationType); if (annotation != null) ret.put(method, annotation); } return ret; } }