KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JavaField


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.bytecode;
30
31 import com.caucho.log.Log;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.lang.reflect.Modifier JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Represents a java field.
42  */

43 public class JavaField extends JField {
44   static private final Logger JavaDoc log = Log.open(JavaField.class);
45
46   private JavaClass _jClass;
47   private int _accessFlags;
48   private String JavaDoc _name;
49   private String JavaDoc _descriptor;
50
51   private ArrayList JavaDoc<Attribute> _attributes = new ArrayList JavaDoc<Attribute>();
52
53   private JavaAnnotation []_annotations;
54
55   /**
56    * Sets the JavaClass.
57    */

58   public void setJavaClass(JavaClass jClass)
59   {
60     _jClass = jClass;
61   }
62
63   /**
64    * Returns the declaring class.
65    */

66   public JClass getDeclaringClass()
67   {
68     return _jClass;
69   }
70
71   /**
72    * Returns the class loader.
73    */

74   public JavaClassLoader getClassLoader()
75   {
76     return _jClass.getClassLoader();
77   }
78
79   /**
80    * Sets the name.
81    */

82   public void setName(String JavaDoc name)
83   {
84     _name = name;
85   }
86
87   /**
88    * Gets the name.
89    */

90   public String JavaDoc getName()
91   {
92     return _name;
93   }
94
95   /**
96    * Sets the access flags
97    */

98   public void setAccessFlags(int flags)
99   {
100     _accessFlags = flags;
101   }
102
103   /**
104    * Gets the access flags
105    */

106   public int getAccessFlags()
107   {
108     return _accessFlags;
109   }
110
111   /**
112    * Sets the descriptor.
113    */

114   public void setDescriptor(String JavaDoc descriptor)
115   {
116     _descriptor = descriptor;
117
118     if (_jClass != null)
119       _jClass.getConstantPool().addUTF8(descriptor);
120   }
121
122   /**
123    * Gets the descriptor.
124    */

125   public String JavaDoc getDescriptor()
126   {
127     return _descriptor;
128   }
129
130   /**
131    * Gets the typename.
132    */

133   public JClass getType()
134   {
135     return getClassLoader().descriptorToClass(getDescriptor(), 0);
136   }
137
138   /**
139    * Returns true for a static field.
140    */

141   public boolean isStatic()
142   {
143     return Modifier.isStatic(getAccessFlags());
144   }
145
146   /**
147    * Returns true for a private field.
148    */

149   public boolean isPrivate()
150   {
151     return Modifier.isPrivate(getAccessFlags());
152   }
153
154   /**
155    * Returns true for a transient field.
156    */

157   public boolean isTransient()
158   {
159     return Modifier.isTransient(getAccessFlags());
160   }
161
162   /**
163    * Gets the typename.
164    */

165   public JType getGenericType()
166   {
167     SignatureAttribute sigAttr = (SignatureAttribute) getAttribute("Signature");
168
169     if (sigAttr != null) {
170       return getClassLoader().parseParameterizedType(sigAttr.getSignature());
171     }
172
173     return getType();
174   }
175
176   /**
177    * Adds an attribute.
178    */

179   public void addAttribute(Attribute attr)
180   {
181     _attributes.add(attr);
182   }
183
184   /**
185    * Returns the attribute.
186    */

187   public Attribute getAttribute(String JavaDoc name)
188   {
189     for (int i = _attributes.size() - 1; i >= 0; i--) {
190       Attribute attr = _attributes.get(i);
191
192       if (attr.getName().equals(name))
193   return attr;
194     }
195
196     return null;
197   }
198
199   /**
200    * Returns the declared annotations.
201    */

202   public JAnnotation []getDeclaredAnnotations()
203   {
204     if (_annotations == null) {
205       Attribute attr = getAttribute("RuntimeVisibleAnnotations");
206
207       if (attr instanceof OpaqueAttribute) {
208   byte []buffer = ((OpaqueAttribute) attr).getValue();
209
210   try {
211     ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(buffer);
212
213     ConstantPool cp = _jClass.getConstantPool();
214
215     _annotations = JavaAnnotation.parseAnnotations(is, cp,
216                getClassLoader());
217   } catch (IOException JavaDoc e) {
218     log.log(Level.FINER, e.toString(), e);
219   }
220       }
221
222       if (_annotations == null) {
223   _annotations = new JavaAnnotation[0];
224       }
225     }
226
227     return _annotations;
228   }
229
230   /**
231    * Writes the field to the output.
232    */

233   public void write(ByteCodeWriter out)
234     throws IOException JavaDoc
235   {
236     out.writeShort(_accessFlags);
237     out.writeUTF8Const(_name);
238     out.writeUTF8Const(_descriptor);
239     out.writeShort(_attributes.size());
240
241     for (int i = 0; i < _attributes.size(); i++) {
242       Attribute attr = _attributes.get(i);
243
244       attr.write(out);
245     }
246   }
247
248   /**
249    * exports the field
250    */

251   public JavaField export(JavaClass cl, JavaClass target)
252   {
253     JavaField field = new JavaField();
254     field.setName(_name);
255     field.setDescriptor(_descriptor);
256     field.setAccessFlags(_accessFlags);
257
258     target.getConstantPool().addUTF8(_name);
259     target.getConstantPool().addUTF8(_descriptor);
260
261     for (int i = 0; i < _attributes.size(); i++) {
262       Attribute attr = _attributes.get(i);
263
264       field.addAttribute(attr.export(cl, target));
265     }
266
267     return field;
268   }
269
270   public boolean equals(Object JavaDoc o)
271   {
272     if (o == null || ! JavaField.class.equals(o.getClass()))
273       return false;
274
275     JavaField field = (JavaField) o;
276
277     return _name.equals(field._name);
278   }
279
280   public String JavaDoc toString()
281   {
282     return "JavaField[" + _name + "]";
283   }
284 }
285
Popular Tags