A toy to trace method calls to objects.

The package provides a proxy factory creating proxies, that are used to log method calls to objects and their resulting objects to a {@link java.io.PrintWriter}. Main component is the {@linkplain com.thoughtworks.proxy.toys.echo.Echoing Echoing toy}, a utility class creating these proxies. Such a proxy contains an instance of a {@link com.thoughtworks.proxy.toys.echo.EchoDecorator} that traces all calls. The implementation subclasses the {@linkplain com.thoughtworks.proxy.toys.decorate.Decorating Decorating toy}. Every call to a method will be logged to the PrintWriter before the original method is called. If the {@link com.thoughtworks.proxy.ProxyFactory} in action is capable creating a proxy for the result of the method, then it is also wrapped with an Echoing proxy.

The follwoing example uses the proxy and demonstrates, how any other returned object is also proxied:

Map map = (Map)Echoing.object(Map.class, new HashMap(), new CglibProxyFactory());
map.put("Date", new Date());
map.put("File", new File("."));
try {
    Iterator iter = map.keySet().iterator();
    while (true) {
        String key = (String)iter.next();
        Object value = map.get(key);
        if (value instanceof Date) {
            Date date = (Date)value;
            date.setTime(4711);
        } else if (value instanceof File) {
            File file = (File)value;
            if (file.exists()) {
               file.renameTo(new File(".."));
            }
        }
    }
} catch (NoSuchElementException e) {
    // No further element
}

The provoked exception is intentional, see the output, that demonstrates the functionality best:

[main] java.util.Map.put(<Date>, <Fri Jul 22 23:35:44 CEST 2005>) --> <NULL>
[main] java.util.Map.put(<File>, <.>) --> <NULL>
[main] java.util.Map.keySet() --> <[Date, File]>
[main] java.util.Set.iterator() --> <java.util.HashMap$HashIterator@4a5ab2>
[main] java.util.Iterator.next() --> <Date>
[main] java.util.Map.get(<Date>) --> <Fri Jul 22 23:35:44 CEST 2005>
[main] java.util.Date.setTime(<4711>) --> <NULL>
[main] java.util.Iterator.next() --> <File>
[main] java.util.Map.get(<File>) --> <.>
[main] java.io.File.exists() --> <true>
[main] java.io.File.renameTo(<..>) --> <false>
[main] java.util.Iterator.next() throws java.util.NoSuchElementException: null