Java反射初探123456789
牛逼的框架都反射,不要问我为什么,因为我也不知道
可能是因为生成了class文件没法实例化,所以只能反射创建对象,但是在spring中,ioc就是反射实现的控制反转
看Spring4.x企业级开发实战 自己写的 第一个是Person类
package com.smart.reflect; /**
* @program: chapter3
* @description: Entity
* @author: Sunshine
* @create: 2020-12-25 15:34
*/
public class Person {
private String name;
private String nipples;
private int hight; public Person() {
} public Person(String name, String nipples, int hight) {
this.name = name;
this.nipples = nipples;
this.hight = hight;
} public void introduce () {
System.out.println("name:"+ name+"; nipples:" + nipples +";hight:"+ hight );
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getNipples() {
return nipples;
} public void setNipples(String nipples) {
this.nipples = nipples;
} public int getHight() {
return hight;
} public void setHight(int hight) {
this.hight = hight;
}
}
package com.smart.reflect; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; /**
* @program: chapter3
* @description: 测试反射
* @author: Sunshine
* @create: 2020-12-25 15:39
*/
public class ReflectTest {
public static Person initByDefaultConst() throws Throwable {
//1 类加载器获取对象
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class clazz = loader.loadClass("com.smart.reflect.Person");
//2 获取类的默认构造器对象 ,通过它实例化Person
Constructor cons = clazz.getDeclaredConstructor((Class<?>[]) null);
Person person = (Person) cons.newInstance(); //3 通过反射方法设置(属性) Method setName = clazz.getMethod("setName", String.class);
// 这里才是真正的射入属性 当然得有前面的铺垫,要不直接射不太行,你说呢?
setName.invoke(person, "兔宝宝"); Method setNipples = clazz.getMethod("setNipples", String.class);
setNipples.invoke(person, "pink"); Method setHignt = clazz.getMethod("setHight", int.class);
setHignt.invoke(person, 170); return person;
} public static void main(String[] args) {
try {
Person p = initByDefaultConst();
p.introduce();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
红色的方法对应的jdk源码 /**
* Returns a {@code Constructor} object that reflects the specified
* constructor of the class or interface represented by this
* {@code Class} object. The {@code parameterTypes} parameter is
* an array of {@code Class} objects that identify the constructor's
* formal parameter types, in declared order.
*
* If this {@code Class} object represents an inner class
* declared in a non-static context, the formal parameter types
* include the explicit enclosing instance as the first parameter.
*
* @param parameterTypes the parameter array
* @return The {@code Constructor} object for the constructor with the
* specified parameter list
* @throws NoSuchMethodException if a matching method is not found.
* @throws SecurityException
* If a security manager, <i>s</i>, is present and any of the
* following conditions is met:
*
* <ul>
*
* <li> the caller's class loader is not the same as the
* class loader of this class and invocation of
* {@link SecurityManager#checkPermission
* s.checkPermission} method with
* {@code RuntimePermission("accessDeclaredMembers")}
* denies access to the declared constructor
*
* <li> the caller's class loader is not the same as or an
* ancestor of the class loader for the current class and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package
* of this class
*
* </ul>
*
* @since JDK1.1
*/
@CallerSensitive
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return getConstructor0(parameterTypes, Member.DECLARED);
}
其实就是获取当前的这个Person类的默认的构造函数,了解的差不多就行了,别逮到一个类就往死点jdk 深入是深入了 出不来不是白给么?
下面这个图片就是借花献佛以下,下面也说了 newInsrtance()就是实例化对象用的

类装载器 CLassLoader
懒得打字 直接看图
类装载找类字节,虚机建类象组件
类加载装虚机中,需要通过下几步 查找导入类文件,校验准备和解析
校验类文件数据,是否准确不掺假
准备阶段很关键,要给静态配空间
解析阶段可以选,符号引用变直引 初始化阶段不拉胯,静态系列初始化
JVM
ClassLoader ExtClassLoader AppClassLoader
  
ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.out.println("current Loader" + loader);
System.out.println("baba Loader" + loader.getParent());
System.out.println("yeye Loader" + loader.getParent().getParent());
"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:E:\idea\IntelliJ IDEA 2020.2.4\lib\idea_rt.jar=57015:E:\idea\IntelliJ IDEA 2020.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\Administrator\Desktop\spring4.x\wangpan\chapter3\target\classes;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-web\1.3.3.RELEASE\spring-boot-starter-web-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter\1.3.3.RELEASE\spring-boot-starter-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot\1.3.3.RELEASE\spring-boot-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-autoconfigure\1.3.3.RELEASE\spring-boot-autoconfigure-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-logging\1.3.3.RELEASE\spring-boot-starter-logging-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\ch\qos\logback\logback-classic\1.1.5\logback-classic-1.1.5.jar;D:\ToolsForDev\repository\ch\qos\logback\logback-core\1.1.5\logback-core-1.1.5.jar;D:\ToolsForDev\repository\org\slf4j\slf4j-api\1.7.16\slf4j-api-1.7.16.jar;D:\ToolsForDev\repository\org\slf4j\jcl-over-slf4j\1.7.16\jcl-over-slf4j-1.7.16.jar;D:\ToolsForDev\repository\org\slf4j\jul-to-slf4j\1.7.16\jul-to-slf4j-1.7.16.jar;D:\ToolsForDev\repository\org\slf4j\log4j-over-slf4j\1.7.16\log4j-over-slf4j-1.7.16.jar;D:\ToolsForDev\repository\org\yaml\snakeyaml\1.16\snakeyaml-1.16.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-tomcat\1.3.3.RELEASE\spring-boot-starter-tomcat-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\apache\tomcat\embed\tomcat-embed-logging-juli\8.0.32\tomcat-embed-logging-juli-8.0.32.jar;D:\ToolsForDev\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.0.32\tomcat-embed-websocket-8.0.32.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-validation\1.3.3.RELEASE\spring-boot-starter-validation-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\hibernate\hibernate-validator\5.2.4.Final\hibernate-validator-5.2.4.Final.jar;D:\ToolsForDev\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\ToolsForDev\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;D:\ToolsForDev\repository\com\fasterxml\classmate\1.1.0\classmate-1.1.0.jar;D:\ToolsForDev\repository\com\fasterxml\jackson\core\jackson-databind\2.6.5\jackson-databind-2.6.5.jar;D:\ToolsForDev\repository\com\fasterxml\jackson\core\jackson-annotations\2.6.5\jackson-annotations-2.6.5.jar;D:\ToolsForDev\repository\com\fasterxml\jackson\core\jackson-core\2.6.5\jackson-core-2.6.5.jar;D:\ToolsForDev\repository\org\springframework\spring-web\4.2.5.RELEASE\spring-web-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\spring-aop\4.2.5.RELEASE\spring-aop-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\ToolsForDev\repository\org\springframework\spring-beans\4.2.5.RELEASE\spring-beans-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\spring-context\4.2.5.RELEASE\spring-context-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\spring-webmvc\4.2.5.RELEASE\spring-webmvc-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\spring-expression\4.2.5.RELEASE\spring-expression-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-jdbc\1.3.3.RELEASE\spring-boot-starter-jdbc-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\apache\tomcat\tomcat-jdbc\8.0.32\tomcat-jdbc-8.0.32.jar;D:\ToolsForDev\repository\org\apache\tomcat\tomcat-juli\8.0.32\tomcat-juli-8.0.32.jar;D:\ToolsForDev\repository\org\springframework\spring-jdbc\4.2.5.RELEASE\spring-jdbc-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\spring-tx\4.2.5.RELEASE\spring-tx-4.2.5.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-starter-actuator\1.3.3.RELEASE\spring-boot-starter-actuator-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\org\springframework\boot\spring-boot-actuator\1.3.3.RELEASE\spring-boot-actuator-1.3.3.RELEASE.jar;D:\ToolsForDev\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar;D:\ToolsForDev\repository\org\apache\tomcat\embed\tomcat-embed-core\8.0.32\tomcat-embed-core-8.0.32.jar;D:\ToolsForDev\repository\org\apache\tomcat\embed\tomcat-embed-el\8.0.32\tomcat-embed-el-8.0.32.jar;D:\ToolsForDev\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;D:\ToolsForDev\repository\org\springframework\spring-core\4.2.5.RELEASE\spring-core-4.2.5.RELEASE.jar" com.smart.reflect.ReflectTest
name:兔宝宝; nipples:pink;hight:170
当前加载器
current Loadersun.misc.Launcher$AppClassLoader@18b4aac2
当前加载器的爹
baba Loadersun.misc.Launcher$ExtClassLoader@33c7353a
当前加载器的爷爷
yeye Loadernull
Process finished with exit code 0



这个费点劲   还得启动 
用这个省事
package com.smart.utils; import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain; /**
* tools to find which jar does the class come from
* @author : chenxh
* @date: 16-04-0
*/
public class ClassLocationUtils { /**
* find the location of the class come from
* @param cls
* @return
*/
public static String where(final Class cls) {
if (cls == null)throw new IllegalArgumentException("null input: cls");
URL result = null;
final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
final ProtectionDomain pd = cls.getProtectionDomain();
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
if (cs != null) result = cs.getLocation();
if (result != null) {
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar") ||
result.toExternalForm().endsWith(".zip"))
result = new URL("jar:".concat(result.toExternalForm())
.concat("!/").concat(clsAsResource));
else if (new File(result.getFile()).isDirectory())
result = new URL(result, clsAsResource);
}
catch (MalformedURLException ignore) {}
}
}
}
if (result == null) {
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ?
clsLoader.getResource(clsAsResource) :
ClassLoader.getSystemResource(clsAsResource);
}
return result.toString();
} }
自己试了一下 嘿 您猜怎么着 还真好使
file:/C:/Users/Administrator/Desktop/spring4.x/wangpan/chapter3/target/classes/com/smart/reflect/Person.class






Java反射初探123456789的更多相关文章
- 【java】java反射初探 ——“当类也照起镜子”
		反射的作用 开门见山地说说反射的作用 1.为我们提供了全面的分析类信息的能力 2.动态加载类 我理解的“反射”的意义 (仅个人理解) 我理解的java反射机制就是: 提供一套完善而强大的API ... 
- 【java】java反射初探 ——“当类也学会照镜子”
		反射的作用 开门见山地说说反射的作用 1.为我们提供了全面的分析类信息的能力 2.动态加载类 我理解的“反射”的意义 (仅个人理解哈) 我理解的java反射机制就是: 提供一套完善而强 ... 
- Java反射初探 ——“当类也学会照镜子”
		反射的作用 开门见山地说说反射的作用 1.为我们提供了全面的分析类信息的能力 2.动态加载类 我理解的“反射”的意义 (仅个人理解哈) 我理解的java反射机制就是: 提供一套完善而强大的 ... 
- java反射初探
		java反射 反射是java的重要特性之一,java.lang.reflect 是jdk支持反射的重要包,我下面可能会对构造器Constructor,属性Filed,方法Method会用到.反射其实很 ... 
- 初探Java反射机制
		反射库提供了一个非常丰富且精心设计的工具集,以便编写能够动态操纵java代码的程序库.这项功能被大量地应用于JavaBeans中.反射机制提供了在运行状态中获得和调用修改任何一个类的属性和方法的能力. ... 
- Java反射机制详解(3) -java的反射和代理实现IOC模式 模拟spring
		IOC(Inverse of Control) 可翻译为“控制反转”,但大多数人都习惯将它称为“依赖注入”.在Spring中,通过IOC可以将实现类.参数信息等配置在其对应的配置文件中,那么当 需要更 ... 
- 浅谈java反射机制
		目录 什么是反射 初探 初始化 类 构造函数 属性 方法 总结 思考 什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意 ... 
- 第28章 java反射机制
		java反射机制 1.类加载机制 1.1.jvm和类 运行Java程序:java 带有main方法的类名 之后java会启动jvm,并加载字节码(字节码就是一个类在内存空间的状态) 当调用java命令 ... 
- Java反射机制
		Java反射机制 一:什么事反射机制 简单地说,就是程序运行时能够通过反射的到类的所有信息,只需要获得类名,方法名,属性名. 二:为什么要用反射: 静态编译:在编译时确定类型,绑定对象,即通过 ... 
- java反射(基础了解)
		package cn.itcast_01; /** *Person类 */ public class Person { /** 姓名 */ private String name; ... 
随机推荐
- mysql 查询差集方法
			第一种是通过not in的方式去处理: select id from table_a where id not in (select id from table_b); 第二种则是通过左连接(left ... 
- MySQL where 操作符
			MySql WHERE 操作符号 前言 在 WHERE 子句中,你可以使用任何条件对记录进行过滤. 准备工作 准备 users 表,并插入数据 # 创建用户表 users create table u ... 
- DeepFaceLab换脸使用
			将视频转换成图片,从图片中提取人脸,从人脸中学习特征.然后应用模型,先对图片进行换脸,然后把图片合成视频. 1.src视频分解图像 2.dst视频分解图像 3.src提取面部 4.dst提取面部 5. ... 
- R语言求取大量遥感影像的平均值、标准差:raster库
			本文介绍基于R语言中的raster包,批量读取多张栅格图像,对多个栅格图像计算平均值.标准差,并将所得新的栅格结果图像保存的方法. 在文章基于R语言的raster包读取遥感影像中,我们介绍了基 ... 
- Shell脚本实现Linux回收站
			前言 到目前为止,非图形化的Linux还没有回收站的命令. 那么,我们如果不小心将某一个文件删掉了,那就只能数据恢复了.如果这也不行,那就等着哭吧. 最新代码在我的github:https://git ... 
- mysql 如何分配root账号创建数据库的权限
			1.mysql 如何分配root账号创建数据库的权限 在 MySQL 中,root 用户通常具有所有的权限,包括创建数据库的权限.但是,如果我们想要为另一个用户分配创建数据库的权限,或者想要限制 ro ... 
- Python 压缩PDF减小文件大小
			压缩 PDF 文件能有效减小文件大小并提高文件传输的效率,同时还能节省计算机存储空间.除了使用一些专业工具对PDF文件进行压缩,我们还可以通过 Python 来执行该操作,实现自动化.批量处理PDF文 ... 
- UART和RS232、RS485的关系是什么?
			串口通讯是电子工程师和嵌入式开发工程师面对的最基本问题,RS232则是其中最简单最常用的通讯方式.但是初学者往往搞不清有关的名词如UART和RS232或RS485之间是什么关系,因为它们经常被放到语句 ... 
- Salt安装部署
			Salt安装部署 一:salt简介  早期运维人员会根据自己的生产环境来写特定脚本完成大量重复性工作,这些脚本复杂且难以维护.系统管理员面临的问题主要是1.系统配置管理,2.远程执行命令,因此诞生了 ... 
- 某手创作服务 __NS_sig3 sig3 | js 逆向
			拿获取作品列表为例 https://cp.kuaishou.com/rest/cp/works/v2/video/pc/photo/list?__NS_sig3=xxxxxxxxxxx 搜索__NS_ ... 
