KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Variable.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.10 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29
30 /**
31  * A Java field. Unfortunately, the word "field" is generally used in
32  * the Java documentation to mean either a variable, or both variables
33  * and methods. This class only describes variables.
34  *
35  * @author Thomas Ball
36  */

37 public final class Variable extends Field {
38
39     private Object JavaDoc constValue = notLoadedConstValue;
40     
41     private static final Object JavaDoc notLoadedConstValue = new Object JavaDoc();
42
43     static Variable[] loadFields(DataInputStream in, ConstantPool pool,
44                                  ClassFile cls)
45       throws IOException {
46           int count = in.readUnsignedShort();
47           Variable[] variables = new Variable[count];
48           for (int i = 0; i < count; i++)
49               variables[i] = new Variable(in, pool, cls);
50           return variables;
51     }
52     
53     /** Creates new Variable */
54     Variable(DataInputStream in, ConstantPool pool, ClassFile cls)
55     throws IOException {
56         super(in, pool, cls, false);
57     }
58
59     /**
60      * Returns true if the variable is a constant; that is, a final
61      * static variable.
62      * @see #getConstantValue
63      */

64     public final boolean isConstant() {
65         return attributes.get("ConstantValue") != null;//NOI18N
66
}
67
68     /**
69      * Returns the value object of this variable if it is a constant,
70      * otherwise null.
71      * @deprecated replaced by <code>Object getConstantValue()</code>.
72      */

73     @Deprecated JavaDoc
74     public final Object JavaDoc getValue() {
75     return getConstantValue();
76     }
77     
78     /**
79      * Returns the value object of this variable if it is a constant,
80      * otherwise null.
81      * @see #isConstant
82      */

83     public final Object JavaDoc getConstantValue() {
84     if (constValue == notLoadedConstValue) {
85         DataInputStream in = attributes.getStream("ConstantValue"); // NOI18N
86
if (in != null) {
87         try {
88             int index = in.readUnsignedShort();
89             CPEntry cpe = classFile.constantPool.get(index);
90             constValue = cpe.getValue();
91         } catch (IOException e) {
92             throw new InvalidClassFileAttributeException("invalid ConstantValue attribute", e);
93         }
94         }
95     }
96         return constValue;
97     }
98     
99     /**
100      * Return a string in the form "<type> <name>". Class types
101      * are shown in a "short" form; i.e. "Object" instead of
102      * "java.lang.Object"j.
103      *
104      * @return string describing the variable and its type.
105      */

106     public final String JavaDoc getDeclaration() {
107     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
108     sb.append(CPFieldMethodInfo.getSignature(getDescriptor(), false));
109     sb.append(' ');
110     sb.append(getName());
111     return sb.toString();
112     }
113
114     /**
115      * Returns true if this field defines an enum constant.
116      */

117     public final boolean isEnumConstant() {
118     return (access & Access.ENUM) == Access.ENUM;
119     }
120             
121     public String JavaDoc toString() {
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
123         if (isConstant()) {
124         sb.append(", const value="); //NOI18N
125
sb.append(getValue());
126     }
127         return sb.toString();
128     }
129 }
130
Popular Tags