Java Reflection Annotation getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)

Here you can find the source of getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)

Description

Search for an annotation on a configuration interface method.

License

Apache License

Parameter

Parameter Description
configInterface The configuration interface.
method The method.
annotationType The annotation type of interest.
searchMethodType Whether to search the method type.

Return

The annotation, or null.

Declaration

public static <T extends Annotation> T getAnnotation(Class<?> configInterface, Method method,
        Class<T> annotationType, boolean searchMethodType) 

Method Source Code


//package com.java2s;
/*//ww w.  j av  a2 s  .c  om
 * A High-Level Framework for Application Configuration
 *
 * Copyright 2007 Merlin Hughes / Learning Objects, Inc.
 *
 * 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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Regex matching an accessor (is*, get* or set*). The second group is the field name.
     */
    static final Pattern ACCESS_RE = Pattern.compile("^(is|get|set)(.+)$");
    /**
     * Regex matching a listener method (add*Listener or remove*Listener). The second group is the field name.
     */
    static final Pattern LISTENER_RE = Pattern.compile("^(add|remove)(.*)Listener$");

    /**
     * Search for an annotation on a configuration interface method. In addition to searching the method itself, the {@link #getGetMethod
     * plain get method} is also searched, as can the {@link
     * #getMethodType method type} be.
     *
     * @param configInterface The configuration interface.
     * @param method The method.
     * @param annotationType The annotation type of interest.
     * @param searchMethodType Whether to search the method type.
     *
     * @return The annotation, or null.
     */
    public static <T extends Annotation> T getAnnotation(Class<?> configInterface, Method method,
            Class<T> annotationType, boolean searchMethodType) {
        T annotation = method.getAnnotation(annotationType);
        if (annotation == null) {
            Method getMethod = getGetMethod(configInterface, method);
            if (getMethod != null) {
                annotation = getMethod.getAnnotation(annotationType);
            }
            if ((annotation == null) && searchMethodType) {
                String methodName = method.getName();
                if (ACCESS_RE.matcher(methodName).matches()) {
                    // Is the annotation present on the method type?
                    Class<?> methodType = getMethodType(method);
                    annotation = methodType.getAnnotation(annotationType);
                }
            }
        }
        return annotation;
    }

    /**
     * Get the plain get method associated with a configuration interface method. This is the unparameterized getter associated with the same field as the
     * specified configuration method. For example, isDisabled() might be returned for the method addDisabledListener().
     *
     * @param configInterface The configuration interface.
     * @param method The method.
     *
     * @return The plain get method, or else null.
     */
    public static Method getGetMethod(Class<?> configInterface, Method method) {
        Method getMethod = null;
        String methodName = method.getName();
        Matcher matcher = ACCESS_RE.matcher(methodName);
        boolean matches = matcher.matches();
        if (!matches) {
            matcher = LISTENER_RE.matcher(methodName);
            matches = matcher.matches();
        }

        if (matches) {
            String prop = matcher.group(2);
            try {
                getMethod = configInterface.getMethod("get" + prop);
            } catch (NoSuchMethodException ex) {
                Class<?> methodType = getMethodType(method);
                if (Boolean.TYPE.equals(methodType)) {
                    try {
                        getMethod = configInterface.getMethod("is" + prop);
                    } catch (NoSuchMethodException ex2) {
                        // ignore
                    }
                }
            }
        }
        return getMethod;
    }

    /**
     * Get the type of a method. If the method has a non-void return type, that type is returned. Otherwise if the method has at least one parameter, the type
     * of the first parameter is returned.
     *
     * @param method The method.
     *
     * @return The method type, or else {@link Void#TYPE}.
     */
    public static Class<?> getMethodType(Method method) {
        Class<?> methodType = method.getReturnType();
        if (Void.TYPE.equals(methodType)) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length > 0) {
                methodType = parameterTypes[0];
            }
        }
        return methodType;
    }
}

Related

  1. getAnnotation(Class clazz, String fieldName, Class annotationClass)
  2. getAnnotation(Class clazz, String name, Class[] types, Class annotationType)
  3. getAnnotation(Class cls, Class annotationClass)
  4. getAnnotation(Class cls, Class annotation)
  5. getAnnotation(Class componentClass, Class annotationClass)
  6. getAnnotation(Class klazz, Class annotationClass)
  7. getAnnotation(Class objectClass, Class annotationClass)
  8. getAnnotation(Class onClass, Class desiredAnnotationClass)
  9. getAnnotation(Class target, Class annoCls)