dynaop manual

Chapter 4. Usage

Table of Contents

Dynamic Proxies
Class Proxies
Documentation Tools

To hook dynaop into your application, use a ProxyFactory to proxy your objects at creation time. Access the default ProxyFactory instance using ProxyFactory.getInstance(). The ProxyFactory will dynamically create a proxy instance that attaches the method interceptors and mixins to your object.

ProxyFactory supports two methods of proxy creation, dynamic proxy and class proxy. Each method has its pros and cons.

Dynamic Proxies

With dynamic proxies, ProxyFactory creates an entirely separate proxy class that implements all of the interfaces from your target class and the added mixins. The proxy wraps an already existing target instance. Dynamic proxies have the following characteristics:

  • keeps target object and proxy instance separate
  • easy object serialization
  • works with objects created by someone else
  • requires that the target object has an interface

Example 4.1. dynamic proxy wrapping

MyClass myInstance = new MyClassImpl();
myInstance = (MyClass) ProxyFactory.getInstance().wrap(myInstance);

((MyMixin) myInstance).invokeMixin();