Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reflection accessing method with default modifier in the super class

Is it possible to invoke the no modifier method in a superclass through Java reflection?

like image 911
Vipin Avatar asked Jan 20 '10 09:01

Vipin


People also ask

What is the default access modifier for a method in Java?

As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java: Default – No keyword required. Private.

How do I set a default access modifier?

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package.

What is default access modifier for fields in the class?

internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , internal , or private . Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private .

Which of the following access modifier can be accessed with in a class?

For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class.


Video Answer


2 Answers

Method method = getClass().getSuperclass().getDeclaredMethod("doSomething");
method.invoke(this);

if you have a bigger hierarchy, you can use:

Class current = getClass();
Method method = null;
while (current != Object.class) {
     try {
          method = current.getDeclaredMethod("doSomething");
          break;
     } catch (NoSuchMethodException ex) {
          current = current.getSuperclass();
     }
}
// only needed if the two classes are in different packages
method.setAccessible(true); 
method.invoke(this);

(the above examples are for a method named doSomething with no arguments. If your method has arguments, you have to add their types as arguments to the getDeclaredMethod(...) method)

like image 70
Bozho Avatar answered Oct 22 '22 13:10

Bozho


After reading the original question -- I realize I assumed you were trying to call an overridden method. Which is what I was trying to do and how I came to find this thread. Calling a base class non-overridden method should work as others here have described. However, if you are trying to call an overridden method, my answer stands as below:

I don't think calling an overridden method is possible, per

http://blogs.oracle.com/sundararajan/entry/calling_overriden_superclass_method_on

Most notably:

Method.invoke

If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur.

like image 44
oldag Avatar answered Oct 22 '22 13:10

oldag