目录
概念
Java programming language and JVM modeling in core reflection
The components of core reflection, which include types in this package as well as?Class,?Package, and?Module, fundamentally present a JVM model of the entities in question rather than a Java programming language model. A Java compiler, such as?javac , translates Java source code into executable output that can be run on a JVM, primarily?class ?files. Compilers for source languages other than Java can and do target the JVM as well.
The translation process, including from Java language sources, to executable output for the JVM is not a one-to-one mapping. Structures present in the source language may have no representation in the output and structures?not?present in the source language may be present in the output. The latter are called?synthetic?structures. Synthetic structures can include?methods,?fields,?parameters,?classes and interfaces. One particular kind of synthetic method is a?bridge method. It is possible a synthetic structure may not be marked as such. In particular, not all?class ?file versions support marking a parameter as synthetic. A source language compiler generally has multiple ways to translate a source program into a?class ?file representation. The translation may also depend on the version of the?class ?file format being targeted as different?class ?file versions have different capabilities and features. In some cases the modifiers present in the?class ?file representation may differ from the modifiers on the originating element in the source language, including?final?on a?parameter?and?protected ,?private , and?static ?on?classes and interfaces.
Besides differences in structural representation between the source language and the JVM representation, core reflection also exposes runtime specific information. For example, the?class loaders?and?protection domains?of a?Class ?are runtime concepts without a direct analogue in source code.
见原文:java.lang.reflect (Java SE 18 & JDK 18)
场景
????????Bean转为一个Map集合,我们在研发底层代码时,有前段传一个参数给我们数据接口通过一个传输对象接受后,在处理业务中有的第三方接口或者业务需要把一个Bean转换为通用的Map结构。
????????Spring IOC实例化,底层通过反射来实例化Bean,实例完成后就放入到ApplicationConxt上下文中。
????????AOP反射的实现,在AOP中,就是通过动态代理来实现我们在处理前 后 中的不同阶段来实现公共逻辑抽象与集中处理逻辑。
Class加载
ClassLoad加载
Class<?> aClass = ClassLoader.getSystemClassLoader().loadClass("java.lang.String");
Class加载
Class<?> aClass = Class.forName("java.lang.String");
通过实例
LoadClass loadClass = new LoadClass();
Class<? extends LoadClass> aClass1 = loadClass.getClass();
Class详情
????????在这个之前我们定义一个用户Bean来实现获取Class信息,我们定义一用户信息Bean包含:用户ID、用户名称、用户身份证号、定义如下:
package com.jdk.reflect;
import java.io.Serializable;
/**
* Copyright (C), 2000-2022
* FileName: UserDto.java
* Author: yangcaho.cool@gamil.com
* Date: 2022/4/29 10:49
* Description: 用户
*/
public class UserDto implements Serializable {
/**
* 用户ID
*/
private Long userId;
/**
* 用户名称
*/
private String userName;
/**
* 身份证件号
*/
private String cardNo;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
}
字符串链接
/**
* @Author: yangchao.cool
* @Date: 2022/4/29 11:11
* Description: 字符链接
*/
private String join(Stream<String> param) {
StringBuffer result = new StringBuffer();
param.forEach((key)->{
if (result.length() > 0) {
result.append(",");
}
result.append(key);
});
return result.toString();
}
包
字段
方法
注解
构造函数
实例化
访问私有变量和私有方法
动态类型反射(泛型/数组)
泛型
数组
动态代理
实例
Bean转MAP
Http接口转发
|