Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Proxy using Scalas new Dynamic Type

Is it possible to create an AOP like interceptor using Scalas new Dynamic Type feature? For example: Would it be possible to create a generic stopwatch interceptor that could be mixed in with arbitrary types to profile my code? Or would I still have to use AspectJ?

like image 435
gruenewa Avatar asked Mar 03 '11 19:03

gruenewa


2 Answers

I'm pretty sure Dynamic is only used when the object you're selecting on doesn't already have what you're selecting:

From the nightly scaladoc:

Instances x of this trait allow calls x.meth(args) for arbitrary method names meth and argument lists args. If a call is not natively supported by x, it is rewritten to x.invokeDynamic("meth", args)

Note that since the documentation was written, the method has been renamed applyDynamic.

like image 98
Alex Cruise Avatar answered Oct 20 '22 18:10

Alex Cruise


No.

In order for a dynamic object to be supplied as a parameter, it'll need to have the expected type - which means inheriting from the class you want to proxy, or from the appropriate superclass / interface.

As soon as you do this, it'll have the relevant methods statically provided, so applyDynamic would never be considered.

like image 44
Kevin Wright Avatar answered Oct 20 '22 19:10

Kevin Wright