Here you can find the source of getAnnotations(Class
Parameter | Description |
---|---|
ann | The annotation to look for. |
o | The object. |
m | The method being inspected. |
param | The index of the parameter to process. |
T | The annotation type. |
@SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotations(Class<T> ann, Object o, Method m, int param)
//package com.java2s; /************************************************************************** * * Gluewine Utility Module//from w w w . j a va 2 s . c o m * * Copyright (C) 2013 FKS bvba http://www.fks.be/ * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. ***************************************************************************/ import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class Main { /** * Returns the parameter annotation requested. * * @param ann The annotation to look for. * @param o The object. * @param m The method being inspected. * @param param The index of the parameter to process. * @param <T> The annotation type. * @return The (possibly null) annotation. */ @SuppressWarnings("unchecked") public static <T extends Annotation> T getAnnotations(Class<T> ann, Object o, Method m, int param) { T res = null; Annotation[][] anns = m.getParameterAnnotations(); for (int i = 0; i < anns[param].length && res == null; i++) if (anns[param][i].annotationType().equals(ann)) res = (T) anns[param][i]; if (res == null) { Class<?> cl = o.getClass().getSuperclass(); while (cl != null && res == null) { try { Method m2 = cl.getMethod(m.getName(), m.getParameterTypes()); anns = m2.getParameterAnnotations(); for (int i = 0; i < anns[param].length && res == null; i++) if (anns[param][i].annotationType().equals(ann)) res = (T) anns[param][i]; } catch (Throwable e) { // Ignore it, as this is bound to happen. } Class<?>[] interf = cl.getInterfaces(); for (int i = 0; i < interf.length && res == null; i++) { try { Method m2 = interf[i].getMethod(m.getName(), m.getParameterTypes()); anns = m2.getParameterAnnotations(); for (int j = 0; j < anns[param].length && res == null; j++) if (anns[param][j].annotationType().equals(ann)) res = (T) anns[param][j]; } catch (Throwable e) { // Ignore it, as this is bound to happen. } } cl = cl.getSuperclass(); } } return res; } }