1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Method;
21
22 /***
23 * BaseObjectProxy
24 *
25 * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
26 * @version $Id: BaseObjectProxy.java 516448 2007-03-09 16:25:47Z ate $
27 */
28 public class BaseObjectProxy implements InvocationHandler
29 {
30
31 protected static Method hashCodeMethod;
32 protected static Method equalsMethod;
33 protected static Method toStringMethod;
34
35 static
36 {
37 try
38 {
39 hashCodeMethod = Object.class.getMethod("hashCode", null);
40 equalsMethod = Object.class.getMethod("equals", new Class [] { Object.class });
41 toStringMethod = Object.class.getMethod("toString", null);
42 }
43 catch (NoSuchMethodException e)
44 {
45 throw new NoSuchMethodError(e.getMessage());
46 }
47 }
48
49 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
50 {
51 Object result = null;
52 Class declaringClass = method.getDeclaringClass();
53
54 if (declaringClass == Object.class)
55 {
56 if (hashCodeMethod.equals(method))
57 {
58 result = proxyHashCode(proxy);
59 }
60 else if (equalsMethod.equals(method))
61 {
62 result = proxyEquals(proxy, args[0]);
63 }
64 else if (toStringMethod.equals(method))
65 {
66 result = proxyToString(proxy);
67 }
68 else
69 {
70 throw new InternalError("unexpected Object method dispatched: " + method);
71 }
72 }
73 else
74 {
75 throw new InternalError("unexpected Object method dispatched: " + method);
76 }
77
78 return result;
79 }
80
81 protected Integer proxyHashCode(Object proxy)
82 {
83 return new Integer(System.identityHashCode(proxy));
84 }
85
86 protected Boolean proxyEquals(Object proxy, Object other)
87 {
88 return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
89 }
90
91 protected String proxyToString(Object proxy)
92 {
93 return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode());
94 }
95
96 }