Java Reflection Annotation getAnnotations(Class ann, Object o, Method m, int param)

Here you can find the source of getAnnotations(Class ann, Object o, Method m, int param)

Description

Returns the parameter annotation requested.

License

Apache License

Parameter

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.

Return

The (possibly null) annotation.

Declaration

@SuppressWarnings("unchecked")
public static <T extends Annotation> T getAnnotations(Class<T> ann, Object o, Method m, int param) 

Method Source Code

//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;
    }
}

Related

  1. getAnnotations(Annotation[][] annotations)
  2. getAnnotations(Class cls)
  3. getAnnotations(Class annotationClass, Annotation[] annotations)
  4. getAnnotations(Class c, Class annotationClass)
  5. getAnnotations(Class annotation, Collection annotations)
  6. getAnnotations(Enum targetEnum, Class targetAnnotationClass)
  7. getAnnotations(Field field)
  8. getAnnotations(final Class c, final Class annClass)
  9. getAnnotations(final Class clazz, final Class annotation)