Skip to content

Commit

Permalink
add @around ProceedingJoinPoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
xujinzheng committed Feb 4, 2016
1 parent 43df8b7 commit 9df2989
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 137 deletions.
53 changes: 39 additions & 14 deletions aop.go
Expand Up @@ -78,17 +78,10 @@ func (p *AOP) funcWrapper(bean *Bean, methodName string, methodType reflect.Type
advicesGroup = append(advicesGroup, advices)
}

callAdvicesFunc := func(order AdviceOrdering, retValues ...reflect.Value) (e error) {

var result Result
if order == AfterReturning {
for _, v := range retValues {
result = append(result, v.Interface())
}
}
callAdvicesFunc := func(order AdviceOrdering, resultValues ...reflect.Value) (e error) {

for _, advices := range advicesGroup {
if e = invokeAdvices(&joinPoint, advices[order], methodName, result); e != nil {
if e = invokeAdvices(&joinPoint, advices[order], methodName, resultValues); e != nil {
if errOutIndex >= 0 {
ret[errOutIndex] = reflect.ValueOf(&e).Elem()
}
Expand All @@ -104,9 +97,41 @@ func (p *AOP) funcWrapper(bean *Bean, methodName string, methodType reflect.Type
return
}

//@Normal func
//@Real func
var retValues []reflect.Value

funcInSturctName := getFuncNameByStructFuncName(methodName)
retValues := beanValue.MethodByName(funcInSturctName).Call(inputs)

realFunc := func(args ...interface{}) Result {
values := []reflect.Value{}
for _, arg := range args {
values = append(values, reflect.ValueOf(arg))
}

return beanValue.MethodByName(funcInSturctName).Call(values)
}

//@Around
var aroundAdvice *Advice
for _, advices := range advicesGroup {
if aroundAdvices, exist := advices[Around]; exist && len(aroundAdvices) > 0 {
aroundAdvice = aroundAdvices[0]
break
}
}

if aroundAdvice != nil {
pjp := ProceedingJoinPoint{JoinPointer: &joinPoint, method: realFunc}

if err = invokeAdvices(&pjp, []*Advice{aroundAdvice}, methodName, nil); err != nil {
if errOutIndex >= 0 {
ret[errOutIndex] = reflect.ValueOf(&err).Elem()
}
return
}
} else {
retValues = realFunc(inputs)
}

if IsTracing() {
var metadata MethodMetadata
Expand Down Expand Up @@ -157,14 +182,14 @@ func (p *AOP) GetProxy(beanID string) (proxy *Proxy, err error) {
mType := methodV.Type()

var metadata MethodMetadata
if metadata, err = getMethodMetadata(methodT.Func.Interface()); err != nil {
if metadata, err = getMethodMetadata(methodT); err != nil {
return
}

newFunc := p.funcWrapper(bean, metadata.MethodName, mType)
newFunc := p.funcWrapper(bean, metadata.Method.Name, mType)
funcV := reflect.MakeFunc(mType, newFunc)

metadata.method = funcV.Interface() // rewrite to new proxy func
metadata.Method.Func = funcV // rewrite to new proxy func

tmpProxy.registryFunc(metadata)
}
Expand Down
2 changes: 1 addition & 1 deletion bean.go
Expand Up @@ -61,7 +61,7 @@ func (p *Bean) methodMetadata(methodName string) (metadata MethodMetadata, err e
return
}

metadata, err = getMethodMetadata(method.Func.Interface())
metadata, err = getMethodMetadata(method)

return
}
Expand Down

0 comments on commit 9df2989

Please sign in to comment.