001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // The Initial Developer of the Original Code is Andrei Popovici. Portions
017: // created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
018: // All Rights Reserved.
019: //
020: // Contributor(s):
021: // $Id: MethodSurrogate.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
022: // =====================================================================
023: //
024: // (history at end)
025: //
026:
027: package ch.ethz.prose.query;
028:
029: // used packages
030: import java.lang.reflect.Method;
031: import java.lang.NoSuchMethodException;
032:
033: /**
034: * Class MethodSurrogate represents a <code>Method</code> object.
035: *
036: * @version $Revision: 1.1.1.1 $
037: * @author Philippe Schoch
038: */
039: public class MethodSurrogate extends Surrogate implements
040: java.io.Serializable {
041:
042: private static final long serialVersionUID = 3258417243942367536L;
043: private String declaringClassName;
044: private String methodName;
045: private String[] parameterTypes;
046: private int hashCode;
047:
048: /**
049: * Constructs a new instance representing method <code>m</code>
050: *
051: * @param m the method to represent
052: */
053: public MethodSurrogate(Method m) {
054: if (m == null)
055: throw new IllegalArgumentException(
056: "specified method must not be null");
057:
058: hashCode = m.hashCode();
059: methodName = m.getName();
060: Class[] pt = m.getParameterTypes();
061: parameterTypes = new String[pt.length];
062: for (int i = 0; i < pt.length; i++)
063: parameterTypes[i] = pt[i].getName();
064:
065: declaringClassName = m.getDeclaringClass().getName();
066: }
067:
068: /**
069: * Returns the name of the contained method
070: *
071: * @return the name of the represented method
072: */
073: public String getName() {
074: return methodName;
075: }
076:
077: /**
078: * Returns the input parameter types of the contained method.
079: *
080: * @return the parameter types of the represented method as String Array.
081: */
082: public String[] getParameterTypes() {
083: return parameterTypes;
084: }
085:
086: public Object toRealInstance() throws ClassNotFoundException,
087: NoSuchMethodException {
088: return toRealInstance(declaringClassName);
089: }
090:
091: /**
092: * Returns a <code>Method</code> Object that is represented by this
093: * <code>MethodSurrogate</code>.
094: *
095: * @exception ClassNotFoundException can be thrown if the declaring class of the method is not found.
096: * @exception NoSuchMethodException can be thrown if the name of the method doesn't represent a <code>Method</code>.
097: */
098: public Method toRealInstance(String className)
099: throws ClassNotFoundException, NoSuchMethodException {
100: Class[] parameterClasses = new Class[parameterTypes.length];
101:
102: Class declaringClass = Class.forName(className);
103: for (int i = 0; i < parameterTypes.length; i++) {
104: if (parameterTypes[i].equals("byte"))
105: parameterClasses[i] = byte.class;
106: else if (parameterTypes[i].equals("short"))
107: parameterClasses[i] = short.class;
108: else if (parameterTypes[i].equals("int"))
109: parameterClasses[i] = int.class;
110: else if (parameterTypes[i].equals("long"))
111: parameterClasses[i] = long.class;
112: else if (parameterTypes[i].equals("char"))
113: parameterClasses[i] = char.class;
114: else if (parameterTypes[i].equals("float"))
115: parameterClasses[i] = float.class;
116: else if (parameterTypes[i].equals("double"))
117: parameterClasses[i] = double.class;
118: else if (parameterTypes[i].equals("boolean"))
119: parameterClasses[i] = boolean.class;
120: else
121: parameterClasses[i] = Class.forName(parameterTypes[i]);
122: }
123: return (declaringClass.getDeclaredMethod(methodName,
124: parameterClasses));
125: }
126:
127: /**
128: * Compares this instance with the passed object. Attention, this method has a
129: * different semantic than the one in the <code>Method</code> class!!!
130: *
131: * @return <code>true</code> if the passed object is of type MethodSurrogate
132: * and has the same name and parameter types.
133: */
134: public boolean equals(Object o) {
135: if (!(o instanceof MethodSurrogate))
136: return false;
137:
138: MethodSurrogate other = (MethodSurrogate) o;
139:
140: if (!this .getName().equals(other.getName()))
141: return false;
142:
143: if (this .parameterTypes.length != other.parameterTypes.length)
144: return false;
145:
146: for (int i = 0; i < this .parameterTypes.length; i++)
147: if (!this .parameterTypes[i].equals(other.parameterTypes[i]))
148: return false;
149:
150: return true;
151: }
152:
153: public int hashCode() {
154: return hashCode;
155: }
156:
157: /**
158: * Returns a string describing this <code>FieldSurrogate</code>. The format is the method name
159: * followed by an opening paranthesis and all paramter types separated by colons and an closing paranthesis.
160: */
161: public String toString() {
162: String s = methodName + "(";
163: for (int i = 0; i < parameterTypes.length; i++) {
164: if (i == 0) {
165: s += parameterTypes[i];
166: continue;
167: }
168: s += (", " + parameterTypes[i]);
169: }
170: s += ")";
171: return s;
172: }
173: }
174:
175: //======================================================================
176: //
177: // $Log: MethodSurrogate.java,v $
178: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
179: // Imported from ETH Zurich
180: //
181: // Revision 1.3 2003/05/20 16:05:09 popovici
182: //
183: // New QueryManager replaces functionality in AspectManager (better Soc)
184: // New 'Surrogate' classes for usage in the QueryManager
185: // The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
186: //
187: // Revision 1.2 2003/05/06 15:51:51 popovici
188: // Mozilla-ification
189: //
190: // Revision 1.1 2003/05/05 13:58:21 popovici
191: // renaming from runes to prose
192: //
193: // Revision 1.5 2003/04/17 15:14:57 popovici
194: // Extension->Aspect renaming
195: //
196: // Revision 1.4 2003/03/13 14:16:56 popovici
197: // All items traveling with Tupels are now Serializable
198: //
199: // Revision 1.3 2003/03/04 18:36:02 popovici
200: // Organization of imprts
201: //
202: // Revision 1.2 2003/01/27 12:58:57 pschoch
203: // new HashCode Policy with Surrogates; toString() of Surrogates improved
204: //
205: // Revision 1.1 2003/01/17 14:43:58 pschoch
206: // Introduction of 'query' methods in the AspectManager and its
207: // subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
208: // ExtensionSystemTest
209: //
|