How to call superclass method in board?

I am working on a project whose purpose is to make changes to the code base without directly changing the source code, these changes are already implemented, and I rewrite the code using AspectJ

So far, I have managed to implement all the changes using AspectJ. But I do not know how to implement it:

  • There is a method called beforeCadastrarAndValidate ()that has been overridden by a subclass. At the end of this method, the following line was inserted:super.beforeCadastrarAndValidate ()
  • This line of code calls the method from the superclass. I can capture the execution of this method, but I have no idea what to call the superclass method. I’ve already searched, and I know that it’s impossible to call it “super” on the board. I also tried using Reflection, but I did not find a solution.

Is there a way I can do this?

Thank!

+4
source share
1 answer

You can call it from the superclass using reflection.

import java.lang.reflect.Method;

class Person {
  public void greet() {
    System.out.println("Person greet");
  }
}

class Employee extends Person {
  public void greet() {
    System.out.println("Employee greet");
  }
}

class Main {
  public static void main(String[] args) 
        throws Exception {
    // get the method object from Person class.
    Method g = Person.class.getMethod("greet",
                     new Class[0]);

    Employee e = new Employee();
    // When "g" is invoked on an "Employee" object, 
    // the "Employee.greet" method is called.
    g.invoke(e, null);
  }
}

Link: https://blogs.oracle.com/sundararajan/entry/calling_overriden_superclass_method_on

+1
source

All Articles