Like so : a class has a method called sayHello() . When a instance of the class call the sayHello(), a method in another class is called automatic before the sayHello() is called.
the sample code can be like this:
public class Robot{
public static void doSomethingBefore(){
System.out.println("Do something before sayHello");
}
}
public class Person {
public void sayHello(){
System.out.println("hello");
}
public static void main(String[] args){
Person p = new Person();
p.sayHello();
}
}
Output Result :
Do something before sayHello
hello
It seems it can be done by using the proxy pattern. But I want it can be more simple.
Use the annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MethodListener {
public String className();
public String methodName();
}
Then put the MethodListener annotation on the method sayHello() in the Person class,
public class Person {
@MethodListener(className="Robot",methodName="doSomethingBefore")
public void sayHello(){
System.out.println("hello");
}
public static void main(String[] args){
Person p = new Person();
p.sayHello();
}
}
When the method which has the MethodListener annotation be called, the method doSomethingBefore() is called automatic.
Should it be possible ? If it can be done, how to achieve it?