Description
Get a non-public (i.e.
License
Apache License
Parameter
Parameter | Description |
---|
clz | The class. |
methodName | A static method name. |
params | Optional parameters. |
Exception
Parameter | Description |
---|
NoSuchMethodException | an exception |
SecurityException | an exception |
Return
A Method object.
Declaration
public static Method getMethod(Class<?> clz, String methodName, Object... params)
throws NoSuchMethodException, SecurityException
Method Source Code
//package com.java2s;
/* Copyright (c) 2015 Magnet Systems, Inc.
*
* 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.// w w w . j a va2 s .c om
*/
import java.lang.reflect.Method;
public class Main {
/**
* Get a non-public (i.e. declared) static method from a class.
* @param clz The class.
* @param methodName A static method name.
* @param params Optional parameters.
* @return A Method object.
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Method getMethod(Class<?> clz, String methodName, Object... params)
throws NoSuchMethodException, SecurityException {
if (params == null) {
return clz.getMethod(methodName);
} else {
int i = 0;
Class<?>[] paramClasses = new Class<?>[params.length];
for (Object param : params) {
paramClasses[i++] = param.getClass();
}
return clz.getDeclaredMethod(methodName, paramClasses);
}
}
/**
* Get a non-public (i.e. declared) method from an object.
* @param obj The object to be used.
* @param methodName A method name.
* @param params Optional parameters.
* @return A Method object.
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Method getMethod(Object obj, String methodName, Object... params)
throws NoSuchMethodException, SecurityException {
if (params == null) {
return obj.getClass().getMethod(methodName);
} else {
int i = 0;
Class<?>[] paramClasses = new Class<?>[params.length];
for (Object param : params) {
paramClasses[i++] = param.getClass();
}
return obj.getClass().getDeclaredMethod(methodName, paramClasses);
}
}
/**
* Get a non-public (i.e. declared) static method from a class.
* @param clz The class.
* @param methodName A method name.
* @param paramClasses Classes of the optional parameters.
* @return A Method object.
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Method getMethod(Class<?> clz, String methodName, Class<?>... paramClasses)
throws NoSuchMethodException, SecurityException {
if (paramClasses == null) {
return clz.getMethod(methodName);
} else {
return clz.getDeclaredMethod(methodName, paramClasses);
}
}
/**
* Get a non-public (i.e. declared) method from an object.
* @param obj The object to be used.
* @param methodName A method name.
* @param paramClasses Classes of the optional parameters.
* @return A Method object.
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Method getMethod(Object obj, String methodName, Class<?>... paramClasses)
throws NoSuchMethodException, SecurityException {
if (paramClasses == null) {
return obj.getClass().getMethod(methodName);
} else {
return obj.getClass().getDeclaredMethod(methodName, paramClasses);
}
}
}
Related
- getMethod(Class objectClass, String methodName, String argumentType)
- getMethod(Class type, String name, Object[] args)
- getMethod(Class extends Object> clz, String methodName, Class[] methodArgs)
- getMethod(Class extends Object> type, String name, Class>... parameterTypes)
- getMethod(Class> cls, String method, Object[] params)
- getMethod(Class> klass, String methodName, Object... params)
- getMethod(Class> objectType, String methodName, Class>... parameterTypes)
- getMethod(Class> type, String name, Object[] args)
- getMethod(final Class extends Object> clazz, final String methodName, final Class>[] parTypes, final Object[] parameters)