Monday, September 17, 2012
Tuesday, September 4, 2012
Invoking parent private method by reflection
boolean superField = true;
private void superMethod(boolean superField) {System.out.println("superMethod() called - param:" + superField);
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public static void main(String args[]) {
SubClass subClassInstance = new SubClass();
try {
Method declaredMethod = SuperClass.class.getDeclaredMethod("superMethod", boolean.class);
declaredMethod.setAccessible(true);
declaredMethod.invoke((SuperClass)subClassInstance, new Object[] {false}); //I remember that there was a situation this explicit cast is required
} catch (NoSuchMethodException e) {} catch (SecurityException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}
Subscribe to:
Posts (Atom)