Java Method Call invokeUnchecked(Method method, Object target, Object... arguments)

Here you can find the source of invokeUnchecked(Method method, Object target, Object... arguments)

Description

Invokes the specified method without throwing any checked exceptions.

License

Apache License

Parameter

Parameter Description
method The method to invoke. Both the method and its class must have been declared public and non-abstract, otherwise they will be inaccessible.
target The object on which to invoke the method.
arguments The method arguments.
T The return type of the method. The compiler can usually infer the correct type.

Return

The result of invoking the method, or null if the method is void.

Declaration

public static <T> T invokeUnchecked(Method method, Object target, Object... arguments) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**/*from  w  w  w  .  ja  v  a2  s.c o  m*/
     * Invokes the specified method without throwing any checked exceptions.
     * This is only valid for methods that are not declared to throw any checked
     * exceptions.  Any unchecked exceptions thrown by the specified method will be
     * re-thrown (in their original form, not wrapped in an InvocationTargetException
     * as would be the case for a normal reflective invocation).
     * @param method The method to invoke.  Both the method and its class must have
     * been declared public and non-abstract, otherwise they will be inaccessible.
     * @param target The object on which to invoke the method.
     * @param arguments The method arguments.
     * @param <T> The return type of the method.  The compiler can usually infer the
     * correct type.
     * @return The result of invoking the method, or null if the method is void.
     */
    public static <T> T invokeUnchecked(Method method, Object target, Object... arguments) {
        try {
            @SuppressWarnings("unchecked")
            T result = (T) method.invoke(target, arguments);
            return result;
        } catch (IllegalAccessException ex) {
            // This cannot happen if the method is public.
            throw new IllegalArgumentException("Method " + method.getName() + " is not publicly accessible.", ex);
        } catch (InvocationTargetException ex) {
            // If the method is not declared to throw any checked exceptions,
            // the worst that can happen is a RuntimeException or an Error (we can,
            // and should, re-throw both).
            if (ex.getCause() instanceof Error) {
                throw (Error) ex.getCause();
            } else {
                throw (RuntimeException) ex.getCause();
            }
        }
    }

    /**
     * Invokes the specified constructor without throwing any checked exceptions.
     * This is only valid for constructors that are not declared to throw any checked
     * exceptions.  Any unchecked exceptions thrown by the specified constructor will be
     * re-thrown (in their original form, not wrapped in an InvocationTargetException
     * as would be the case for a normal reflective invocation).
     * @param constructor The constructor to invoke.  Both the constructor and its
     * class must have been declared public, and the class must not be abstract,
     * otherwise they will be inaccessible.
     * @param arguments The method arguments.
     * @param <T> The return type of the method.  The compiler can usually infer the
     * correct type.
     * @return The object created by invoking the specified constructor with the specified
     * arguments.
     */
    public static <T> T invokeUnchecked(Constructor<T> constructor, Object... arguments) {
        try {
            return constructor.newInstance(arguments);
        } catch (IllegalAccessException ex) {
            // This cannot happen if the constructor is public.
            throw new IllegalArgumentException("Constructor is not publicly accessible.", ex);
        } catch (InstantiationException ex) {
            // This can only happen if the constructor belongs to an
            // abstract class.
            throw new IllegalArgumentException("Constructor is part of an abstract class.", ex);
        } catch (InvocationTargetException ex) {
            // If the method is not declared to throw any checked exceptions,
            // the worst that can happen is a RuntimeException or an Error (we can,
            // and should, re-throw both).
            if (ex.getCause() instanceof Error) {
                throw (Error) ex.getCause();
            } else {
                throw (RuntimeException) ex.getCause();
            }
        }
    }
}

Related

  1. invokeSimple(Object target, String name)
  2. invokeTarget(Method method, Object obj, Object... args)
  3. invokeTargetCheckPoint(String resuming_checkpoint_path, Object jobObject, final java.util.Map globalMap)
  4. invokeTargetMethods(Object obj, T data, List methods)
  5. invokeToStringMethod(Object value, Class type)
  6. invokeUnwrapException(final Object target, final Method method, @Nullable final Object[] args)
  7. invokeValue(AnnotatedElement element, Class annotationClass)
  8. invokeVirtual(String methodName, Object onObj, Class[] declaredArgTypes, Object... withArgs)
  9. invokeVirtual(T o, Method method, Object... pa)