01: /*
02: * Copyright (C) 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 01. January 2008 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.cache.targets;
12:
13: import com.thoughtworks.xstream.tools.benchmark.Target;
14:
15: import java.awt.Color;
16: import java.lang.reflect.InvocationHandler;
17: import java.lang.reflect.Method;
18: import java.lang.reflect.Proxy;
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: /**
23: * Target containing extended types.
24: *
25: * @author Jörg Schaible
26: * @since 1.3
27: */
28: public class ExtendedTarget implements Target {
29:
30: private final static Method EQUALS;
31: static {
32: Method method;
33: try {
34: method = Object.class.getMethod("equals",
35: new Class[] { Object.class });
36: } catch (NoSuchMethodException e) {
37: throw new ExceptionInInitializerError(e);
38: }
39: EQUALS = method;
40: }
41:
42: private List list;
43:
44: public ExtendedTarget() {
45: list = new ArrayList();
46: list.add(new Color(128, 0, 255));
47: Object proxy = Proxy.newProxyInstance(getClass()
48: .getClassLoader(), new Class[] { Runnable.class },
49: new RunnableInvocationHandler());
50: list.add(proxy);
51: list.add(ExtendedTarget.class);
52: list.add(EQUALS);
53: }
54:
55: public boolean isEqual(Object other) {
56: return list.equals(other);
57: }
58:
59: public Object target() {
60: return list;
61: }
62:
63: public String toString() {
64: return "Extended types";
65: }
66:
67: static class RunnableInvocationHandler implements InvocationHandler {
68:
69: public Object invoke(Object proxy, Method method, Object[] args)
70: throws Throwable {
71: if (method.equals(EQUALS)) {
72: return new Boolean(args[0] instanceof Runnable);
73: } else if (method.getName().equals("hashCode")) {
74: return new Integer(System.identityHashCode(proxy));
75: } else if (method.getName().equals("toString")) {
76: return "Proxy" + System.identityHashCode(proxy);
77: } else if (method.getName().equals("getClass")) {
78: return proxy.getClass();
79: }
80: return null;
81: }
82:
83: }
84: }
|