Get all annotations of a class, methods, and parameters. - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

Get all annotations of a class, methods, and parameters.

Demo Code

/**//  w  ww . j a va 2 s .  co m
 * @file AnnotationUtil.java
 * 
 *       AnnotationUtil is a utility to get all annotations of a class, its
 *       methods,
 *       and the method parameters. Returned annotations include all annotations
 *       of
 *       the classes interfaces and super classes.
 *       Requested classes are cached, so requesting a classes annotations
 *       repeatedly
 *       is fast.
 * 
 *       Example usage:
 * 
 *       AnnotatedClass annotatedClass = AnnotationUtil.get(MyClass.class);
 *       List<AnnotatedMethod> methods = annotatedClass.getMethods();
 *       for (AnnotatedMethod method : methods) {
 *       System.out.println("Method: " + method.getName());
 *       List<Annotation> annotations = method.getAnnotations();
 *       for (Annotation annotation : annotations) {
 *       System.out.println("    Annotation: " + annotation.toString());
 *       }
 *       }
 * 
 * @brief
 *        AnnotationUtil is a utility to retrieve merged annotations from a
 *        class
 *        including all its superclasses and interfaces.
 * 
 * @license
 *          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.
 * 
 *          Copyright (c) 2013 Almende B.V.
 * 
 * @author Jos de Jong, <jos@almende.org>
 * @date 2013-01-21
 */
import java.lang.annotation.Annotation;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.WrongMethodTypeException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class clazz = String.class;
        System.out.println(get(clazz));
    }
    private static Map<String, AnnotatedClass> cache = new ConcurrentHashMap<String, AnnotatedClass>();
    private static Map<String, AnnotatedClass> cacheIncludingObject = new ConcurrentHashMap<String, AnnotatedClass>();
    /**
     * Get all annotations of a class, methods, and parameters.
     * Returned annotations include all annotations of the classes interfaces
     * and super classes (excluding java.lang.Object).
     * 
     * @param clazz
     *            the clazz
     * @return annotatedClazz
     */
    public static AnnotatedClass get(final Class<?> clazz) {
        final boolean includeObject = false;
        return get(clazz, includeObject);
    }
    /**
     * Get all annotations of a class, methods, and parameters.
     * Returned annotations include all annotations of the classes interfaces
     * and super classes.
     * 
     * @param clazz
     *            the clazz
     * @param includeObject
     *            If true, methods of java.lang.Object will be
     *            included in the superclasses too.
     * @return annotatedClazz
     */
    public static AnnotatedClass get(final Class<?> clazz,
            final boolean includeObject) {
        final Map<String, AnnotatedClass> myCache = includeObject ? cacheIncludingObject
                : cache;
        AnnotatedClass annotatedClazz = myCache.get(clazz.getName());
        if (annotatedClazz == null) {
            annotatedClazz = new AnnotatedClass(clazz, includeObject);
            myCache.put(clazz.getName(), annotatedClazz);
        }
        return annotatedClazz;
    }
}

Related Tutorials