KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > JiapiField


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import alt.jiapi.file.ClassFile;
26 import alt.jiapi.file.ConstantPool;
27 import alt.jiapi.file.Field;
28 import alt.jiapi.file.Attribute;
29
30 /**
31  * This class represents a Field.
32  *
33  * @author Mika Riekkinen
34  * @author Joni Suominen
35  * @version $Revision: 1.12 $ $Date: 2004/03/28 14:11:20 $
36  */

37 public class JiapiField {
38     private JiapiClass declaringClass;
39     private Field field;
40
41     JiapiField(Field f) {
42         this.field = f;
43     }
44
45
46     /**
47      * Get the name of this field.
48      *
49      * @return Name of this field
50      */

51     public String JavaDoc getName() {
52         return field.getName();
53     }
54
55     /**
56      * Get the type of this field. This involves loading of the the type
57      * through the same loader that was used to load the declaring class.
58      *
59      * @return a JiapiClass which represents a type of this field
60      * @exception ClassNotFoundException is thrown, if JiapiClass could not
61      * be loaded
62      * @see #getTypeName()
63      */

64     public JiapiClass getType() throws ClassNotFoundException JavaDoc {
65         Loader loader = getDeclaringClass().getLoader();
66         JiapiClass clazz = null;
67         try {
68             clazz = loader.loadClass(getTypeName());
69         }
70         catch(java.io.IOException JavaDoc ioe) {
71             throw new ClassNotFoundException JavaDoc(getTypeName());
72         }
73
74         return clazz;
75     }
76
77     /**
78      * Get the type of this field.
79      *
80      * @return a String which represents a type of this field
81      */

82     public String JavaDoc getTypeName() {
83         String JavaDoc descriptor = getDescriptor();
84         String JavaDoc type = TypeHelper.descriptorToType(descriptor);
85
86         return type;
87     }
88
89     /**
90      * Get the field descriptor.
91      */

92     String JavaDoc getDescriptor() {
93         return field.getDescriptor();
94     }
95
96     /**
97      * Get the modifiers of this field.
98      * @see java.lang.reflect.Modifier
99      */

100     public int getModifiers() {
101         return field.getAccessFlags();
102     }
103
104     /**
105      * Sets the modifiers of this field.
106      *
107      * @param modifiers new modifiers
108      * @see java.lang.reflect.Modifier
109      */

110     void setModifiers(int modifiers) {
111 // // NOTE: We Should update constantpool here
112
// this.modifiers = modifiers;
113
throw new RuntimeException JavaDoc("Operation not supported");
114     }
115
116
117     Field getField() {
118         return field;
119     }
120
121
122     /**
123      * Gets the JiapiClass, that declares this JiapiField.
124      *
125      * @return Declaring class, or null if one has not been assigned to
126      * this JiapiField
127      */

128     public JiapiClass getDeclaringClass() {
129         return declaringClass;
130     }
131
132
133     /**
134      */

135     public String JavaDoc toString() {
136         StringBuffer JavaDoc sb =
137             new StringBuffer JavaDoc(Modifier.toString(getModifiers()));
138         sb.append(' ');
139         sb.append(getTypeName());
140         sb.append(' ');
141         sb.append(getName());
142
143         return sb.toString();
144     }
145
146
147     /**
148      * Sets the declaring class.
149      *
150      * @param declaringClass A JiapiClass, that declares this JiapiField
151      */

152     void setDeclaringClass(JiapiClass declaringClass) {
153         this.declaringClass = declaringClass;
154     }
155
156
157
158     /**
159      * Gets all the field attributes defined in ClassFile.
160      */

161     private List JavaDoc getAttributes() {
162         return field.getAttributes();
163     }
164
165
166     /**
167      * Gets all the method attributes defined in ClassFile.
168      */

169     private Attribute getAttribute(String JavaDoc name) {
170         List JavaDoc l = getAttributes();
171         Iterator JavaDoc i = l.iterator();
172 // ConstantPool cp = declaringClass.getConstantPool();
173

174         while(i.hasNext()) {
175             Attribute a = (Attribute)i.next();
176
177             if (a.getName().equals(name)) {
178                 return a;
179             }
180         }
181
182         return null;
183     }
184 }
185
Popular Tags