Hi,
I've successfully used round advice to intercept input parameter for a method.
However, it seems constructor does not work for me. Here're my test: (It's in scala, but should be easy to understand)
class MyObjectTest extends FlatSpecLike with Matchers {
"MyObjectAspect" should "work" in {
val t = new MyObject("leon")
val result = t.talk()
println(result)
result should be("LEON")
}
}
class MyObject(text: String) {
def talk(): String = {
println("MyObject " + text)
text
}
}
@Aspect
class MyObjectAspect {
@Around(value = "execution (com.leon.aop.MyObject.new(..))")
def constructCP(jp: ProceedingJoinPoint): Object = {
try {
println("Start...")
val args = jp.getArgs
args(0) = args(0).toString.toUpperCase
jp.proceed(args)
} finally {
println("End...")
}
}
}
output:
Start...
End...
MyObject leon
leon
"[leon]" was not equal to "[LEON]"
org.scalatest.exceptions.TestFailedException: "[leon]" was not equal to "[LEON]"
at org.scalatest.MatchersHelper$.newTestFailedException(MatchersHelper.scala:160)
at org.scalatest.Matchers$ShouldMethodHelper$.shouldMatcher(Matchers.scala:6141)
at org.scalatest.Matchers$AnyShouldWrapper.should(Matchers.scala:6175)
at com.leon.aop.MyObjectTest$$anonfun$1.apply$mcV$sp(ConstructorTest.scala:18)
Anything wrong?
Thanks
Leon