Here you can find the source of invokeUnchecked(Method method, Object target, Object... arguments)
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. |
public static <T> T invokeUnchecked(Method method, Object target, Object... arguments)
//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(); } } } }