Documentation
¶
Overview ¶
Package generator 提供 AOP 代码生成功能,用于 enhance 框架。
该模块用于生成静态代理代码,提高运行时性能。 通过代码生成避免运行时动态代理的性能开销。
架构设计 ¶
- CodeGenerator: 代码生成器接口,定义代码生成逻辑
- ProxyGenerator: 代理代码生成器,生成静态代理类
- TemplateManager: 模板管理器,管理代码生成模板
- MetadataParser: 元数据解析器,解析 AOP 元数据
核心功能 ¶
- 静态代理生成: 在编译时生成代理代码
- 模板引擎: 支持自定义代码生成模板
- 元数据解析: 解析注解和切面定义
- 代码格式化: 生成格式化的 Go 代码
使用方式 ¶
使用 go generate 生成代码:
//go:generate go run github.com/xudefa/enhance/cmd/goaop
或使用代码生成器 API:
generator := generator.NewProxyGenerator()
generator.SetOutputDir("./generated")
generator.Generate(targetType, aspects)
生成的代码 ¶
生成的代理代码包含:
- 代理结构体: 包装目标对象
- 方法拦截: 拦截目标方法调用
- 切面执行: 按顺序执行切面逻辑
- 异常处理: 处理切面执行中的异常
性能优势 ¶
静态代理相比动态代理的优势:
- 无运行时反射开销
- 编译器优化更有效
- 代码可调试性更好
- 启动时间更短
Index ¶
- Constants
- func RenderInterfaceProxy(data *InterfaceProxyTemplateData) (string, error)
- func RenderStaticProxy(data *StaticProxyTemplateData) (string, error)
- type AdviceAdapterData
- type AdviceAdapterTemplateData
- type AdviceBindingData
- type AdviceField
- type AdviceInfo
- type AdviceRef
- type AdviceType
- type AspectInfo
- type AspectTemplateData
- type Generator
- type GoGenerateDirective
- type InterfaceInfo
- type InterfaceProxyTemplateData
- type MethodInfo
- type MethodTemplateData
- type ParamInfo
- type Parser
- type ProxyInfo
- type ProxyTemplateData
- type Registry
- type StaticMethodInfo
- type StaticProxyTemplateData
- type TemplateEngine
Constants ¶
const InterfaceProxyTemplate = `` /* 1200-byte string literal not displayed */
InterfaceProxyTemplate 接口代理模板
const StaticProxyTemplate = `` /* 1573-byte string literal not displayed */
StaticProxyTemplate 静态代理模板(零反射)
生成的代理代码直接内联通知链,完全消除运行时反射调用。
Variables ¶
This section is empty.
Functions ¶
func RenderInterfaceProxy ¶
func RenderInterfaceProxy(data *InterfaceProxyTemplateData) (string, error)
RenderInterfaceProxy 渲染接口代理代码
func RenderStaticProxy ¶
func RenderStaticProxy(data *StaticProxyTemplateData) (string, error)
RenderStaticProxy 渲染静态代理代码
Types ¶
type AdviceAdapterData ¶
type AdviceAdapterData struct {
FuncName string // 适配器函数名
AspectType string // 切面类型名
MethodName string // 切面方法名
IsAround bool // 是否为环绕通知
HasReturn bool // 是否有返回值
}
AdviceAdapterData 通知适配器数据
type AdviceAdapterTemplateData ¶
type AdviceAdapterTemplateData struct {
Package string // 包名
Adapters []AdviceAdapterData // 适配器列表
}
AdviceAdapterTemplateData 通知适配器模板数据
type AdviceBindingData ¶
AdviceBindingData 通知绑定数据
type AdviceInfo ¶
type AdviceInfo struct {
Type AdviceType // 通知类型
Method string // 通知方法名
Targets []string // 目标方法列表(格式:StructName.MethodName)
IsFunc bool // 是否为独立函数(非结构体方法)
FuncName string // 函数名(独立函数时使用)
Package string // 所属包名
AspectName string // 所属切面名称
}
AdviceInfo 通知信息
type AdviceType ¶
type AdviceType string
AdviceType 通知类型
const ( AdviceBefore AdviceType = "before" // 前置通知 AdviceAfter AdviceType = "after" // 后置通知 AdviceAround AdviceType = "around" // 环绕通知 AdviceAfterReturning AdviceType = "after_returning" // 返回后通知 AdviceAfterThrowing AdviceType = "after_throwing" // 异常通知 )
type AspectInfo ¶
type AspectInfo struct {
Name string // 切面名称
Order int // 切面优先级(值越小优先级越高)
Package string // 所属包名
Advices []AdviceInfo // 通知列表
}
AspectInfo 切面信息
type AspectTemplateData ¶
type AspectTemplateData struct {
MethodName string // 目标方法名
AdviceType string // 通知类型(Before/After/Around 等)
AdviceFunc string // 通知函数名
Order int // 切面优先级
AspectName string // 切面名称
AspectMethodName string // 切面方法名
}
AspectTemplateData 切面模板数据
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator AOP 代理代码生成器
解析 Go 源码中的注解,匹配切面与代理目标, 生成代理类代码文件。
type GoGenerateDirective ¶
type GoGenerateDirective struct {
Types []string // 需要生成代理的结构体类型
Interfaces []string // 需要生成代理的接口类型
Output string // 输出文件名(默认 *_aop.go)
Package string // 包名
Mode string // 生成模式:simple, aop, static
All bool // 是否为所有类型生成代理
}
GoGenerateDirective 解析 //go:generate 指令
支持以下格式:
- //go:generate enhance aop gen -type=UserService
- //go:generate enhance aop gen -type=UserService,OrderService
- //go:generate enhance aop gen -type=UserService -output=proxy.go
- //go:generate enhance aop gen -interface=ServiceInterface
- //go:generate enhance aop gen -all
func ParseGoGenerate ¶
func ParseGoGenerate(comment string) (*GoGenerateDirective, error)
ParseGoGenerate 解析 //go:generate 指令
func (*GoGenerateDirective) HasTargets ¶
func (d *GoGenerateDirective) HasTargets() bool
HasTargets 检查是否有需要生成的目标
type InterfaceInfo ¶
type InterfaceInfo struct {
Name string // 接口名称
Package string // 所属包名
FilePath string // 源文件路径
Methods []MethodInfo // 方法列表
BeanID string // Bean 标识
Aspects []AspectInfo // 关联的切面列表
}
InterfaceInfo 接口信息
type InterfaceProxyTemplateData ¶
type InterfaceProxyTemplateData struct {
Package string
ProxyName string
InterfaceName string
SourceFile string
Imports []string
Methods []StaticMethodInfo
AdviceFields []AdviceField
}
InterfaceProxyTemplateData 接口代理模板数据
type MethodInfo ¶
type MethodInfo struct {
Name string // 方法名
Receiver string // 接收者类型名
Params []ParamInfo // 参数列表
Results []ParamInfo // 返回值列表
Exported bool // 是否为导出方法
}
MethodInfo 方法信息
type MethodTemplateData ¶
type MethodTemplateData struct {
Name string // 方法名
ParamsStr string // 参数列表字符串
ResultsStr string // 返回值列表字符串
ArgsList string // 参数名列表字符串
HasError bool // 是否包含 error 返回值
HasMultipleReturns bool // 是否有多个返回值
HasSingleReturn bool // 是否仅有单个非 error 返回值
HasSingleErrorReturn bool // 是否仅有单个 error 返回值
NoReturn bool // 是否无返回值
SingleReturnType string // 单返回值类型
ReturnValues string // 返回值表达式
ReturnValuesWithError string // 含 error 的返回值表达式
ReturnValuesFromTuple string // 从元组解析的返回值表达式
ReturnValuesFallback string // 返回值回退表达式
HasAspects bool // 是否有切面增强
HasReturnValue bool // 是否有返回值(用于静态模板)
HasParams bool // 是否有参数(用于静态模板)
HasBeforeAdvices bool // 是否有 Before 通知
HasAroundAdvices bool // 是否有 Around 通知
FirstAroundAdviceFunc string // 第一个 Around 通知函数名
BeforeAdvices []AdviceBindingData // Before 通知列表
AroundAdvices []AdviceBindingData // Around 通知列表
AfterAdvices []AdviceBindingData // After 通知列表
AfterReturningAdvices []AdviceBindingData // AfterReturning 通知列表
AfterThrowingAdvices []AdviceBindingData // AfterThrowing 通知列表
}
MethodTemplateData 方法模板数据
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser Go 源码注解解析器
扫描 Go 源码文件,解析 @Aspect、@AopProxy、@ProxyInterface、@Before 等注解, 提取切面、代理和通知信息。
func (*Parser) GetInterfaces ¶
func (p *Parser) GetInterfaces() []InterfaceInfo
GetInterfaces 获取所有解析到的接口信息
type ProxyInfo ¶
type ProxyInfo struct {
Name string // 代理名称(即目标结构体名)
Package string // 所属包名
FilePath string // 源文件路径
Target string // 目标结构体名
Methods []MethodInfo // 方法列表
Aspects []AspectInfo // 关联的切面列表
BeanID string // Bean 标识
}
ProxyInfo 代理信息
type ProxyTemplateData ¶
type ProxyTemplateData struct {
Package string // 包名
ProxyName string // 代理类名
TargetName string // 目标结构体名
BeanID string // Bean 标识
Imports []string // 导入列表
Aspects []AspectTemplateData // 切面数据列表
Methods []MethodTemplateData // 方法数据列表
Dependencies []string // 依赖列表
}
ProxyTemplateData 代理模板数据
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry 代理注册表
记录 Bean 标识与生成文件路径的映射关系,支持持久化到 JSON 文件。 使用 sync.Map 优化并发读取性能(读多写少场景)。
type StaticMethodInfo ¶
type StaticMethodInfo struct {
Name string
Params []ParamInfo
Results []string
ParamsStr string
ResultsStr string
ArgsList string
HasParams bool
HasReturnValue bool
HasAspects bool
BeforeAdvices []AdviceRef
AfterAdvices []AdviceRef
AfterReturningAdvices []AdviceRef
AroundAdvice *AdviceRef
}
StaticMethodInfo 静态代理方法信息
type StaticProxyTemplateData ¶
type StaticProxyTemplateData struct {
Package string
ProxyName string
TargetName string
SourceFile string
Imports []string
Methods []StaticMethodInfo
AdviceFields []AdviceField
}
StaticProxyTemplateData 静态代理模板数据
type TemplateEngine ¶
type TemplateEngine struct {
// contains filtered or unexported fields
}
TemplateEngine 代码模板引擎
管理代理代码生成的 Go 模板,支持简单代理、AOP 增强代理、静态 AOP 代理和静态接口代理四种模式。
func NewTemplateEngine ¶
func NewTemplateEngine() (*TemplateEngine, error)
NewTemplateEngine 创建代码模板引擎
func (*TemplateEngine) GenerateAdviceAdapter ¶
func (e *TemplateEngine) GenerateAdviceAdapter(data AdviceAdapterTemplateData) (string, error)
GenerateAdviceAdapter 生成通知适配器代码
func (*TemplateEngine) GenerateInterfaceProxy ¶
func (e *TemplateEngine) GenerateInterfaceProxy(data ProxyTemplateData, mode string) (string, error)
GenerateInterfaceProxy 根据模板数据生成接口代理代码
mode 参数: "simple" 使用简单代理模板, "aop" 使用 AOP 增强模板, "static" 使用静态接口代理模板(零反射)。
func (*TemplateEngine) GenerateProxy ¶
func (e *TemplateEngine) GenerateProxy(data ProxyTemplateData, mode string) (string, error)
GenerateProxy 根据模板数据生成代理代码
mode 参数: "simple" 使用简单代理模板, "aop" 使用 AOP 增强模板, "static" 使用静态 AOP 模板(零反射)。