reflect|青训营笔记

38 阅读5分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 18 天

开始之前

在开始分析原理之前,有必要问一下自己一个问题:

反射是什么?以及其作用是什么?

不论在哪种语言中,我们所提到的反射功能,均指开发者可以在运行时通过调用反射库来获取到来获取到指定对象类型信息,通常类型信息中会包含对象的字段/方法等信息。并且,反射库通常会提供方法的调用, 以及字段赋值等功能。

使用反射可以帮助我们避免写大量重复的代码, 因此反射功能常见用于ORM框架, 以及序列化何反序列化框架,除此之外在Java中反射还被应用到了AOP等功能中。

了解完反射的功能之后,我们再引申一个问题:

假如你开发了一种语言, 该如何为开发者提供反射的功能?

首先,我们知道反射的核心的功能有:

  • 类型信息获取
  • 对象字段访问/赋值
  • 方法调用

因此实际作为语言的开发者(假设),我们要解决的问题有:

  • 如何存储并获取到对象类型信息?
  • 如何定位到对象字段的内存地址?

注: 只要知道了对象字段的内存地址配合上类型信息,我们便可以实现赋值与访问的操作。

  • 如何定位到方法的内存地址?

注:代码在内存中也是数据,因此只需要定位到代码所在的地址,便可解决方法调用的问题

分析

从何处获取类型信息

如果你熟悉Go的reflect(反射)库, 相信你或多或少的听过反射三原则, 即:

  • interface{}可以反射出反射对象
  • 从反射对象中可以获取到interface{}
  • 要修改反射对象, 其值必须可设置

根据以上三原则不难看出interface{}是实现反射功能的基石, 那么这是为什么呢?

要回答这个问题,我们了解interface{}的本质是什么。

interface{}本质上Go提供的一种数据类型, 与其他数据类型不同的是, interface{}会为我们提供变量的类型信息以及变量所在的内存地址。

Runtime中使用结构体来表示interface{}, 其结构如下所示:

type emptyInterface struct {
    typ  *rtype
    word unsafe.Pointer  
}
复制代码

该结构体只有两个字段, 分别是:

  • typ 变量的类型信息, 这一步骤在编译步骤便可确定下来
  • word 指向变量数据的指针, 这一步骤在运行时进行确定

接下来我们通过反编译下文的代码, 来观察当把一个变量转换成interface{}的时候都发生了什么:

package main

import "fmt"

func main() {
	s := 1024
	var a interface{} = &s
	fmt.Println(a)
}
复制代码

执行以下命令, 获取汇编代码

go tool compile -N -S .\main.go
复制代码

以下代码即为将字符串赋值给interface{}类型的变量a的对应汇编代码

0x0057 00087 (.\main.go:7)      MOVQ    "".&s+104(SP), AX
0x005c 00092 (.\main.go:7)      MOVQ    AX, ""..autotmp_9+88(SP)
0x0061 00097 (.\main.go:7)      LEAQ    type.*int(SB), CX
0x0068 00104 (.\main.go:7)      MOVQ    CX, "".a+144(SP)
0x0070 00112 (.\main.go:7)      MOVQ    AX, "".a+152(SP)
复制代码

相信即便你不熟悉汇编,但至少也发现了, 以上代码做了如下操作:

  • 获取变量s的地址, 保存到AX寄存器, 并往a+144的地址写入数据
  • 获取变量s的类型信息(type.*int),保存到CX寄存器, 并往a+152的地址写入数据

注:感兴趣的读者可以把取地址的操作去掉,再看看有什么不同

此外, 我们还可以通过指针数据类型转换来获取到interface{}中的数据来侧面验证一下。

注: unsafe.Pointer 可以转换成任意类型的指针

type EmptyInterface struct {
	typ  unsafe.Pointer
	word unsafe.Pointer
}

func getWordPtr(i interface{})  unsafe.Pointer {
	eface := *(*EmptyInterface)(unsafe.Pointer(&i))
	return eface.word
}

func Test_GetWordPtr(t *testing.T) {
	str := "Hello, KeSan"
	strPtr := &str
	//此处由编译器做了类型转换 *string -> interface{}
	wordPtr := getWordPtr(strPtr)
	t.Logf("String Ptr: %p",  strPtr)
	t.Logf("Word Ptr: %p", wordPtr)
}
复制代码

输入如下所示:

image.png

因此,不难推出reflect.TypeOf的实现实际上就是获取interface{}type信息,并返回给开发人员。其代码如下所示:


func TypeOf(i interface{}) Type {
	eface := *(*emptyInterface)(unsafe.Pointer(&i))
	return toType(eface.typ)
}

// 将 *rtype 转成接口类型的Type
func toType(t *rtype) Type {
	if t == nil {
		return nil
	}
	return t
}
复制代码

再进一步我们可以来看看类型信息中都包含了什么?

结构体rtype描述了基础的类型信息,其字段如下所示:

type rtype struct {
	size       uintptr
	ptrdata    uintptr // number of bytes in the type that can contain pointers
	hash       uint32  // hash of type; avoids computation in hash tables
	tflag      tflag   // extra type information flags
	align      uint8   // alignment of variable with this type
	fieldAlign uint8   // alignment of struct field with this type
	kind       uint8   // enumeration for C
	// function for comparing objects of this type
	// (ptr to object A, ptr to object B) -> ==?
	equal     func(unsafe.Pointer, unsafe.Pointer) bool
	gcdata    *byte   // garbage collection data
	str       nameOff // string form
	ptrToThis typeOff // type for pointer to this type, may be zero
}

复制代码

rtype结构体包含了Golang中所有数据类型的基础类型信息, 对于不同的数据类型其类型信息会有略微的差异。

作者:飞天大薯条
链接:juejin.cn/post/715979…
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。