KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > Field


1 /*
2  * Field.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.15 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31 /**
32  * Base class for variables and methods.
33  *
34  * @author Thomas Ball
35  */

36 public abstract class Field {
37
38     private int iName;
39     private int iType;
40     private String JavaDoc _name;
41     private String JavaDoc _type;
42
43     int access;
44     ClassFile classFile;
45     Map<ClassName,Annotation> annotations;
46     String JavaDoc typeSignature;
47     AttributeMap attributes;
48
49     /** Creates new Field */
50     Field(DataInputStream in, ConstantPool pool, ClassFile classFile,
51       boolean includeCode) throws IOException {
52         access = in.readUnsignedShort();
53     iName = in.readUnsignedShort();
54     iType = in.readUnsignedShort();
55     this.classFile = classFile;
56         attributes = AttributeMap.load(in, pool, includeCode);
57     }
58
59     Field(String JavaDoc name, String JavaDoc type, ClassFile classFile) {
60     access = 0;
61     _name = name;
62     _type = type;
63     this.classFile = classFile;
64     attributes = new AttributeMap(new HashMap<String JavaDoc,byte[]>(1));
65     }
66     
67     public final String JavaDoc getName() {
68     if (_name == null && iName != 0) {
69         CPUTF8Info utfName = (CPUTF8Info)classFile.constantPool.get(iName);
70             _name = utfName.getName();
71     }
72         return _name;
73     }
74
75     public final String JavaDoc getDescriptor() {
76     if (_type == null && iType != 0) {
77         CPUTF8Info utfType = (CPUTF8Info)classFile.constantPool.get(iType);
78             _type = utfType.getName();
79     }
80         return _type;
81     }
82
83     public abstract String JavaDoc getDeclaration();
84     
85     public final int getAccess() {
86         return access;
87     }
88     
89     public final boolean isStatic() {
90         return Access.isStatic(access);
91     }
92
93     public final boolean isPublic() {
94         return Access.isPublic(access);
95     }
96
97     public final boolean isProtected() {
98         return Access.isProtected(access);
99     }
100
101     public final boolean isPackagePrivate() {
102         return Access.isPackagePrivate(access);
103     }
104
105     public final boolean isPrivate() {
106         return Access.isPrivate(access);
107     }
108
109     public final boolean isDeprecated() {
110     return attributes.get("Deprecated") != null;
111     }
112     
113     public final boolean isSynthetic() {
114         return attributes.get("Synthetic") != null;
115     }
116
117     /**
118      * Returns the class file this field is defined in.
119      * @return the class file of this field.
120      */

121     public final ClassFile getClassFile() {
122         return classFile;
123     }
124     
125     /**
126      * Returns the generic type information associated with this field.
127      * If this field does not have generic type information, then null
128      * is returned.
129      */

130     public String JavaDoc getTypeSignature() {
131     if (typeSignature == null) {
132         DataInputStream in = attributes.getStream("Signature"); // NOI18N
133
if (in != null) {
134         try {
135             int index = in.readUnsignedShort();
136             CPUTF8Info entry =
137             (CPUTF8Info)classFile.constantPool.get(index);
138             typeSignature = entry.getName();
139             in.close();
140         } catch (IOException e) {
141             throw new InvalidClassFileAttributeException("invalid Signature attribute", e);
142         }
143         }
144     }
145     return typeSignature;
146     }
147
148     void setTypeSignature(String JavaDoc sig) {
149     typeSignature = sig;
150     }
151
152     /**
153      * Returns al<ClassName,Annotation>l runtime annotations defined for this field. Inherited
154      * annotations are not included in this collection.
155      */

156     public final Collection<Annotation> getAnnotations() {
157     loadAnnotations();
158     return annotations.values();
159     }
160
161     /**
162      * Returns the annotation for a specified annotation type, or null if
163      * no annotation of that type exists for this field.
164      */

165     public final Annotation getAnnotation(final ClassName annotationClass) {
166     loadAnnotations();
167     return annotations.get(annotationClass);
168     }
169     
170     /**
171      * Returns true if an annotation of the specified type is defined for
172      * this field.
173      */

174     public final boolean isAnnotationPresent(final ClassName annotationClass) {
175     loadAnnotations();
176     return annotations.get(annotationClass) != null;
177     }
178     
179     /**
180      * Returns a map of the raw attributes for this field. The
181      * keys for this map are the names of the attributes (as Strings,
182      * not constant pool indexes). The values are byte arrays that
183      * hold the contents of the attribute. If the ClassFile was
184      * created with an <code>includeCode</code> parameter that is
185      * false, then <b>Code</b> attributes are not included in this map.
186      *
187      * @see org.netbeans.modules.classfile.ClassFile#getAttributes
188      */

189     public final AttributeMap getAttributes(){
190         return attributes;
191     }
192     
193     void loadAnnotations() {
194     if (annotations == null)
195         annotations = ClassFile.buildAnnotationMap(classFile.constantPool,
196                                attributes);
197     }
198
199     public String JavaDoc toString() {
200         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
201     String JavaDoc name = getName();
202     if (name != null) {
203         sb.append(getName());
204         sb.append(' ');
205     }
206         if (isSynthetic())
207             sb.append("(synthetic)"); //NOI18N
208
if (isDeprecated())
209             sb.append("(deprecated)"); //NOI18N
210
sb.append("type="); //NOI18N
211
sb.append(getDescriptor());
212     if (getTypeSignature() != null) {
213         sb.append(", signature="); //NOI18N
214
sb.append(typeSignature);
215     }
216         sb.append(", access="); //NOI18N
217
sb.append(Access.toString(access));
218     loadAnnotations();
219     if (annotations.size() > 0) {
220         Iterator iter = annotations.values().iterator();
221         sb.append(", annotations={ ");
222         while (iter.hasNext()) {
223         sb.append(iter.next().toString());
224         if (iter.hasNext())
225             sb.append(", ");
226         }
227         sb.append(" }");
228     }
229         return sb.toString();
230     }
231 }
232
Popular Tags