KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > query > MethodSurrogate


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
// $Id: MethodSurrogate.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.query;
28
29 // used packages
30
import java.lang.reflect.Method JavaDoc;
31 import java.lang.NoSuchMethodException JavaDoc;
32
33 /**
34  * Class MethodSurrogate represents a <code>Method</code> object.
35  *
36  * @version $Revision: 1.1.1.1 $
37  * @author Philippe Schoch
38  */

39 public
40 class MethodSurrogate extends Surrogate implements java.io.Serializable JavaDoc {
41
42     private String JavaDoc declaringClassName;
43     private String JavaDoc methodName;
44     private String JavaDoc[] parameterTypes;
45     private int hashCode;
46
47   /**
48    * Constructs a new instance representing method <code>m</code>
49    *
50    * @param m the method to represent
51    */

52   public MethodSurrogate(Method JavaDoc m)
53     {
54       if (m == null)
55           throw new IllegalArgumentException JavaDoc("specified method must not be null");
56
57       hashCode = m.hashCode();
58       methodName = m.getName();
59       Class JavaDoc[] pt = m.getParameterTypes();
60       parameterTypes = new String JavaDoc[pt.length];
61       for (int i=0; i<pt.length; i++)
62           parameterTypes[i] = pt[i].getName();
63
64       declaringClassName = m.getDeclaringClass().getName();
65     }
66
67
68   /**
69    * Returns the name of the contained method
70    *
71    * @returns the name of the represented method
72    */

73   public String JavaDoc getName()
74     {
75       return methodName;
76     }
77
78
79   /**
80    * Returns the input parameter types of the contained method.
81    *
82    * @returns the parameter types of the represented method as String Array.
83    */

84   public String JavaDoc[] getParameterTypes()
85     {
86       return parameterTypes;
87     }
88
89
90     public Object JavaDoc toRealInstance() throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
91     {
92     return toRealInstance(declaringClassName);
93     }
94
95   /**
96    * Returns a <code>Method</code> Object that is represented by this
97    * <code>MethodSurrogate</code>.
98    *
99    * @exception ClassNotFoundException can be thrown if the declaring class of the method is not found.
100    * @exception NoSuchMethodException can be thrown if the name of the method doesn't represent a <code>Method</code>.
101    */

102   public Method JavaDoc toRealInstance(String JavaDoc className) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
103     {
104       Class JavaDoc[] parameterClasses = new Class JavaDoc[parameterTypes.length];
105
106       Class JavaDoc declaringClass = Class.forName(className);
107       for (int i=0; i < parameterTypes.length; i++)
108         {
109       if (parameterTypes[i].equals("byte"))
110           parameterClasses[i] = byte.class;
111       else if (parameterTypes[i].equals("short"))
112           parameterClasses[i] = short.class;
113       else if (parameterTypes[i].equals("int"))
114           parameterClasses[i] = int.class;
115       else if (parameterTypes[i].equals("long"))
116           parameterClasses[i] = long.class;
117       else if (parameterTypes[i].equals("char"))
118           parameterClasses[i] = char.class;
119       else if (parameterTypes[i].equals("float"))
120           parameterClasses[i] = float.class;
121       else if (parameterTypes[i].equals("double"))
122           parameterClasses[i] = double.class;
123       else if (parameterTypes[i].equals("boolean"))
124           parameterClasses[i] = boolean.class;
125       else
126           parameterClasses[i] = Class.forName(parameterTypes[i]);
127     }
128       return (declaringClass.getDeclaredMethod(methodName, parameterClasses));
129     }
130
131
132   /**
133    * Compares this instance with the passed object. Attention, this method has a
134    * different semantic than the one in the <code>Method</code> class!!!
135    *
136    * @returns <code>true</code> if the passed object is of type MethodSurrogate
137    * and has the same name and parameter types.
138    */

139   public boolean equals(Object JavaDoc o)
140     {
141       if (!(o instanceof MethodSurrogate))
142           return false;
143
144       MethodSurrogate other = (MethodSurrogate) o;
145
146       if (!this.getName().equals(other.getName()))
147           return false;
148
149       if (this.parameterTypes.length != other.parameterTypes.length)
150         return false;
151
152       for (int i=0; i<this.parameterTypes.length; i++)
153         if (!this.parameterTypes[i].equals(other.parameterTypes[i]))
154         return false;
155
156       return true;
157     }
158
159   public int hashCode()
160     {
161       return hashCode;
162     }
163
164   /**
165    * Returns a string describing this <code>FieldSurrogate</code>. The format is the method name
166    * followed by an opening paranthesis and all paramter types separated by colons and an closing paranthesis.
167    */

168   public String JavaDoc toString()
169     {
170       String JavaDoc s = methodName + "(";
171       for (int i=0; i < parameterTypes.length; i++)
172         {
173           if (i==0)
174         {
175           s += parameterTypes[i];
176           continue;
177         }
178           s += (", " + parameterTypes[i]);
179     }
180       s += ")";
181       return s;
182     }
183 }
184
185 //======================================================================
186
//
187
// $Log: MethodSurrogate.java,v $
188
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
189
// Imported from ETH Zurich
190
//
191
// Revision 1.3 2003/05/20 16:05:09 popovici
192
//
193
// New QueryManager replaces functionality in AspectManager (better Soc)
194
// New 'Surrogate' classes for usage in the QueryManager
195
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
196
//
197
// Revision 1.2 2003/05/06 15:51:51 popovici
198
// Mozilla-ification
199
//
200
// Revision 1.1 2003/05/05 13:58:21 popovici
201
// renaming from runes to prose
202
//
203
// Revision 1.5 2003/04/17 15:14:57 popovici
204
// Extension->Aspect renaming
205
//
206
// Revision 1.4 2003/03/13 14:16:56 popovici
207
// All items traveling with Tupels are now Serializable
208
//
209
// Revision 1.3 2003/03/04 18:36:02 popovici
210
// Organization of imprts
211
//
212
// Revision 1.2 2003/01/27 12:58:57 pschoch
213
// new HashCode Policy with Surrogates; toString() of Surrogates improved
214
//
215
// Revision 1.1 2003/01/17 14:43:58 pschoch
216
// Introduction of 'query' methods in the AspectManager and its
217
// subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
218
// ExtensionSystemTest
219
//
220
Popular Tags