Is there any way to make a sort of \"supermethod\" that is called every time a method is called, even for non-defined methods? Sort of like this:
public void
Sure you can do this, not with standard java but with AspectJ
Here is a simple example:
Aspect with an after-advice
package net.fsa.aspectj.test;
public aspect SuperMethdAspect {
pointcut afterPointCut() : execution(public * com.my.pack.age.MyClass.*(..));
after() : afterPointCut() {
System.out.println("Super");
}
}
You target class
package com.my.pack.age;
public class MyClass {
public void onStart() {
System.out.println("Start");
}
public void onEnd() {
System.out.println("End");
}
}
And finally some test app
package net.fsa.aspectj.test;
import com.my.pack.age.MyClass;
public class MyApp {
public static void main(String... args) {
MyClass myClass = new MyClass();
myClass.onStart();
myClass.onEnd();
}
}
Output
Start
Super
End
Super