How to Call a Superclass Method Using Java Reflection

Invoke Super class methods using Reflection

Try this:

Method mthd = classAInstance.getClass().getSuperclass().getDeclaredMethod("XYZ");
mthd.invoke(classAInstance)

The difference is using getDeclaredMethod(), which gets methods of all visibilities (public, protected, package/default and private) instead of getMethod(), which only gets methods with public visibility.

Java Inheritance - calling superclass method

You can do:

super.alphaMethod1();

Note, that super is a reference to the parent class, but super() is its constructor.

Invoke Superclass method via reflection

As others have mentioned, it is not possible to call a superclass's method through reflection if it has been overridden. If at all possible you should look for another way of solving your issue before doing this, such as yole's suggestion, as this is both difficult to read and maintain (it could easily break unexpectedly if, for example, the setIcon() method implementation changes or the default_icon field changes name).

However, if you absolutely want to and must you can do something like this to "mimic" the method call based on the source for setIcon() here on line 1710.

public static void setButtonIcon(MyToggleButton button, Icon icon) {
if (button.getIcon() == icon)
return;

Icon old = button.getIcon();

// Use reflection to set the icon field
try {
Field f = AbstractButton.class.getDeclaredField("default_icon");
f.setAccessible(true);
f.set(button, icon);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
return;
}

button.firePropertyChange(AbstractButton.ICON_CHANGED_PROPERTY, old, icon);
button.revalidate();
button.repaint();
}

Reflection reference

Java Reflection : invoking inherited methods from child class

You need to use getMethod(String name, Class<?>... parameterTypes) instead of getDeclaredMethod

Can I use Reflection to call a method's super method?

After trying several things, I've concluded that this just isn't possible. (This doesn't surprise me, but I thought I'd give it a shot. I'm often surprised by what you can do through reflection, but you can't do this.)

Calling a method of subclass through reflection

You are invoking an instance method on a null instance - so it makes sense that you receive a NPE. Try this instead (passing an instance to the invoke method instead of null):

a.getClass().getMethod("Run").invoke(a);

Note: the first call worked because you can call a static method on a null instance without causing a NPE.

Reflection: how to call superclass constructor which is hidden?

After some research I'm able to answer the question by myself.

Short answer: I can't do this because if the superclass constructor is protected and hidden, the compiler is going to complain even if I found a way how to call the constructor via reflection.

Long answer: it turns out it's not so complicated to "unhide" this stuff. Following this tutorial I'm able to extend the class to my needs.

See? A lot of noise for nothing, this is the answer I was looking for.



Related Topics



Leave a reply



Submit