KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > reflection > GenericProxy


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.reflection;
19
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23
24 /**
25  * <p>This generic implementation of a <code>DynamicProxy</code>
26  * invocation handler will provide the basics of object method handling
27  * for the methods hashCode, equals and toString. Any other method calls
28  * will be forwarded to subclasses for implementation.
29  * </p>
30  *
31  * <P>Invocation handlers wishing to provide specialized implementations of
32  * the follow methods: { hashCode, toString, equals} should override those
33  * versions of the methods defined in this class that start with the
34  * key <code>proxy</code>.
35  *
36  * Copyright 2002 Sapient
37  * @author Greg Hinkle, December 2001
38  * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/05/05 21:21:23 $)
39  */

40 public abstract class GenericProxy implements InvocationHandler JavaDoc {
41
42     /** Preloaded Method object for the hashCode method of java.lang.Object */
43     private static Method JavaDoc hashCodeMethod;
44
45     /** Preloaded Method object for the equals method of java.lang.Object */
46     private static Method JavaDoc equalsMethod;
47
48     /** Preloaded Method object for the toString method of java.lang.Object */
49     private static Method JavaDoc toStringMethod;
50
51
52     // Statically load the Method objects for the basic java.lang.Object
53
// methods.
54
static {
55         try {
56             hashCodeMethod = Object JavaDoc.class.getMethod("hashCode", null);
57             equalsMethod =
58             Object JavaDoc.class.getMethod("equals", new Class JavaDoc[] { Object JavaDoc.class });
59             toStringMethod = Object JavaDoc.class.getMethod("toString", null);
60         } catch (NoSuchMethodException JavaDoc e) {
61             throw new NoSuchMethodError JavaDoc(e.getMessage());
62         }
63     }
64
65
66
67
68     /**
69      * This method is called through by the JVM generated
70      * DynamicProxy for all calls to the represented object.
71      * This method then manages delegating those calls the
72      * appropriate implementation. This implementation
73      * specially handles the default <code>Object</code>
74      * class methods and forwards all other method calls to
75      * the abstract <code>handleInvoke</code> method.
76      * @param proxy the object that is being represented
77      * @param m the method descriptor for the method called
78      * @param args an array of arguments passed to that method
79      * @throws java.lang.Throwable when there is an exception thrown from the
80      * delegated method. This may be a Checked
81      * exception if the implemented interface declares
82      * the exception. Otherwise checked exceptions will
83      * be automatically wrapped in an
84      * <code>UndeclaredThrowableException</code>.
85      * Runtime exceptions are thrown as is.
86      * @return the return value of the delegated method.
87      */

88     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args)
89     throws Throwable JavaDoc {
90         Class JavaDoc declaringClass = m.getDeclaringClass();
91
92         if (declaringClass == Object JavaDoc.class) {
93             if (m.equals(hashCodeMethod)) {
94             return proxyHashCode(proxy);
95             } else if (m.equals(equalsMethod)) {
96             return proxyEquals(proxy, args[0]);
97             } else if (m.equals(toStringMethod)) {
98             return proxyToString(proxy);
99             } else {
100             throw new InternalError JavaDoc(
101                 "unexpected Object method dispatched: " + m);
102             }
103         } else {
104             return handleInvoke(proxy, m, args);
105         }
106     }
107
108     /**
109      * This abstract method should be implemented to handle the specific
110      * functionality for an invocation handler.
111      *
112      * @param proxy the object that is being represented
113      * @param m the method descriptor for the method called
114      * @param args an array of arguments passed to that method
115      * @throws java.lang.Throwable when there is an exception thrown from the
116      * delegated method. This may be a Checked
117      * exception if the implemented interface declares
118      * the exception. Otherwise checked exceptions will
119      * be automatically wrapped in an
120      * <code>UndeclaredThrowableException</code>.
121      * Runtime exceptions are thrown as is.
122      * @return the return value of the delegated method.
123      */

124     protected abstract Object JavaDoc handleInvoke(
125     Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args)
126     throws Throwable JavaDoc;
127
128     /**
129      * Implements the standard hashCode method with a
130      * simple call to System.identityHashCode().
131      * @param proxy the object for which a hashcode should
132      * be returned
133      * @return the hash code of the provided object
134      */

135     protected Integer JavaDoc proxyHashCode(Object JavaDoc proxy) {
136         return new Integer JavaDoc(System.identityHashCode(proxy));
137     }
138
139     /**
140      * This method implements the standard proxyEquals
141      * method for subclassed invocation handler
142      * dynamic proxies.
143      * @param proxy the proxy object for which equals is
144      * being handled
145      * @param other the other object being compared too
146      * @return true if they are symantically equal,
147      * false otherwise
148      */

149     protected Boolean JavaDoc proxyEquals(Object JavaDoc proxy, Object JavaDoc other) {
150         return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
151     }
152
153     /**
154      * Prints out a string representation of the proxy object in the
155      * standard Java Object toString format of
156      * &lt;classname&gt;@&lt;hashcode&gt;
157      *
158      * @param proxy the proxy object to toString
159      * @return the String representation of the proxy object
160      */

161     protected String JavaDoc proxyToString(Object JavaDoc proxy) {
162         return proxy.getClass().getName() + '@' +
163             Integer.toHexString(proxy.hashCode());
164     }
165 }
Popular Tags