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: ClassSurrogate.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.Field;
031: import java.lang.reflect.Method;
032:
033: /**
034: * Class ClassSurrogate represents a <code>Class</code> object without the need
035: * that the classe's byte-code is available in the current virtual machine.
036: *
037: * @version $Revision: 1.1.1.1 $
038: * @author Philippe Schoch
039: */
040: public class ClassSurrogate extends Surrogate implements
041: java.io.Serializable {
042:
043: private static final long serialVersionUID = 3760850064503222320L;
044: private String className;
045: private FieldSurrogate[] fields;
046: private MethodSurrogate[] methods;
047: private int hashCode;
048:
049: /**
050: * Constructs new surrogate that represents <code>cls</code>
051: * @param cls the class this instance should represent
052: */
053: public ClassSurrogate(Class cls) {
054: if (cls == null)
055: throw new IllegalArgumentException(
056: "class parameter must not be null");
057:
058: hashCode = cls.hashCode();
059: className = cls.getName();
060:
061: Method[] m = cls.getMethods();
062: methods = new MethodSurrogate[m.length];
063: for (int i = 0; i < m.length; i++)
064: methods[i] = new MethodSurrogate(m[i]);
065:
066: Field[] f = cls.getFields();
067: fields = new FieldSurrogate[f.length];
068: for (int i = 0; i < f.length; i++)
069: fields[i] = new FieldSurrogate(f[i]);
070:
071: }
072:
073: /**
074: * Returns method surrogates for all methods from the contained class
075: *
076: * @return the methods surrogates
077: */
078: public MethodSurrogate[] getMethodSurrogates() {
079: return methods;
080: }
081:
082: /**
083: * Returns field surrogates for all fields from the contained class
084: *
085: * @return the fields surrogates
086: */
087: public FieldSurrogate[] getFieldSurrogates() {
088: return fields;
089: }
090:
091: /**
092: * Returns the qualified name of the class.
093: */
094: public String getName() {
095: return className;
096: }
097:
098: /**
099: * Tries to reconstruct original class.
100: *
101: * @return the original class
102: * @exception ClassNotFoundException the classes byte-code cannot be loaded.
103: */
104: public Object toRealInstance() throws ClassNotFoundException {
105: return Class.forName(className);
106: }
107:
108: /**
109: * Compares this instance with the passed object.
110: *
111: * @return <code>true</code> if the passed object is of type ClassSurrogate
112: * and has the same name, the same methods and the same fields.
113: */
114: public boolean equals(Object o) {
115: if (!(o instanceof ClassSurrogate))
116: return false;
117:
118: ClassSurrogate other = (ClassSurrogate) o;
119:
120: if (!this .getName().equals(other.getName()))
121: return false;
122:
123: MethodSurrogate[] mt = this .getMethodSurrogates();
124: MethodSurrogate[] mo = other.getMethodSurrogates();
125:
126: if (mt.length != mo.length)
127: return false;
128:
129: for (int i = 0; i < mt.length; i++)
130: if (!mt[i].equals(mo[i]))
131: return false;
132:
133: FieldSurrogate[] ft = this .getFieldSurrogates();
134: FieldSurrogate[] fo = other.getFieldSurrogates();
135:
136: if (ft.length != fo.length)
137: return false;
138:
139: for (int i = 0; i < ft.length; i++)
140: if (!ft[i].equals(fo[i]))
141: return false;
142:
143: return true;
144: }
145:
146: public int hashCode() {
147: return hashCode;
148: }
149:
150: /**
151: * Returns the qualified class name.
152: */
153: public String toString() {
154: return className;
155: }
156: }
157:
158: //======================================================================
159: //
160: // $Log: ClassSurrogate.java,v $
161: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
162: // Imported from ETH Zurich
163: //
164: // Revision 1.3 2003/05/20 16:05:07 popovici
165: //
166: // New QueryManager replaces functionality in AspectManager (better Soc)
167: // New 'Surrogate' classes for usage in the QueryManager
168: // The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
169: //
170: // Revision 1.2 2003/05/06 15:51:49 popovici
171: // Mozilla-ification
172: //
173: // Revision 1.1 2003/05/05 13:58:23 popovici
174: // renaming from runes to prose
175: //
176: // Revision 1.4 2003/04/17 15:14:57 popovici
177: // Extension->Aspect renaming
178: //
179: // Revision 1.3 2003/03/13 14:16:55 popovici
180: // All items traveling with Tupels are now Serializable
181: //
182: // Revision 1.2 2003/03/04 18:36:02 popovici
183: // Organization of imprts
184: //
185: // Revision 1.1 2003/01/17 14:43:56 pschoch
186: // Introduction of 'query' methods in the AspectManager and its
187: // subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
188: // ExtensionSystemTest
189: //
|