find Method Annotation - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

find Method Annotation

Demo Code


//package com.java2s;
import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        String methodName = "java2s.com";
        System.out.println(java.util.Arrays.toString(findMethodAnnotation(
                clazz, methodName)));//from  w  w w . j av a2 s.  c om
    }

    public static Annotation[] findMethodAnnotation(Class<?> clazz,
            String methodName) {

        Annotation[] annotations = null;
        try {
            Class<?>[] params = null;
            Method method = clazz.getDeclaredMethod(methodName, params);
            if (method != null) {
                annotations = method.getAnnotations();
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return annotations;
    }
}

Related Tutorials