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: FieldSurrogate.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:
032: /**
033: * Class FieldSurrogate represents a <code>Field</code> object.
034: *
035: * @version $Revision: 1.1.1.1 $
036: * @author Philippe Schoch
037: */
038: public class FieldSurrogate extends Surrogate {
039:
040: private static final long serialVersionUID = 4051045280784790578L;
041: private String fieldName;
042: private String fieldType;
043: private int hashCode;
044: private String declaringClassName;
045:
046: /**
047: * Constructs a new instance representing field <code>f</code>
048: *
049: * @param f the field to represent
050: */
051: public FieldSurrogate(Field f) {
052: if (f == null)
053: throw new IllegalArgumentException(
054: "specified field must not be null");
055:
056: hashCode = f.hashCode();
057: fieldName = f.getName();
058: fieldType = f.getType().getName();
059: declaringClassName = f.getDeclaringClass().getName();
060: }
061:
062: /**
063: * Return the name of the contained field.
064: *
065: * @return the name of the represented field.
066: */
067: public String getName() {
068: return fieldName;
069: }
070:
071: /**
072: * Return the type of the contained method
073: *
074: * @return the type of the represented method
075: */
076: public String getType() {
077: return fieldType;
078: }
079:
080: public Object toRealInstance() throws ClassNotFoundException,
081: NoSuchFieldException {
082: return toRealInstance(declaringClassName);
083: }
084:
085: /**
086: * Returns a <code>Field</code> Object that is represented by this
087: * <code>FieldSurrogate</code>.
088: *
089: * @exception ClassNotFoundException can be thrown if the declaring class of the field is not found.
090: * @exception NoSuchFieldException can be thrown if the name of the field doesn't represent a <code>Field</code>.
091: */
092: public Field toRealInstance(String className)
093: throws ClassNotFoundException, NoSuchFieldException {
094: Class c = Class.forName(className);
095: return c.getDeclaredField(fieldName);
096: }
097:
098: /**
099: * Compares this instance with the passed object. Attention, this field has a
100: * different semantic than the one in the <code>Field</code> class!!!
101: *
102: * @return <code>true</code> if the passed object is of type FieldSurrogate
103: * and has the same name and type.
104: */
105: public boolean equals(Object o) {
106: if (!(o instanceof FieldSurrogate))
107: return false;
108:
109: FieldSurrogate other = (FieldSurrogate) o;
110:
111: if (!this .getName().equals(other.getName()))
112: return false;
113:
114: if (!this .getType().equals(other.getType()))
115: return false;
116:
117: return true;
118: }
119:
120: public int hashCode() {
121: return hashCode;
122: }
123:
124: /**
125: * Returns a string describing this Field. The format is the field type followed by the field name.
126: */
127: public String toString() {
128: return fieldType + " " + fieldName;
129: }
130: }
131:
132: //======================================================================
133: //
134: // $Log: FieldSurrogate.java,v $
135: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
136: // Imported from ETH Zurich
137: //
138: // Revision 1.3 2003/05/20 16:05:08 popovici
139: //
140: // New QueryManager replaces functionality in AspectManager (better Soc)
141: // New 'Surrogate' classes for usage in the QueryManager
142: // The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
143: //
144: // Revision 1.2 2003/05/06 15:51:50 popovici
145: // Mozilla-ification
146: //
147: // Revision 1.1 2003/05/05 13:58:22 popovici
148: // renaming from runes to prose
149: //
150: // Revision 1.3 2003/04/17 15:14:57 popovici
151: // Extension->Aspect renaming
152: //
153: // Revision 1.2 2003/03/04 18:36:02 popovici
154: // Organization of imprts
155: //
156: // Revision 1.1 2003/01/17 14:43:57 pschoch
157: // Introduction of 'query' methods in the AspectManager and its
158: // subclasses. The result set is given back in form of surrogates; 4 new tests added to ExtensionManagerTest
159: // ExtensionSystemTest
160: //
|