/Users/lyon/j4p/src/javassist/bytecode/FieldInfo.java
|
1 /*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16 package javassist.bytecode;
17
18 import java.io.DataInputStream;
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.util.Hashtable;
22 import java.util.List;
23 import java.util.LinkedList;
24
25 /**
26 * <code>field_info</code> structure.
27 *
28 * @see javassist.CtField#getFieldInfo()
29 */
30 public final class FieldInfo {
31 ConstPool constPool;
32 int accessFlags;
33 int name;
34 int descriptor;
35 LinkedList attribute; // may be null.
36
37 private FieldInfo(ConstPool cp) {
38 constPool = cp;
39 accessFlags = 0;
40 attribute = null;
41 }
42
43 /**
44 * Constructs a <code>field_info</code> structure.
45 *
46 * @param cp a constant pool table
47 * @param fieldName field name
48 * @param desc field descriptor
49 *
50 * @see Descriptor
51 */
52 public FieldInfo(ConstPool cp, String fieldName, String desc) {
53 this(cp);
54 name = cp.addUtf8Info(fieldName);
55 descriptor = cp.addUtf8Info(desc);
56 }
57
58 FieldInfo(ConstPool cp, DataInputStream in) throws IOException {
59 this(cp);
60 read(in);
61 }
62
63 /**
64 * Returns the constant pool table used
65 * by this <code>field_info</code>.
66 */
67 public ConstPool getConstPool() {
68 return constPool;
69 }
70
71 /**
72 * Returns the field name.
73 */
74 public String getName() {
75 return constPool.getUtf8Info(name);
76 }
77
78 /**
79 * Sets the field name.
80 */
81 public void setName(String newName) {
82 name = constPool.addUtf8Info(newName);
83 }
84
85 /**
86 * Returns the access flags.
87 *
88 * @see AccessFlag
89 */
90 public int getAccessFlags() {
91 return accessFlags;
92 }
93
94 /**
95 * Sets the access flags.
96 *
97 * @see AccessFlag
98 */
99 public void setAccessFlags(int acc) {
100 accessFlags = acc;
101 }
102
103 /**
104 * Returns the field descriptor.
105 *
106 * @see Descriptor
107 */
108 public String getDescriptor() {
109 return constPool.getUtf8Info(descriptor);
110 }
111
112 /**
113 * Sets the field descriptor.
114 *
115 * @see Descriptor
116 */
117 public void setDescriptor(String desc) {
118 if (!desc.equals(getDescriptor()))
119 descriptor = constPool.addUtf8Info(desc);
120 }
121
122 /**
123 * Returns all the attributes.
124 *
125 * @return a list of <code>AttributeInfo</code> objects.
126 * @see AttributeInfo
127 */
128 public List getAttributes() {
129 if (attribute == null)
130 attribute = new LinkedList();
131
132 return attribute;
133 }
134
135 /**
136 * Returns the attribute with the specified name.
137 *
138 * @param name attribute name
139 */
140 public AttributeInfo getAttribute(String name) {
141 return AttributeInfo.lookup(attribute, name);
142 }
143
144 /**
145 * Appends an attribute. If there is already an attribute with
146 * the same name, the new one substitutes for it.
147 */
148 public void addAttribute(AttributeInfo info) {
149 if (attribute == null)
150 attribute = new LinkedList();
151
152 AttributeInfo.remove(attribute, info.getName());
153 attribute.add(info);
154 }
155
156 private void read(DataInputStream in) throws IOException {
157 accessFlags = in.readUnsignedShort();
158 name = in.readUnsignedShort();
159 descriptor = in.readUnsignedShort();
160 int n = in.readUnsignedShort();
161 attribute = new LinkedList();
162 for (int i = 0; i < n; ++i)
163 attribute.add(AttributeInfo.read(constPool, in));
164 }
165
166 void write(DataOutputStream out) throws IOException {
167 out.writeShort(accessFlags);
168 out.writeShort(name);
169 out.writeShort(descriptor);
170 if (attribute == null)
171 out.writeShort(0);
172 else {
173 out.writeShort(attribute.size());
174 AttributeInfo.writeAll(attribute, out);
175 }
176 }
177 }
178