Package com.thoughtworks.proxy.toys.echo

A toy to trace method calls to objects.

See:
          Description

Class Summary
EchoDecorator<T> A Decorator implementation that echoes any invocation to a PrintWriter.
Echoing<T> Factory for echoing proxy instances.
Echoing.EchoingBuild<T>  
Echoing.EchoingTo<T>  
Echoing.EchoingWithOrTo<T>  
 

Package com.thoughtworks.proxy.toys.echo Description

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 PrintWriter. Main component is the Echoing toy, a utility class creating these proxies. Such a proxy contains an instance of a EchoDecorator that traces all calls. The implementation subclasses the Decorating toy. Every call to a method will be logged to the PrintWriter before the original method is called. If the ProxyFactory in action is capable creating a proxy for the result of the method, then it is also wrapped with an Echoing proxy.

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

@SuppressWarnings("unchecked")
Map<String, Object> map = Echoing.proxy(Map.class)
    .with(new HashMap())
    .to(new PrintWriter(System.err))
    .build(new CglibProxyFactory());
map.put("Date", new Date());
map.put("File", new File("."));
try {
    Iterator<String> iter = map.keySet().iterator();
    while (true) {
        String key = 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



Copyright © 2005-2010 Codehaus. All Rights Reserved.