invoke Annotated Method - Java java.lang.annotation

Java examples for java.lang.annotation:Method Annotation

Description

invoke Annotated Method

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 {
        Object instance = "java2s.com";
        Class annotatedClass = String.class;
        Object[] args = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        invokeAnnotatedMethod(instance, annotatedClass, args);
    }//  w w  w. jav a 2s.  c om

    static public <T extends Annotation> void invokeAnnotatedMethod(
            Object instance, Class<T> annotatedClass, Object[] args)
            throws Exception {
        Method[] method = instance.getClass().getMethods();
        for (Method selMethod : method) {
            Annotation annotation = selMethod.getAnnotation(annotatedClass);
            if (annotation != null) {
                selMethod.invoke(instance, args);
            }
        }
    }
}

Related Tutorials