01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 25. April 2004 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance.objects;
13:
14: import java.lang.reflect.InvocationHandler;
15: import java.lang.reflect.Method;
16: import java.lang.reflect.Proxy;
17:
18: public class SampleDynamicProxy implements InvocationHandler {
19:
20: private String aField = "hello";
21:
22: public static interface InterfaceOne {
23: String doSomething();
24: }
25:
26: public static interface InterfaceTwo {
27: String doSomething();
28: }
29:
30: public static Object newInstance() {
31: return Proxy.newProxyInstance(InterfaceOne.class
32: .getClassLoader(), new Class[] { InterfaceOne.class,
33: InterfaceTwo.class }, new SampleDynamicProxy());
34: }
35:
36: public Object invoke(Object proxy, Method method, Object[] args)
37: throws Throwable {
38: if (method.getName().equals("equals")) {
39: return equals(args[0]) ? Boolean.TRUE : Boolean.FALSE;
40: } else {
41: return aField;
42: }
43: }
44:
45: public boolean equals(Object obj) {
46: return equalsInterfaceOne(obj) && equalsInterfaceTwo(obj);
47: }
48:
49: private boolean equalsInterfaceOne(Object o) {
50: if (o instanceof InterfaceOne) {
51: InterfaceOne interfaceOne = (InterfaceOne) o;
52: return aField.equals(interfaceOne.doSomething());
53: } else {
54: return false;
55: }
56: }
57:
58: private boolean equalsInterfaceTwo(Object o) {
59: if (o instanceof InterfaceTwo) {
60: InterfaceTwo interfaceTwo = (InterfaceTwo) o;
61: return aField.equals(interfaceTwo.doSomething());
62: } else {
63: return false;
64: }
65: }
66:
67: }
|