📄 callback.java
字号:
import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;/** * <pre> * Generic class to facilate use of callbacks * Also provides a static helper function to get an object of Method that can be passed to the constructor of this class * Example: * To get a Method object for the method public foo(String str) in class Test: * String[] paramTypes = {"java.lang.String"}; * Method method = Callback.getMethod("foo", this, paramTypes); * Callback cb = new Callback(method, this, "fooTest"); * * The above code snippet assumes that it is written inside class Test, hence the use of this. * The method must have public visibility. * </pre> */public class Callback { private Method method; private Object obj; private Object[] params; /** * Initializes member variables * @param method The method to be invoked * @param obj The object on which the method is to be invoked * @param params An array of objects to be passed to the method as parameters when it is invoked. * Can be null if no parameters are to be passed */ public Callback(Method method, Object obj, Object[] params) { this.method = method; this.obj = obj; this.params = params; } /** * Sets the params to be passed to the method when it is invoked * @param params The params to be passed to the method when it is invoked */ public void setParams(Object[] params) { this.params = params; } /** * Invokes the callback * @throws IllegalAccessException Thrown by invoke method in class Method * @throws InvocationTargetException Thrown by invoke method in class Method, if the underlying method throws an exception */ public void invoke() throws IllegalAccessException, InvocationTargetException { this.method.invoke(this.obj, this.params); } /** * Helper function to get a Method object which is needed to pass to the constructor of this class * @param methodName The name of the method that we want a Method object for * @param obj The object that owns the method * @return A Method object which can be passed to the constructor of this class * @param parameterTypes Array of strings of parameter type names. Can be null if there are no parameter types * @throws ClassNotFoundException This exception is thrown by Class.forName if the given class name does not exist * @throws NoSuchMethodException Thrown by Class.getMethod if a matching method is not found * @throws SecurityException Thrown by Class.getMethod if access to the information is denied */ public static Method getMethod(String methodName, Object obj, String[] parameterTypes) throws ClassNotFoundException, NoSuchMethodException, SecurityException { return obj.getClass().getMethod(methodName, Callback.getParameterTypes(parameterTypes)); } private static Class[] getParameterTypes(String[] parameterTypes) throws ClassNotFoundException { if ((parameterTypes == null) || (parameterTypes.length == 0)) { return null; } Class[] paramTypes = new Class[parameterTypes.length]; for(int i = 0; i < paramTypes.length; i++) { paramTypes[i] = Class.forName(parameterTypes[i]); } return paramTypes; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -