View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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  
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  }