01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.kuali.rice.proxy;
18:
19: import java.lang.reflect.InvocationHandler;
20: import java.lang.reflect.Method;
21:
22: import org.kuali.rice.util.ExceptionUtils;
23:
24: /**
25: * An abstract base class for InvocationHanlders which can be used to implement
26: * an InvocationHandler that delegates hashCode and equals methods to the proxied
27: *
28: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
29: */
30: public abstract class BaseInvocationHandler implements
31: InvocationHandler {
32:
33: // preloaded Method objects for the methods in java.lang.Object
34: private static Method hashCodeMethod;
35: private static Method equalsMethod;
36: static {
37: try {
38: hashCodeMethod = Object.class.getMethod("hashCode",
39: (Class[]) null);
40: equalsMethod = Object.class.getMethod("equals",
41: new Class[] { Object.class });
42: } catch (NoSuchMethodException e) {
43: throw new NoSuchMethodError(e.getMessage());
44: }
45: }
46:
47: public Object invoke(Object proxy, Method method, Object[] arguments)
48: throws Throwable {
49: Class declaringClass = method.getDeclaringClass();
50: if (declaringClass == Object.class) {
51: if (method.equals(hashCodeMethod)) {
52: return proxyHashCode(proxy);
53: } else if (method.equals(equalsMethod)) {
54: return proxyEquals(proxy, arguments[0]);
55: } /*else if (m.equals(toStringMethod)) {
56: return proxyToString(proxy);
57: } else {
58: throw new InternalError(
59: "unexpected Object method dispatched: " + m);
60: }*/
61: }
62: try {
63: return invokeInternal(proxy, method, arguments);
64: } catch (Throwable t) {
65: throw ExceptionUtils.unwrapActualCause(t);
66: }
67: }
68:
69: protected abstract Object invokeInternal(Object proxy,
70: Method method, Object[] arguments) throws Throwable;
71:
72: protected Integer proxyHashCode(Object proxy) {
73: return new Integer(System.identityHashCode(proxy));
74: }
75:
76: protected Boolean proxyEquals(Object proxy, Object other) {
77: return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
78: }
79:
80: }
|