开始之前
在开始分析原理之前,有必要问一下自己一个问题:
反射是什么?以及其作用是什么?
不论在哪种语言中,我们所提到的反射功能,均指开发者可以在运行时通过调用反射库来获取到来获取到指定对象类型信息,通常类型信息中会包含对象的字段/方法等信息。并且,反射库通常会提供方法的调用, 以及字段赋值等功能。
使用反射可以帮助我们避免写大量重复的代码, 因此反射功能常见用于ORM框架, 以及序列化何反序列化框架,除此之外在Java中反射还被应用到了AOP等功能中。
了解完反射的功能之后,我们再引申一个问题:
假如你开发了一种语言, 该如何为开发者提供反射的功能?
首先,我们知道反射的核心的功能有:
- 类型信息获取
- 对象字段访问/赋值
- 方法调用
因此实际作为语言的开发者(假设),我们要解决的问题有:
- 如何存储并获取到对象类型信息?
- 如何定位到对象字段的内存地址?
注: 只要知道了对象字段的内存地址配合上类型信息,我们便可以实现赋值与访问的操作。
- 如何定位到方法的内存地址?
注:代码在内存中也是数据,因此只需要定位到代码所在的地址,便可解决方法调用的问题
分析
从何处获取类型信息
如果你熟悉Go的reflect(反射)库, 相信你或多或少的听过反射三原则, 即:
-
从
interface{}
可以反射出反射对象 -
从反射对象中可以获取到
interface{}
- 要修改反射对象, 其值必须可设置
根据以上三原则不难看出interface{}
是实现反射功能的基石, 那么这是为什么呢?
要回答这个问题,我们了解interface{}
的本质是什么。
interface{}
本质上Go提供的一种数据类型, 与其他数据类型不同的是, interface{}
会为我们提供变量的类型信息以及变量所在的内存地址。
在Runtime
中使用结构体来表示interface{}
, 其结构如下所示:
1
2
3
4
|
type emptyInterface struct { typ *rtype word unsafe.Pointer } |
该结构体只有两个字段, 分别是:
-
typ
变量的类型信息, 这一步骤在编译步骤便可确定下来 -
word
指向变量数据的指针, 这一步骤在运行时进行确定
接下来我们通过反编译下文的代码, 来观察当把一个变量转换成interface{}
的时候都发生了什么:
1
2
3
4
5
6
7
|
package main import "fmt" func main() { s := 1024 var a interface {} = &s fmt. Println (a) } |
执行以下命令, 获取汇编代码
1
|
go tool compile -N -S .\main. go |
以下代码即为将字符串赋值给interface{}
类型的变量a
的对应汇编代码
1
2
3
4
5
|
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 可以转换成任意类型的指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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) } |
输入如下所示:
因此,不难推出reflect.TypeOf
的实现实际上就是获取interface{}
中type
信息,并返回给开发人员。其代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
|
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
描述了基础的类型信息,其字段如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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中所有数据类型的基础类型信息, 对于不同的数据类型其类型信息会有略微的差异。
1
2
3
4
5
6
7
8
9
10
11
12
|
// 结构体的类型信息 type structType struct { rtype pkgPath name fields []structField // sorted by offset } // channel 的类型信息 type chanType struct { rtype elem *rtype // channel element type dir uintptr // channel direction (ChanDir) } |
如何实现赋值操作?
赋值操作的本质上是往对应的内存地址写入数据, 因此我们有必要简单了解一下结构体在内存中的布局方式, 以一个最为简单坐标的结构体为例,其结构体如下所示:
1
2
3
4
5
|
type Coordinate struct { X int64 Y int64 Z int64 } |
其在内存中的表现为一段大小为24字节的连续内存,具体如下图所示
因此,我们实际上要做的就是获取到结构体的首地址之后,根据各个字段相对首字段的偏移地址计算出其在内存中地址。
实际上在Runtime
提供的类型信息中,已经包含了各个字段的偏移以及类型信息,我们可以具体的来看一下反射功能获取字段Field
的实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
func (v Value) Field(i int ) Value { if v.kind() != Struct { panic (&ValueError{ "reflect.Value.Field" , v.kind()}) } // 获取类型信息 tt := (*structType)(unsafe.Pointer(v.typ)) if uint (i) >= uint ( len (tt.fields)) { panic ( "reflect: Field index out of range" ) } // 获取字段信息 field := &tt.fields[i] typ := field.typ // 继承结构体的部分flag信息 fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind()) if !field.name.isExported() { if field.embedded() { fl |= flagEmbedRO } else { fl |= flagStickyRO } } // 根据偏移地址计 + 结构体的首地址 计算出 字段在内存中的地址, 并返回Value对象 ptr := add(v.ptr, field.offset(), "same as non-reflect &v.field" ) return Value{typ, ptr, fl} } |
了解到如何获取字段在内存中的地址之后,我们再来看看赋值操作是如何实现。
如以下代码SetInt
所示, 本质上还是一些指针的转换以及解引用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
func (v Value) SetInt(x int64 ) { v.mustBeAssignable() switch k := v.kind(); k { default : panic (&ValueError{ "reflect.Value.SetInt" , v.kind()}) case Int : *(* int )(v.ptr) = int (x) case Int8 : *(* int8 )(v.ptr) = int8 (x) case Int16 : *(* int16 )(v.ptr) = int16 (x) case Int32 : *(* int32 )(v.ptr) = int32 (x) case Int64 : *(* int64 )(v.ptr) = x } } |
那么,肯定有同学会问,为啥你一直都在讲结构体啊,那字符串(string
), 切片(slice
), map
呢?
实际上这些Go
的内建的数据类型,在Runtime
中的表现形式也是结构体, 我们可以在reflect
包中找到如下定义:
1
2
3
4
5
6
7
8
9
10
11
|
// 切片头 type SliceHeader struct { Data uintptr // 数组的指针地址 Len int // 数组长度 Cap int // 数组容量 } // 字符串头 type StringHeader struct { Data uintptr // 字节数组的指针地址 Len int // 字节数组的长度 } |
因此,通过反射来操作切片和字符串本质上还是操作结构体。
总结
-
interface{}
是一种数据类型, 其存储了变量的类型信息与数据指针,其中类型信息是在编译期间确定下来的 -
Golang
反射的原理就是从interface{}
中获取到类型信息以及变量的指针,从而实现类型获取以及赋值的功能
以上就是Go reflect 反射原理示例详解的详细内容,更多关于Go reflect 反射原理的资料请关注服务器之家其它相关文章!
原文链接:https://juejin.cn/post/7159797055060672543