A toy to decorate the interaction with another object.
The package provides a proxy factory creating proxies, that are used to decorate the interaction with another object as done with AOP. Main component is the {@linkplain com.thoughtworks.proxy.toys.decorate.Decorating Decorating toy}, a utility class creating these proxies. Such a proxy contains an instance of a {@link com.thoughtworks.proxy.toys.decorate.DecoratingInvoker} that defines the additional functionality. A DecoratingInvoker is invoked for each method call on the proxy, for each value returned from a proxied method, for each exception thrown from the original object's method and for each occurring exception invoking the method in the original object.
The following example decorates an iterator of a list of strings to convert every String into an Integer. This is obviously not a type safe operation, therefore all warnings for using unchecked types are suppressed:
List<String> list = Arrays.asList("1", "2", "3");
@SuppressWarnings({"serial", "unchecked"})
Decorator<Iterator> decorator = new Decorator<Iterator>() {
@Override
public Object decorateResult(Iterator proxy, Method method, Object[] args, Object result) {
if (method.getName().equals("next"))
return Integer.valueOf(String.class.cast(result));
else
return result;
}
};
@SuppressWarnings("unchecked")
Iterator<Integer> intIter = Decorating.proxy(Iterator.class)
.with(list.iterator())
.visiting(decorator)
.build();
while (intIter.hasNext()) {
Integer i = intIter.next();
System.out.println(i);
}
The next example implements a simple trace mechanism for a single object. Since we proxy here a real object instead of an interface, we have to use the {@link com.thoughtworks.proxy.factory.CglibProxyFactory}:
File file = new File(".");
@SuppressWarnings("serial")
Decorator decorator = new Decorator() {
@Override
public Object[] beforeMethodStarts(Object proxy, Method method, Object[] args) {
System.out.print("Called: " + method.getName());
return super.beforeMethodStarts(proxy, method, args);
}
@Override
public Object decorateResult(Object proxy, Method method, Object[] args, Object result) {
System.out.println(" ==< " + result);
return result;
}
};
File decoratedFile = Decorating.proxy(File.class)
.with(file, decorator)
.build(new CglibProxyFactory());
decoratedFile.exists();
decoratedFile.isFile();
decoratedFile.isDirectory();
For a more sophisticated trace mechanism, have a look at the {@linkplain com.thoughtworks.proxy.toys.echo Echoing toy}