The POJO classes that inherit from Set , List , and Map are treated as "aspectized" automatically. That is, users need not declare them "prepared" in the xml configuration file or via annotation. Since we are not allowed to instrument the Java system library, we will use a proxy approach instead. That is, when we encounter any Collection instance, we will:
Create a Collection proxy instance and place it in the cache (instead of the original reference). The mapping of the Collection elements will still be carried out recursively as expected.
If the Collection instance is a sub-object, e.g., inside another POJO, we will swap out the original reference with the new proxy one to promote transparent usage.
To obtain the proxy reference, users can then use another getObject to retrieve this proxy reference and use this reference to perform POJO operations.
Here is a code snippet that illustrates the usage of a Collection proxy reference:
List list = new ArrayList();
list.add("ONE");
list.add("TWO");
tree.putObject("/aop/list", list);
list.add("THREE"); // This won't intercept by the cache!
List proxyList = tree.getObject("/aop/list"; // Note that list is a proxy reference
proxyList.add("FOUR"); // This will be intercepted by the cache
Here is another snippet to illustrate the dynamic swapping of the Collection reference when it is embedded inside another object:
Person joe = new Person();
joe.setName("Joe Black"); // This is base class attributes
ArrayList lang = new ArrayList();
lang.add("English");
lang.add("Mandarin");
joe.setLanguages(lang);
// This will map the languages List automatically and swap it out with the proxy reference.
tree.putObject("/aop/student/joe", joe);
ArrayList lang = joe.getLanguages(); // Note that lang is a proxy reference
lang.add("French"); // This will be intercepted by the cache
As you can see, getLanguages simply returns the field reference that has been swapped out for the proxy reference counterpart.
Finally, when you remove a Collection reference from the cache (e.g., via removeObject), you still can use the proxy reference since we will update the in-memory copy of that reference during detachment. Below is a code snippet illustrating this:
List list = new ArrayList();
list.add("ONE");
list.add("TWO");
tree.putObject("/aop/list", list);
List proxyList = tree.getObject("/aop/list"); // Note that list is a proxy reference
proxyList.add("THREE"); // This will be intercepted by the cache
tree.removeObject("/aop/list"); // detach from the cache
proxyList.add("FOUR"); // proxyList has 4 elements still.