package com.proxyTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyTest {
public static void main(String[] args) throws Exception{
//获取代理字节码
Class clazzProxy=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
System.out.println(clazzProxy.getName());
System.out.println("-----------Proxy构造方法列表------------");
Constructor[] constructor=clazzProxy.getConstructors();
for(Constructor con:constructor){
//get名称
String conName=con.getName();
//接接参数
StringBuilder sb=new StringBuilder(conName);//单线程 快
sb.append('(');
//get 获得参数类型的字节码
Class[] paraClass=con.getParameterTypes();
for(Class c:paraClass){
//获取参数
sb.append(c.getName()).append(',');
}
if(paraClass!=null&¶Class.length!=0)
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
System.out.println("\n================Proxy方法列表=============================");
Method[] methods=clazzProxy.getMethods();
for(Method me:methods){
//get名称
String conName=me.getName();
//接接参数
StringBuilder sb=new StringBuilder(conName);//单线程 快
sb.append('(');
//get 获得参数类型的字节码
Class[] paraClass=me.getParameterTypes();
for(Class c:paraClass){
//获取参数
sb.append(c.getName()).append(',');
}
if(paraClass!=null&¶Class.length!=0)
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
System.out.println("\n================Proxy创建实例=============================");
//获取指定的构造方法
Constructor conIns=clazzProxy.getConstructor(InvocationHandler.class);
//创建一个connection类做参数
class myConn1 implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
}
//创建实例
Collection proxy1 =(Collection) conIns.newInstance(new myConn1());
System.out.println(proxy1);
// proxy1.size();
//创建实例 方式2===================================
@SuppressWarnings("unchecked")
Collection<String> proxy2 =(Collection<String>) conIns.newInstance(new InvocationHandler(){
//内部类
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return null;
}
} );
//创建实例 方式3===================================
Collection proxy3=(Collection) Proxy.newProxyInstance(
//类加载器
Collection.class.getClassLoader(),
// 参数数内型
new Class[]{Collection.class},
//参数值
new InvocationHandler(){
/**
* proxy 代理的对象
* method 代理对象的那个方法
* args 方法中传入的参数
*/
ArrayList target=new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Long startTime= System.currentTimeMillis();
//方法调用 参数 目标对象和参数
Object obj=method.invoke(target, args);
Long endTime= System.currentTimeMillis();
System.out.println(method.getName()+"runTime of 用时:"+(endTime=startTime));
//死循环
// return method.invoke(proxy,args);
//返回对象
return obj;
}}
);
proxy3.add("aaaa");
proxy3.add("bbbb");
proxy3.add("ccccc");
// System.out.println(proxy3.size());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java加强课程测试代码 反射、 代理 、泛型、beanUtils等
共55个文件
class:28个
java:16个
properties:5个
4星 · 超过85%的资源 需积分: 10 6 下载量 180 浏览量
2013-11-17
23:49:49
上传
评论
收藏 701KB RAR 举报
温馨提示
java加强课程测试代码 反射 代理 泛型 beanUtils等 有相关注释说明
资源推荐
资源详情
资源评论
收起资源包目录
zhangTest.rar (55个子文件)
zhangTest
.project 385B
config.properties 42B
src
com
proxyTest
ProxyTests.java 2KB
ProxyTest.java 4KB
Myadvice.java 598B
Advice.java 266B
emun
test
WeekdayTest.java 263B
WeekDay.java 1KB
zhang
test
GenericityTest.java 1KB
config.properties 42B
MethodTest.java 650B
LoadProperties.java 2KB
HashMian.java 792B
Beans.java 321B
HashCodeTest.java 1KB
beanUtilsTest
BeanUtilsTest.java 2KB
AopFramework
AopFrameworkTest.java 921B
config.properties 183B
ProxyFactoryBean.java 1KB
FactoryBean.java 1KB
lib
commons-beanutils-core-1.8.3.jar 202KB
commons-logging-1.1.1.jar 59KB
log4j-1.2.17.jar 478KB
.settings
org.eclipse.jdt.core.prefs 629B
.classpath 444B
bin
com
proxyTest
ProxyTest$1myConn1.class 803B
ProxyTests.class 2KB
ProxyTests$1.class 1KB
ProxyTest$2.class 2KB
ProxyTest.class 4KB
Myadvice.class 1KB
Advice.class 184B
ProxyTest$1.class 783B
emun
test
WeekDay$2.class 520B
WeekdayTest.class 1KB
WeekDay.class 873B
WeekDay$1.class 520B
WeekDay$TrafficLamp.class 1KB
WeekDay$TrafficLamp$2.class 681B
WeekDay$TrafficLamp$1.class 680B
WeekDay$TrafficLamp$3.class 678B
zhang
test
MethodTest.class 1KB
Beans.class 696B
HashCodeTest.class 2KB
HashMian.class 1001B
config.properties 42B
TestReflect.class 658B
GenericityTest.class 2KB
LoadProperties.class 3KB
beanUtilsTest
BeanUtilsTest.class 3KB
AopFramework
FactoryBean.class 2KB
config.properties 183B
AopFrameworkTest.class 1KB
ProxyFactoryBean.class 2KB
ProxyFactoryBean$1.class 1KB
共 55 条
- 1
资源评论
- wjc_linux2014-07-29很不错的资料
java的爪哇
- 粉丝: 190
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AI代码生成和补全.txt
- 工作资料.jpg
- IMG_20240420_162747.jpg
- Screenshot_2024-04-20-16-26-41-628_com.tencent.mm-edit.jpg
- QT界面+FFMPEG 做解码库实现播放流媒体以及本地视频+可叠加透明视频+源码(毕业设计&课程设计&项目开发)
- 基于opencv的停车场空余车位实时监测系统(源码)
- KUKA机器人基本指令的使用
- 基于Qt+c++实现的仿Linux锁屏界面+源码+文档(毕业设计&课程设计&项目开发)
- 基于Qt+c++实现的简易光线追踪渲染器适合小白入手学习+源码+文档(毕业设计&课程设计&项目开发)
- C语言操作系统实验代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功