JBoss.org Community Documentation
Introducing interfaces only is quite limited when the introduced interfaces have methods that the class doesn't implement as you have to write a lot of generically inclined code that handle these types of method calls within an advice or interceptor. This is where mixins come in. The AOP framework allows you to define a mixin class that implements the introduced interface(s). An instance of this mixin class will be allocated the first time you invoke a method of the introduced interface.
Again, let's steal from the introductions tutorial. We'll take an existing class, force it to implement
the
java.io.Externalizable
interface and provide a class that implements
Externalizable
public class POJO { private String field; }
To take this class and make it externalizable is very simple. Just the following XML is required:
<introduction class="POJO"> <mixin> <interfaces> java.io.Externalizable </interfaces> <class>ExternalizableMixin</class> <construction>new ExternalizableMixin(this)</construction> </mixin> </introduction>
The above XML just states that the AOP framework is to apply the
java.io.Externalizable
interface
to the
POJO
class. You can have one or more interfaces specified with the
interfaces
element. These interfaces are comma delimited.
The
class
element defines the mixin class that will implement the externalizable interface
and handle serialization of the
POJO
class.
The
construction
element allows you to specify Java code that will be used to initialize
the mixin class when it is created. JBoss AOP will create a field within the
POJO
class that will hold the instance of the mixin. This field will be initialized with the Java code you provide
in the
construction
element. The
this
pointer in the construction above
pertains to the
POJO
class instance.
Finally, you need to implement the mixin class that will handle externalization.
public class ExternalizableMixin implements java.io.Externalizable { POJO pojo; public ExternalizableMixin(POJO pojo) { this.pojo = pojo; } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { pojo.stuff2 = in.readUTF(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(pojo.stuff2); } }