/*
* Dynamic creation of proxy classes
*/
public static <T> T getproxy (class<?> interfacetype,final Object target) throws exception{
if (!interfacetype.isinterface ()) {
throw new Exception ("The interface type is incorrect! ");
}
@SuppressWarnings ("Unchecked")
T targerinstance= (t) proxy.newproxyinstance (Interfacetype.getclassloader (), New Class[]{interfacetype}, new Invocationhandler () {
@Override
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
System.out.println ("Start");
Object Value=method.invoke (target, args);
System.out.println ("End");
return value;
}
});
return targerinstance;
}
Invoke instance:
Istudent Stu=getproxy (Class.forName ("Proxy.yl.IStudent"), Class.forName ("Proxy.yl.Student"). newinstance ());
Stu.setname ("xxxxx");
System.out.println (Stu.getname ());
Java Dynamic Creation Class