programmer10193
programmer10193

Reputation: 41

How to call a superclass method within an advice?

I'm working on a project which aims to introduce modifications in a code base without directly change the source code, these changes have already been implemented and I am rewriting the code with AspectJ

So far I managed to implement all changes using AspectJ. But I don't know how to implement it:

There is a way I can do this?

Thanks!

Upvotes: 4

Views: 228

Answers (1)

Azoraqua
Azoraqua

Reputation: 155

You could call it from the superclass using reflection.

import java.lang.reflect.Method;

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

class Employee extends Person {
  public void greet() {
    System.out.println("Employee's 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);
  }
}

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

Upvotes: 1

Related Questions