package myaop;
import java.util.*;
import java.lang.reflect.*;
interface person {
String getName();
int getID();
}
class student implements person{
private String name;
private int id;
student(){
System.out.println("student: student() called ");
name="nonmae";
id=0;
}
student(int mid, String name){
this.name=name;
this.id=mid;
}
public String getName(){
System.out.println("student: getName() called. Name= "+name);
return name;
}
public int getID() {
System.out.println("student: getID() called: id= "+id);
return id;
}
}
class actions {
public void BeforeActions(){
System.out.println("actions: BeforeActions() called ");
}
public void AfterActions(){
System.out.println("actions: AfterActions() called ");
}
}
class myAOP implements InvocationHandler{
public Object proxyee;
public void setTarget(Object target){
proxyee=target;
}
public Object invoke(Object myproxy, Method method, Object[] args){
System.out.println("myAOP:invoke() enters");
actions maction = new actions();
maction.BeforeActions();
Object result=null;
try {
result=method.invoke(proxyee,args);
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
maction.AfterActions();
return result;
}
}
public class AOPTrial {
public static void main(String[] args) {
// TODO Auto-generated method stub
person sta = new student(1,"jac2k");
myAOP handler = new myAOP();
handler.setTarget(sta);
Object mystu=Proxy.newProxyInstance(sta.getClass().getClassLoader(),
sta.getClass().getInterfaces(),handler);
/*
* student: getID() called: id= 1
* student: getName() called. Name= jac2k
*/
((person)sta).getID();
((person)sta).getName();
/*
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called
*/
//here can't covert to student
((person)mystu).getID();
((person)mystu).getName();
}
import java.util.*;
import java.lang.reflect.*;
interface person {
String getName();
int getID();
}
class student implements person{
private String name;
private int id;
student(){
System.out.println("student: student() called ");
name="nonmae";
id=0;
}
student(int mid, String name){
this.name=name;
this.id=mid;
}
public String getName(){
System.out.println("student: getName() called. Name= "+name);
return name;
}
public int getID() {
System.out.println("student: getID() called: id= "+id);
return id;
}
}
class actions {
public void BeforeActions(){
System.out.println("actions: BeforeActions() called ");
}
public void AfterActions(){
System.out.println("actions: AfterActions() called ");
}
}
class myAOP implements InvocationHandler{
public Object proxyee;
public void setTarget(Object target){
proxyee=target;
}
public Object invoke(Object myproxy, Method method, Object[] args){
System.out.println("myAOP:invoke() enters");
actions maction = new actions();
maction.BeforeActions();
Object result=null;
try {
result=method.invoke(proxyee,args);
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
maction.AfterActions();
return result;
}
}
public class AOPTrial {
public static void main(String[] args) {
// TODO Auto-generated method stub
person sta = new student(1,"jac2k");
myAOP handler = new myAOP();
handler.setTarget(sta);
Object mystu=Proxy.newProxyInstance(sta.getClass().getClassLoader(),
sta.getClass().getInterfaces(),handler);
/*
* student: getID() called: id= 1
* student: getName() called. Name= jac2k
*/
((person)sta).getID();
((person)sta).getName();
/*
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called
*/
//here can't covert to student
((person)mystu).getID();
((person)mystu).getName();
}
}
输出结果
student: getID() called: id= 1
student: getName() called. Name= jac2k
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called