KickJava   Java API By Example, From Geeks To Geeks.

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


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: ClassSurrogate.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.Field JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 /**
34  * Class ClassSurrogate represents a <code>Class</code> object without the need
35  * that the classe's byte-code is available in the current virtual machine.
36  *
37  * @version $Revision: 1.1.1.1 $
38  * @author Philippe Schoch
39  */

40 public class ClassSurrogate extends Surrogate implements java.io.Serializable JavaDoc {
41
42   private String JavaDoc className;
43   private FieldSurrogate[] fields;
44   private MethodSurrogate[] methods;
45   private int hashCode;
46
47   /**
48    * Constructs new surrogate that represents <code>cls</code>
49    * @param cls the class this instance should represent
50    */

51   public ClassSurrogate(Class JavaDoc cls)
52     {
53       if (cls == null)
54           throw new IllegalArgumentException JavaDoc("class parameter must not be null");
55
56       hashCode = cls.hashCode();
57       className = cls.getName();
58
59       Method JavaDoc[] m = cls.getMethods();
60       methods = new MethodSurrogate[m.length];
61       for (int i=0; i < m.length; i++)
62           methods[i] = new MethodSurrogate(m[i]);
63
64       Field JavaDoc[] f = cls.getFields();
65       fields = new FieldSurrogate[f.length];
66       for (int i=0; i < f.length; i++)
67           fields[i] = new FieldSurrogate(f[i]);
68
69
70     }
71
72
73   /**
74    * Returns method surrogates for all methods from the contained class
75    *
76    * @returns the methods surrogates
77    */

78   public MethodSurrogate[] getMethodSurrogates()
79     {
80       return methods;
81     }
82
83
84   /**
85    * Returns field surrogates for all fields from the contained class
86    *
87    * @returns the fields surrogates
88    */

89   public FieldSurrogate[] getFieldSurrogates()
90     {
91       return fields;
92     }
93
94
95   /**
96    * Returns the qualified name of the class.
97    */

98   public String JavaDoc getName()
99     {
100       return className;
101     }
102
103
104   /**
105    * Tries to reconstruct original class.
106    *
107    * @returns the original class
108    * @exception ClassNotFoundException the classes byte-code cannot be loaded.
109    */

110   public Object JavaDoc toRealInstance() throws ClassNotFoundException JavaDoc
111     {
112       return Class.forName(className);
113     }
114
115
116   /**
117    * Compares this instance with the passed object.
118    *
119    * @returns <code>true</code> if the passed object is of type ClassSurrogate
120    * and has the same name, the same methods and the same fields.
121    */

122   public boolean equals(Object JavaDoc o)
123     {
124       if (!(o instanceof ClassSurrogate))
125           return false;
126
127       ClassSurrogate other = (ClassSurrogate) o;
128
129       if (!this.getName().equals(other.getName()))
130           return false;
131
132       MethodSurrogate[] mt = this.getMethodSurrogates();
133       MethodSurrogate[] mo = other.getMethodSurrogates();
134
135       if (mt.length != mo.length)
136           return false;
137
138       for (int i=0; i<mt.length; i++)
139           if (!mt[i].equals(mo[i]))
140               return false;
141
142       FieldSurrogate[] ft = this.getFieldSurrogates();
143       FieldSurrogate[] fo = other.getFieldSurrogates();
144
145       if (ft.length != fo.length)
146           return false;
147
148       for (int i=0; i < ft.length; i++)
149           if (!ft[i].equals(fo[i]))
150           return false;
151
152       return true;
153     }
154
155   public int hashCode()
156     {
157       return hashCode;
158     }
159
160
161   /**
162    * Returns the qualified class name.
163    */

164   public String JavaDoc toString()
165     {
166       return className;
167     }
168 }
169
170
171 //======================================================================
172
//
173
// $Log: ClassSurrogate.java,v $
174
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
175
// Imported from ETH Zurich
176
//
177
// Revision 1.3 2003/05/20 16:05:07 popovici
178
//
179
// New QueryManager replaces functionality in AspectManager (better Soc)
180
// New 'Surrogate' classes for usage in the QueryManager
181
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
182
//
183
// Revision 1.2 2003/05/06 15:51:49 popovici
184
// Mozilla-ification
185
//
186
// Revision 1.1 2003/05/05 13:58:23 popovici
187
// renaming from runes to prose
188
//
189
// Revision 1.4 2003/04/17 15:14:57 popovici
190
// Extension->Aspect renaming
191
//
192
// Revision 1.3 2003/03/13 14:16:55 popovici
193
// All items traveling with Tupels are now Serializable
194
//
195
// Revision 1.2 2003/03/04 18:36:02 popovici
196
// Organization of imprts
197
//
198
// Revision 1.1 2003/01/17 14:43:56 pschoch
199
// Introduction of 'query' methods in the AspectManager and its
200
// subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
201
// ExtensionSystemTest
202
//
203
Popular Tags