反射调用方法时的两种情况,走get set和不走get set
@Test
public void test1() throws Exception{
//获取User类
Class class1=Class.forName("cn.jbit.bean.User");
//获取所有字段包括私有的
Field[] fileds=class1.getDeclaredFields();
for (Field field : fileds) {
System.out.println(field.getName());
}
//获取所有方法
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
//获取构造方法
Constructor[] constructors = class1.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println("构造方法:"+constructor);
}
//调用所有方法
Method method = class1.getMethod("show");
Object obj=class1.newInstance();
//method.invoke(obj);
//调用set方法赋值
Field field = class1.getDeclaredField("name");
//设置为Accessible可进入的,因为name是自由字段
field.setAccessible(true);
//给私有字段赋值
field.set(obj,"sp");
method.invoke(obj);
}
//上面的给私有字段赋值的方法是不走get,set方法的,那么有时在get,set方法里进行判断就不管用了
//这时需要用PropertyDescriptor
@Test
public void test2() throws Exception{
//获取User类
Class class1=Class.forName("cn.jbit.bean.User");
//获取所有字段包括私有的
Field[] fileds=class1.getDeclaredFields();
for (Field field : fileds) {
System.out.println(field.getName());
}
//获取所有方法
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
//调用所有方法
Method method = class1.getMethod("show");
Object obj=class1.newInstance();
//method.invoke(obj);
//调用set方法赋值
Field field = class1.getDeclaredField("name");
//属性描述
PropertyDescriptor pd=new PropertyDescriptor("name", class1);
//调用
Method method2 = pd.getWriteMethod();
method2.invoke(obj, "小红");
Method method3 = pd.getReadMethod();
String name = method3.invoke(obj).toString();
System.out.println(name);
}
反射调用方法时的两种情况,走get set和不走get set的更多相关文章
- 利用反射调用方法时,处理ref,out参数需要注意的问题(转)
转自:http://www.68idc.cn/help/buildlang/ask/20150318283817.html 项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现 ...
- java项目打jar包的两种情况
链接地址:http://jingyan.baidu.com/article/6b97984d8a6ddc1ca2b0bfa0.html 本文介绍一下java项目打jar包时的两种情况各怎么操作 方 ...
- WCF 客户端调用服务操作的两种方法
本节的主要内容:1.通过代理类的方式调用服务操作.2.通过通道的方式调用服务操作.3.代码下载 一.通过代理类的方式调用服务操作(两种方式添加代理类) 1.手动编写代理类,如下: 客户端契约: usi ...
- Hibernate多对多两种情况
Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...
- JAVA反射调用方法
1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotation.SetTable ...
- 导致“mysql has gone away”的两种情况
导致“mysql has gone away”的两种情况 By Cruise 1. wait_timeout参数 在开发代理server时, 我使用了jdbc连接数据库,并采用长连接的方式连接数据库 ...
- 外壳exe通过反射调用dll时
外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...
- .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)——转载
原文链接:https://blog.walterlv.com/post/dotnet-high-performance-reflection-suggestions.html ***** 大家都说反射 ...
- Day6------------磁盘用满的两种情况
1.文件包含元数据和写入的内容 元数据:存在硬盘中的inode ls -i /etc/passwd.bak 查看inode df -i 查看inode 2.磁盘用满的两种情况 1).内容太多 2).空 ...
随机推荐
- C# 窗体位置 Show和ShowDialog(转)
CenterParent 窗体在其父窗体中居中. CenterScreen 窗体在当前显示窗口中居中,其尺寸在窗体大小中指定. Manual 窗体的位置由 Location 属性确定. Windows ...
- ECLIPSE下SVN的创建分支/合并/切换使用
最近接项目要求,要在svn主干上创建分支,用分支来进行程序的bug修改,而主干上进行新功能的开发.分支上的bug修改完,发布后,可以合并到主干上.项目程序可以在主干和分支之间进行切换,来实现主干和分支 ...
- [LintCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [LintCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...
- Linux下安装PHP
从php官网下载好需要php的压缩包,我下的是5.5.37版, 解压:# tar -xvf php-5.5.37.tar.gz 移至解压出的文件夹:# cd php-5.5.37 检查安装环境:# . ...
- input相关问题总结
1. 禁止为所有被激活的输入框添加边框 *:focus {outline: none} 2. 禁止为被激活的输入框添加边框,说明:".abc"为输入框对象自定义添加的class类命 ...
- 使用裸设备配置Oracle ASM实例一例
1.查看当前磁盘 [root@std u01]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders Units = cylin ...
- centos重启不能自动联网的解决方法
在命令行下输入 下面的ifcfg-eth0,eth0为我的网卡名字.机器之间不同,请先查看自己网卡的名字 vi /etc/sysconfig/network-scripts/ifcfg-eth0 进行 ...
- Password Attacker
Passwords are widely used in our lives: for ATMs, online forum logins, mobile device unlock and door ...
- java notify和notifyAll的区别
首先从名字可以了解,notify是通知一个线程获取锁,notifyAll是通知所有相关的线程去竞争锁. notify不能保证获得锁的线程,真正需要锁,并且可能产生死锁. 举例1: 所有人(消费者线程) ...