Description
Invoke the named static method.
License
Open Source License
Parameter
Parameter | Description |
---|
cls | The class. |
methodName | The method name. |
throwException | Whether to throw an exception on failure. |
Exception
Parameter | Description |
---|
IllegalArgumentException | If the method could not be invoked. |
Return
The result of the method invocation.
Declaration
public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
final boolean throwException) throws IllegalArgumentException
Method Source Code
//package com.java2s;
/*/*from w w w . j a v a 2 s. c o m*/
* This file is part of ClassGraph.
*
* Author: Luke Hutchison
*
* Hosted at: https://github.com/lukehutch/fast-classpath-scanner
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Luke Hutchison
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.lang.reflect.Method;
public class Main {
/**
* Invoke the named static method. If an exception is thrown while trying to call the method, and throwException
* is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If
* passed a null class reference, returns null unless throwException is true, then throws
* IllegalArgumentException.
*
* @param cls
* The class.
* @param methodName
* The method name.
* @param throwException
* Whether to throw an exception on failure.
* @return The result of the method invocation.
* @throws IllegalArgumentException
* If the method could not be invoked.
*/
public static Object invokeStaticMethod(final Class<?> cls, final String methodName,
final boolean throwException) throws IllegalArgumentException {
if (cls != null) {
try {
final Method method = cls.getDeclaredMethod(methodName);
try {
method.setAccessible(true);
} catch (final Exception e) {
}
return method.invoke(null);
} catch (final Throwable e) {
if (throwException) {
throw new IllegalArgumentException("Could not invoke method \"" + methodName + "\"", e);
}
}
} else if (throwException) {
throw new IllegalArgumentException("Can't invoke static method on null class reference");
}
return null;
}
/**
* Invoke the named static method. If an exception is thrown while trying to call the method, and throwException
* is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If
* passed a null class reference, returns null unless throwException is true, then throws
* IllegalArgumentException.
*
* @param cls
* The class.
* @param methodName
* The method name.
* @param argType
* The type of the parameter.
* @param arg
* The argument value.
* @param throwException
* Whether to throw an exception on failure.
* @return The result of the method invocation.
* @throws IllegalArgumentException
* If the method could not be invoked.
*/
public static Object invokeStaticMethod(final Class<?> cls, final String methodName, final Class<?> argType,
final Object arg, final boolean throwException) throws IllegalArgumentException {
if (cls != null) {
try {
final Method method = cls.getDeclaredMethod(methodName, argType);
try {
method.setAccessible(true);
} catch (final Exception e) {
}
return method.invoke(null, arg);
} catch (final Throwable e) {
if (throwException) {
throw new IllegalArgumentException("Could not invoke method \"" + methodName + "\"", e);
}
}
} else if (throwException) {
throw new IllegalArgumentException("Can't invoke static method on null class reference");
}
return null;
}
}
Related
- invokeStaticMethod(Class> cls, String name)
- invokeStaticMethod(Class> klass, String methodName)
- invokeStaticMethod(Class> objectType, String name, Class> paramTypes[], Object args[])
- invokeStaticMethod(Class> pClass, String pMethod, Object pParam)
- invokeStaticMethod(final Class> cls, final String methodName)
- invokeStaticMethod(final Class clazz, final String methodName, final Class>[] parameterTypes, final Object[] arglist)
- invokeStaticMethod(final Method method, final Object... args)
- invokeStaticMethod(String className, String method, Class[] paramTypes, Object[] paramValues)
- invokeStaticMethod(String className, String methodName)