Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseInvocationHandler |
|
| 3.25;3.25 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.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.core.util.reflect; | |
18 | ||
19 | import java.lang.reflect.InvocationHandler; | |
20 | import java.lang.reflect.InvocationTargetException; | |
21 | import java.lang.reflect.Method; | |
22 | ||
23 | /** | |
24 | * An abstract base class for InvocationHanlders which can be used to implement | |
25 | * an InvocationHandler that delegates hashCode and equals methods to the proxied | |
26 | * | |
27 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
28 | */ | |
29 | 0 | public abstract class BaseInvocationHandler implements InvocationHandler { |
30 | ||
31 | // preloaded Method objects for the methods in java.lang.Object | |
32 | private static Method hashCodeMethod; | |
33 | private static Method equalsMethod; | |
34 | static { | |
35 | try { | |
36 | 0 | hashCodeMethod = Object.class.getMethod("hashCode", (Class[])null); |
37 | 0 | equalsMethod = Object.class.getMethod("equals", new Class[] { Object.class }); |
38 | 0 | } catch (NoSuchMethodException e) { |
39 | // this should never happen | |
40 | 0 | throw new NoSuchMethodError(e.getMessage()); |
41 | 0 | } |
42 | 0 | } |
43 | ||
44 | public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { | |
45 | 0 | Class declaringClass = method.getDeclaringClass(); |
46 | 0 | if (declaringClass == Object.class) { |
47 | 0 | if (method.equals(hashCodeMethod)) { |
48 | 0 | return proxyHashCode(proxy); |
49 | 0 | } else if (method.equals(equalsMethod)) { |
50 | 0 | return proxyEquals(proxy, arguments[0]); |
51 | } /*else if (m.equals(toStringMethod)) { | |
52 | return proxyToString(proxy); | |
53 | } else { | |
54 | throw new InternalError( | |
55 | "unexpected Object method dispatched: " + m); | |
56 | }*/ | |
57 | } | |
58 | try { | |
59 | 0 | return invokeInternal(proxy, method, arguments); |
60 | 0 | } catch (InvocationTargetException e) { |
61 | 0 | throw e.getCause(); |
62 | } | |
63 | } | |
64 | ||
65 | protected abstract Object invokeInternal(Object proxy, Method method, Object[] arguments) throws Throwable; | |
66 | ||
67 | protected Integer proxyHashCode(Object proxy) { | |
68 | 0 | return new Integer(System.identityHashCode(proxy)); |
69 | } | |
70 | ||
71 | protected Boolean proxyEquals(Object proxy, Object other) { | |
72 | 0 | return (proxy == other ? Boolean.TRUE : Boolean.FALSE); |
73 | } | |
74 | ||
75 | } |