- 浏览: 68763 次
- 性别:
- 来自: 北京
文章分类
最新评论
import junit.framework.TestCase;
import junit.framework.Assert;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationHandler;
public class DynamicProxyTest extends TestCase {
private Object proxy;
public void setUp() throws Exception
{
proxy = makeAProxyWhichExtendsTestInterface();
}
private Object makeAProxyWhichExtendsTestInterface()
{
return Proxy.newProxyInstance(TestInterface.class.getClassLoader(), new Class[]{TestInterface.class},new TestInvocationHandler());
}
private class TestInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable
{
if ("operationA".equals(method.getName()))
{
System.out.println("operationA in proxy");
}
else if ("operationB".equals(method.getName()))
{
System.out.println("operationB in proxy");
}
else if ("equals".equals(method.getName()))
{
return true;
}
else if ("hashCode".equals(method.getName()))
{
return 100;
}
else if ("toString".equals(method.getName())){
return "proxy.toString()";
}
return null;
}
}
public void testProxyDoesExtendTheInterface
()
{
Assert.assertTrue(proxy instanceof TestInterface);
}
public void testProxyClassIsPublicAndFinalButNotAbstract
()
{
Assert.assertTrue(Modifier.isPublic(proxy.getClass().getModifiers()));
Assert.assertTrue(Modifier.isFinal(proxy.getClass().getModifiers()));
Assert.assertFalse(Modifier.isAbstract(proxy.getClass().getModifiers()));
}
public void testProxyInheritedFromJavaLangReflectProxy
()
{
Assert.assertSame(java.lang.reflect.Proxy.class,proxy.getClass().getSuperclass());
}
public void testUnqualifiedNameOfProxyStartsWith
$Proxy()
{
Assert.assertTrue(proxy.getClass().getSimpleName().startsWith("$Proxy"));
}
public void testToStringOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals("proxy.toString()",proxy.toString());
}
public void testEqualsOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals(proxy,makeAProxyWhichExtendsTestInterface());
Assert.assertEquals(proxy,null);
}
public void testhashCodeOfProxyIsHandledByInvocationHandler
()
{
Assert.assertEquals(100,proxy.hashCode());
Assert.assertEquals(100,makeAProxyWhichExtendsTestInterface().hashCode()); }
public interface TestInterface
{
void operationA();
void operationB();
}
}
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 547well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 358http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 629this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 699http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 632http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 795What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 735http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 672I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 605http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 674http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1640http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 798Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 859The XML declaration typically a ... -
UCM
2010-05-08 11:41 711Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 693refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 813As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 902copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 779copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 841It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1278copy from http://blog.cornetdes ...
相关推荐
package cn.sxt.dynamicproxy; import java.util.ArrayList; import java.util.List; import cn.sxt.service.UserService; import cn.sxt.service.UserServiceImpl; public class Client { public ...
SpringIOC,DI+dynamic proxy 实现盗版AOP的源代码
Castle框架注入依赖所需dll Version:2.1.0.0
这是动态代理的简单实例,方便学,入手.这个例子非常的简单哦!
Castle Dynamic Proxy代理使用
DynamicProxy.zip
java 反射与动态代理Reflection&DynamicProxy,源码展示
解决VS报错:未能加载文件或程序集“LinFu.DynamicProxy, Version=1.0.3.14911, Culture=neutral, PublicKeyToken=62a6874124340d6e”或它的某一个依赖项。系统找不到指定的文件。 把该文件放到bin文件夹就好了。
DynamicProxy,源码DynamicProxy,源码DynamicProxy,源码
主要写了静态代理、动态代理、还写了URL的使用,其实就是下载的原理了。 动态代理动态代理动态代理动态代理
动态代理一个简单实用的JDK动态代理演示
一个简单的动态调用wcf类,在公司的服务器上使用稳定。
解决了IBatisNET v1.x 应用.NET4.0后出现的'Ambiguous match found'问题,替换原有的dll即可。
WPF下MEF+Caliburn.Micro+MVVMValidation+Castle.Dynamicproxy AOP
动态代理框架源码 /// 说明: /// 在第一次需要用到代理时动态生成代理类,之后的使用均调用已经生成的代理类 /// 规则: /// 1.不能为接口创建代理类代理类 ... /// 2.... /// 3.... /// 1.... /// 2....
NULL 博文链接:https://xxp3369-126-com.iteye.com/blog/301133