Java Method Call invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)

Here you can find the source of invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)

Description

invoke Annotated Declared Method

License

CDDL license

Declaration

private static void invokeAnnotatedDeclaredMethod(Object obj, Class<? extends Annotation> annotationType)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code

    //package com.java2s;
    /*//from w  ww.j  a va2s.c  o  m
     *         COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Notice
     *
     * The contents of this file are subject to the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL)
     * Version 1.0 (the "License"); you may not use this file except in
     * compliance with the License. A copy of the License is available at
     * http://www.opensource.org/licenses/cddl1.txt
     *
     * The Original Code is Drombler.org. The Initial Developer of the
     * Original Code is Florian Brunner (GitHub user: puce77).
     * Copyright 2015 Drombler.org. All Rights Reserved.
     *
     * Contributor(s): .
     */

    import java.lang.annotation.Annotation;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.Optional;

    public class Main {
        private static void invokeAnnotatedDeclaredMethod(Object obj, Class<? extends Annotation> annotationType)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    Optional<Method> annotatedMethod = Arrays.stream(obj.getClass().getDeclaredMethods())
            .filter(method -> method.getParameterCount() == 0)
            .filter(method -> method.isAnnotationPresent(annotationType))
            .findFirst();

    if (annotatedMethod.isPresent()) {
        annotatedMethod.get().invoke(obj);
    }
}
    }

Related

  1. invoke(String methodName, Object target, Class targetClass, Object[] args)
  2. invoke(String name, Object object, Object... args)
  3. invoke_ex(Object obj, String method, Class[] params, Object[] args)
  4. invokeAccessableMethodWithArguments(Object instance, String method, Object... arguments)
  5. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)
  6. invokeAnUnwrapException(final Method method, final Object[] args, final Object target)
  7. invokeCallback(String callbackName, String callbackParam)
  8. invokeCmd(String className, String[] args)
  9. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)