KickJava   Java API By Example, From Geeks To Geeks.

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


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: FieldSurrogate.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
32 /**
33  * Class FieldSurrogate represents a <code>Field</code> object.
34  *
35  * @version $Revision: 1.1.1.1 $
36  * @author Philippe Schoch
37  */

38 public
39 class FieldSurrogate extends Surrogate {
40
41     private String JavaDoc fieldName;
42     private String JavaDoc fieldType;
43     private int hashCode;
44     private String JavaDoc declaringClassName;
45
46
47   /**
48    * Constructs a new instance representing field <code>f</code>
49    *
50    * @param f the field to represent
51    */

52   public FieldSurrogate(Field JavaDoc f)
53     {
54       if (f == null)
55           throw new IllegalArgumentException JavaDoc("specified field must not be null");
56
57       hashCode = f.hashCode();
58       fieldName = f.getName();
59       fieldType = f.getType().getName();
60       declaringClassName = f.getDeclaringClass().getName();
61     }
62
63
64   /**
65    * Return the name of the contained field.
66    *
67    * @returns the name of the represented field.
68    */

69   public String JavaDoc getName()
70     {
71       return fieldName;
72     }
73
74
75   /**
76    * Return the type of the contained method
77    *
78    * @returns the type of the represented method
79    */

80     public String JavaDoc getType()
81     {
82     return fieldType;
83     }
84
85     public Object JavaDoc toRealInstance() throws ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc
86     {
87     return toRealInstance(declaringClassName);
88     }
89
90   /**
91    * Returns a <code>Field</code> Object that is represented by this
92    * <code>FieldSurrogate</code>.
93    *
94    * @exception ClassNotFoundException can be thrown if the declaring class of the field is not found.
95    * @exception NoSuchFieldException can be thrown if the name of the field doesn't represent a <code>Field</code>.
96    */

97   public Field JavaDoc toRealInstance(String JavaDoc className) throws ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc
98     {
99       Class JavaDoc c = Class.forName(className);
100       return c.getDeclaredField(fieldName);
101     }
102
103
104   /**
105    * Compares this instance with the passed object. Attention, this field has a
106    * different semantic than the one in the <code>Field</code> class!!!
107    *
108    * @returns <code>true</code> if the passed object is of type FieldSurrogate
109    * and has the same name and type.
110    */

111   public boolean equals(Object JavaDoc o)
112     {
113       if (!(o instanceof FieldSurrogate))
114           return false;
115
116       FieldSurrogate other = (FieldSurrogate) o;
117
118       if (!this.getName().equals(other.getName()))
119           return false;
120
121       if (!this.getType().equals(other.getType()))
122           return false;
123
124       return true;
125     }
126
127   public int hashCode()
128     {
129       return hashCode;
130     }
131
132   /**
133    * Returns a string describing this Field. The format is the field type followed by the field name.
134    */

135   public String JavaDoc toString()
136     {
137       return fieldType + " " + fieldName;
138     }
139 }
140
141
142 //======================================================================
143
//
144
// $Log: FieldSurrogate.java,v $
145
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
146
// Imported from ETH Zurich
147
//
148
// Revision 1.3 2003/05/20 16:05:08 popovici
149
//
150
// New QueryManager replaces functionality in AspectManager (better Soc)
151
// New 'Surrogate' classes for usage in the QueryManager
152
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
153
//
154
// Revision 1.2 2003/05/06 15:51:50 popovici
155
// Mozilla-ification
156
//
157
// Revision 1.1 2003/05/05 13:58:22 popovici
158
// renaming from runes to prose
159
//
160
// Revision 1.3 2003/04/17 15:14:57 popovici
161
// Extension->Aspect renaming
162
//
163
// Revision 1.2 2003/03/04 18:36:02 popovici
164
// Organization of imprts
165
//
166
// Revision 1.1 2003/01/17 14:43:57 pschoch
167
// Introduction of 'query' methods in the AspectManager and its
168
// subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
169
// ExtensionSystemTest
170
//
171
Popular Tags