public interface DamInterface {
public void sayHello();
public int sayHelloa();
}
public class DamImpl implements DamInterface {
@Override
public void sayHello() {
System.out.println("Hello");
}
@Override
public int sayHelloa() {
System.out.println("Helloa");
return 0;
}
}
public class DamInvocationHandler implements InvocationHandler {
private Object target;
public DamInvocationHandler(Object target) {
super();
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
result = method.invoke(proxy, args);
return result;
}
public Object getProxy() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass()
.getInterfaces(), this);
}
}
public class ProxyMain {
public static void main(String[] args) {
DamInterface dam = new DamImpl();
DamInvocationHandler handler = new DamInvocationHandler(dam);
// 创建动态代理对象
DamInterface proxy = (DamInterface) handler.getProxy();
proxy.sayHello();
proxy.sayHelloa();
}
}
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy0.sayHello(Unknown Source)
at com.jd.o2o.proxy.ProxyMain.main(ProxyMain.java:12)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.jd.o2o.proxy.DamInvocationHandler.invoke(DamInvocationHandler.java:19)
... 2 more
public static void main(String[] args) {
DamInterface dam =
new DamImpl();
byte[] generateProxyClass =
ProxyGenerator.generateProxyClass("Dam$1", dam.getClass().getInterfaces());
try {
File file =
new File("d:/Dam$1.class");
FileOutputStream fos =
new FileOutputStream(file);
fos.write(generateProxyClass);
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public final class Dam$1
extends Proxy
implements DamInterface
{
private static Method m4;
private static Method m1;
private static Method m3;
private static Method m0;
private static Method m2;
public final void sayHello()
throws
{
try
{
this.h.invoke(
this, m4,
null);
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final boolean equals(Object paramObject)
throws
{
try
{
return ((Boolean)
this.h.invoke(
this, m1,
new Object[] { paramObject })).booleanValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final int sayHelloa()
throws
{
try
{
return ((Integer)
this.h.invoke(
this, m3,
null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final int hashCode()
throws
{
try
{
return ((Integer)
this.h.invoke(
this, m0,
null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final String toString()
throws
{
try
{
return ((String)
this.h.invoke(
this, m2,
null));
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
static
{
try
{
m4 = Class.forName("com.jd.o2o.proxy.DamInterface").getMethod("sayHello",
new Class[0]);
m1 = Class.forName("java.lang.Object").getMethod("equals",
new Class[] { Class.forName("java.lang.Object") });
m3 = Class.forName("com.jd.o2o.proxy.DamInterface").getMethod("sayHelloa",
new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode",
new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString",
new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result =
null;
result = method.invoke(target, args);
return result;
}