反射调用方法时的两种情况,走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).空 ...
随机推荐
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- OSG-OSGEarth
OSG-OSGEarth 初次使用Cmake——以OsgEarth工程创建为例 转:http://www.cnblogs.com/Realh/archive/2012/02/08/2342507.ht ...
- mysql命令行参数(转)
MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式 -?, --help //显示帮助信息并退出 -I, --help //显示帮助信息并退出 ...
- windows 给ping加时间
@echo off set /p host=host Address: set logfile=Log_%host%.log echo Target Host = %host% >%logfil ...
- [转载]学习VC MFC开发必须了解的常用宏和指令————复习一下
1.#include指令 包含指定的文件 2.#define指令 预定义,通常用它来定义常量(包括无参量与带参量),以及用来实现那些“表面似和善.背后一长串”的宏,它本身并不在编译过程中进行,而 ...
- 网络地址转换NAT原理及其作用
1 概述 1.1 简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task F ...
- phpwind < v6 版本命令执行漏洞
phpwind/sort.php 会定期每天处理一次帖子的浏览量.回复量.精华版排序 代码直接使用savearray将数据库查询出来的内容写入php文件,savearray出来的参数,都使用" ...
- TCP/IP协议分层
TCP/IP协议从上而下,层层包装: (1)应用层:HTTP (2)传输层:TCP和UDP (3)网络层(网际互联层):IP (4)数据连接层(网络接入层):为IP模块发送和接收IP数据报. (5)硬 ...
- only for equality comparisons Hash Index Characteristics
http://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html Hash Index Characteristics Hash indexes ...
- java基础学习总结——基础语法2
一.语句