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