java 反射调用成员方法
public static GenericDialog showScreenshotDialog(JTextComponent area2, int width, int height){
Class clazz=area2.getClass();
Object obj=null;
Method m = null;
try {
m = clazz.getMethod("showScreenshotDialog", new Class[]{int.class,int.class});
m.setAccessible(true);
obj=m.invoke(area2, width,height);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(null==m){//抛异常 :NoSuchMethodException
GenericDialog genericDialog= screenshotDialog;
Point point= area2.getLocationOnScreen();
genericDialog.setBounds(point.x,point.y,width,height);
genericDialog.launchFrame();
return genericDialog;
}
return null;
}
public static GenericDialog getScreenshotDialog(JTextComponent area2) {
return (GenericDialog) getReflectGetMethod(area2, "getScreenshotDialog", null, null);
}
public static JDialog getMaxDialog(JTextComponent area2) {
return (JDialog) getReflectGetMethod(area2, "getMaxJDialog", null, null);
}
public static void closeMaxDialog(JDialog area2) {
getReflectGetMethod(area2, "closeDialog", null, null);
}
public static void setMaxDialog(JTextComponent area2, Object dialog, Class clazz) {
getReflectGetMethod(area2, "setMaxJDialog", dialog, clazz);
}
public static Integer getMaxStatus(JTextComponent area2) {
return (Integer) getReflectGetMethod(area2, "getMaxStatus", null, null);
}
public static void setMaxStatus(JTextComponent area2, int maxStatus) {
getReflectGetMethod(area2, "setMaxStatus", maxStatus, null);
}
public static Object getReflectGetMethod(Object area2, String methodName, Object param, Class clazz2) {
Class clazz=area2.getClass();
Object obj=null;
Method m;
try {
if (clazz2 == null && null != param) {
clazz2 = param.getClass();
}
if (null == clazz2) {
m = clazz.getMethod(methodName, new Class[]{});
} else {
m = clazz.getMethod(methodName, new Class[]{clazz2});
}
m.setAccessible(true);
if (null == param) {
obj = m.invoke(area2);
} else {
obj = m.invoke(area2, param);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
public static Map<String, ActionCallback> getActionCallbackMap(JTextComponent area2) {
return (Map<String, ActionCallback>) getReflectGetMethod(area2, "getActionCallbackMap", null, null);
}