Notice: Undefined index: HTTP_ACCEPT_ENCODING in C:\server\web\www.showyounger.com\s-system.php on line 2
利用java反射调用类的的私有方法 | 小样儿(ShowYounger)
小样儿,顶呱呱!!![http://www.showyounger.com]
- import java.lang.reflect.Method;
-
- public class PrivateUtil {
-
-
-
-
-
-
-
-
-
-
-
-
- public static Method getMethod(Class clazz, String methodName,
- final Class[] classes) throws Exception {
- Method method = null;
- try {
- method = clazz.getDeclaredMethod(methodName, classes);
- } catch (NoSuchMethodException e) {
- try {
- method = clazz.getMethod(methodName, classes);
- } catch (NoSuchMethodException ex) {
- if (clazz.getSuperclass() == null) {
- return method;
- } else {
- method = getMethod(clazz.getSuperclass(), methodName,
- classes);
- }
- }
- }
- return method;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes, final Object[] objects) {
- try {
- Method method = getMethod(obj.getClass(), methodName, classes);
- method.setAccessible(true);
- return method.invoke(obj, objects);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes) {
- return invoke(obj, methodName, classes, new Object[] {});
- }
-
- public static Object invoke(final Object obj, final String methodName) {
- return invoke(obj, methodName, new Class[] {}, new Object[] {});
- }
-
-
-
-
-
-
- public static void main(String[] args) {
- PrivateUtil.invoke(new B(), "printlnA", new Class[] { String.class },
- new Object[] { "test" });
- PrivateUtil.invoke(new B(), "printlnB");
- }
- }
-
- class A {
- private void printlnA(String s) {
- System.out.println(s);
- }
- }
-
- class B extends A {
- private void printlnB() {
- System.out.println("b");
- }
- }
-
- import java.lang.reflect.Method;
-
- public class PrivateUtil {
-
-
-
-
-
-
-
-
-
-
-
-
- public static Method getMethod(Class clazz, String methodName,
- final Class[] classes) throws Exception {
- Method method = null;
- try {
- method = clazz.getDeclaredMethod(methodName, classes);
- } catch (NoSuchMethodException e) {
- try {
- method = clazz.getMethod(methodName, classes);
- } catch (NoSuchMethodException ex) {
- if (clazz.getSuperclass() == null) {
- return method;
- } else {
- method = getMethod(clazz.getSuperclass(), methodName,
- classes);
- }
- }
- }
- return method;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes, final Object[] objects) {
- try {
- Method method = getMethod(obj.getClass(), methodName, classes);
- method.setAccessible(true);
- return method.invoke(obj, objects);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public static Object invoke(final Object obj, final String methodName,
- final Class[] classes) {
- return invoke(obj, methodName, classes, new Object[] {});
- }
-
- public static Object invoke(final Object obj, final String methodName) {
- return invoke(obj, methodName, new Class[] {}, new Object[] {});
- }
-
-
-
-
-
-
- public static void main(String[] args) {
- PrivateUtil.invoke(new B(), "printlnA", new Class[] { String.class },
- new Object[] { "test" });
- PrivateUtil.invoke(new B(), "printlnB");
- }
- }
-
- class A {
- private void printlnA(String s) {
- System.out.println(s);
- }
- }
-
- class B extends A {
- private void printlnB() {
- System.out.println("b");
- }
- }