Java Method Call invoke(Object target, Method method)

Here you can find the source of invoke(Object target, Method method)

Description

invoke

License

Apache License

Declaration

static Object invoke(Object target, Method method) throws MBeanException, ReflectionException 

Method Source Code


//package com.java2s;
/*/* ww w .ja v a2 s. c  o m*/
 *  Copyright 2010 Dain Sundstrom
 *
 *  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 javax.management.MBeanException;
import javax.management.ReflectionException;
import javax.management.RuntimeErrorException;
import javax.management.RuntimeOperationsException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static java.util.Objects.requireNonNull;

public class Main {
    static Object invoke(Object target, Method method) throws MBeanException, ReflectionException {
        requireNonNull(target, "target is null");
        requireNonNull(method, "method is null");

        try {
            return method.invoke(target);
        } catch (InvocationTargetException e) {
            // unwrap exception
            Throwable targetException = e.getTargetException();
            if (targetException instanceof RuntimeException) {
                throw new MBeanException((RuntimeException) targetException,
                        "RuntimeException occurred while invoking " + toSimpleName(method));
            } else if (targetException instanceof ReflectionException) {
                // allow ReflectionException to passthrough
                throw (ReflectionException) targetException;
            } else if (targetException instanceof MBeanException) {
                // allow MBeanException to passthrough
                throw (MBeanException) targetException;
            } else if (targetException instanceof Exception) {
                throw new MBeanException((Exception) targetException,
                        "Exception occurred while invoking " + toSimpleName(method));
            } else if (targetException instanceof Error) {
                throw new RuntimeErrorException((Error) targetException,
                        "Error occurred while invoking " + toSimpleName(method));
            } else {
                throw new RuntimeErrorException(new AssertionError(targetException),
                        "Unexpected throwable occurred while invoking " + toSimpleName(method));
            }
        } catch (RuntimeException e) {
            throw new RuntimeOperationsException(e,
                    "RuntimeException occurred while invoking " + toSimpleName(method));
        } catch (IllegalAccessException e) {
            throw new ReflectionException(e,
                    "IllegalAccessException occurred while invoking " + toSimpleName(method));
        } catch (Error err) {
            throw new RuntimeErrorException(err, "Error occurred while invoking " + toSimpleName(method));
        } catch (Exception e) {
            throw new ReflectionException(e, "Exception occurred while invoking " + toSimpleName(method));
        }
    }

    private static String toSimpleName(Method method) {
        return method.getName() + "()";
    }
}

Related

  1. invoke(Object owner, String methodName)
  2. invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
  3. invoke(Object source, String key)
  4. invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)
  5. invoke(Object target, Class clazz, String method)
  6. invoke(Object target, String methodName)
  7. invoke(Object target, String methodName, Object... args)
  8. invoke(Object target, String methodName, Object[] args)
  9. invoke(Object target, String name, Class[] params, Object[] args)