01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
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.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: protected static Method hashCodeMethod;
31: protected static Method equalsMethod;
32: protected static Method toStringMethod;
33:
34: static {
35: try {
36: hashCodeMethod = Object.class.getMethod("hashCode", null);
37: equalsMethod = Object.class.getMethod("equals",
38: new Class[] { Object.class });
39: toStringMethod = Object.class.getMethod("toString", null);
40: } catch (NoSuchMethodException e) {
41: throw new NoSuchMethodError(e.getMessage());
42: }
43: }
44:
45: public Object invoke(Object proxy, Method method, Object[] args)
46: throws Throwable {
47: Object result = null;
48: Class declaringClass = method.getDeclaringClass();
49:
50: if (declaringClass == Object.class) {
51: if (hashCodeMethod.equals(method)) {
52: result = proxyHashCode(proxy);
53: } else if (equalsMethod.equals(method)) {
54: result = proxyEquals(proxy, args[0]);
55: } else if (toStringMethod.equals(method)) {
56: result = proxyToString(proxy);
57: } else {
58: throw new InternalError(
59: "unexpected Object method dispatched: "
60: + method);
61: }
62: } else {
63: throw new InternalError(
64: "unexpected Object method dispatched: " + method);
65: }
66:
67: return result;
68: }
69:
70: protected Integer proxyHashCode(Object proxy) {
71: return new Integer(System.identityHashCode(proxy));
72: }
73:
74: protected Boolean proxyEquals(Object proxy, Object other) {
75: return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
76: }
77:
78: protected String proxyToString(Object proxy) {
79: return proxy.getClass().getName() + '@'
80: + Integer.toHexString(proxy.hashCode());
81: }
82:
83: }
|