KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > AttributeInfo


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 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 JavaDoc;
19 import java.io.DataOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.ListIterator JavaDoc;
24
25 // Note: if you define a new subclass of AttributeInfo, then
26
// update AttributeInfo.read().
27

28 /**
29  * <code>attribute_info</code> structure.
30  */

31 public class AttributeInfo {
32     protected ConstPool constPool;
33     int name;
34     byte[] info;
35
36     protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) {
37         constPool = cp;
38         name = attrname;
39         info = attrinfo;
40     }
41
42     protected AttributeInfo(ConstPool cp, String JavaDoc attrname) {
43         this(cp, attrname, (byte[])null);
44     }
45
46     /**
47      * Constructs an <code>attribute_info</code> structure.
48      *
49      * @param cp constant pool table
50      * @param attrname attribute name
51      * @param attrinfo <code>info</code> field
52      * of <code>attribute_info</code> structure.
53      */

54     public AttributeInfo(ConstPool cp, String JavaDoc attrname, byte[] attrinfo) {
55         this(cp, cp.addUtf8Info(attrname), attrinfo);
56     }
57
58     protected AttributeInfo(ConstPool cp, int n, DataInputStream JavaDoc in)
59         throws IOException JavaDoc
60     {
61         constPool = cp;
62         name = n;
63         int len = in.readInt();
64         info = new byte[len];
65         if (len > 0)
66             in.readFully(info);
67     }
68
69     static AttributeInfo read(ConstPool cp, DataInputStream JavaDoc in)
70         throws IOException JavaDoc
71     {
72         int name = in.readUnsignedShort();
73         String JavaDoc nameStr = cp.getUtf8Info(name);
74         if (nameStr.charAt(0) < 'L') {
75             if (nameStr.equals(CodeAttribute.tag))
76                 return new CodeAttribute(cp, name, in);
77             else if (nameStr.equals(ConstantAttribute.tag))
78                 return new ConstantAttribute(cp, name, in);
79             else if (nameStr.equals(DeprecatedAttribute.tag))
80                 return new DeprecatedAttribute(cp, name, in);
81             else if (nameStr.equals(EnclosingMethodAttribute.tag))
82                 return new EnclosingMethodAttribute(cp, name, in);
83             else if (nameStr.equals(ExceptionsAttribute.tag))
84                 return new ExceptionsAttribute(cp, name, in);
85             else if (nameStr.equals(InnerClassesAttribute.tag))
86                 return new InnerClassesAttribute(cp, name, in);
87         }
88         else {
89             /* Note that the names of Annotations attributes begin with 'R'.
90              */

91             if (nameStr.equals(LineNumberAttribute.tag))
92                 return new LineNumberAttribute(cp, name, in);
93             else if (nameStr.equals(LocalVariableAttribute.tag)
94                      || nameStr.equals(LocalVariableAttribute.typeTag))
95                 return new LocalVariableAttribute(cp, name, in);
96             else if (nameStr.equals(AnnotationsAttribute.visibleTag)
97                      || nameStr.equals(AnnotationsAttribute.invisibleTag))
98                 return new AnnotationsAttribute(cp, name, in);
99             else if (nameStr.equals(ParameterAnnotationsAttribute.visibleTag)
100                 || nameStr.equals(ParameterAnnotationsAttribute.invisibleTag))
101                 return new ParameterAnnotationsAttribute(cp, name, in);
102             else if (nameStr.equals(SignatureAttribute.tag))
103                 return new SignatureAttribute(cp, name, in);
104             else if (nameStr.equals(SourceFileAttribute.tag))
105                 return new SourceFileAttribute(cp, name, in);
106             else if (nameStr.equals(SyntheticAttribute.tag))
107                 return new SyntheticAttribute(cp, name, in);
108         }
109
110         return new AttributeInfo(cp, name, in);
111     }
112
113     /**
114      * Returns an attribute name.
115      */

116     public String JavaDoc getName() {
117         return constPool.getUtf8Info(name);
118     }
119
120     /**
121      * Returns a constant pool table.
122      */

123     public ConstPool getConstPool() { return constPool; }
124
125     /**
126      * Returns the length of this <code>attribute_info</code>
127      * structure.
128      * The returned value is <code>attribute_length + 6</code>.
129      */

130     public int length() {
131         return info.length + 6;
132     }
133
134     /**
135      * Returns the <code>info</code> field
136      * of this <code>attribute_info</code> structure.
137      *
138      * <p>This method is not available if the object is an instance
139      * of <code>CodeAttribute</code>.
140      */

141     public byte[] get() { return info; }
142
143     /**
144      * Sets the <code>info</code> field
145      * of this <code>attribute_info</code> structure.
146      *
147      * <p>This method is not available if the object is an instance
148      * of <code>CodeAttribute</code>.
149      */

150     public void set(byte[] newinfo) { info = newinfo; }
151
152     /**
153      * Makes a copy. Class names are replaced according to the
154      * given <code>Map</code> object.
155      *
156      * @param newCp the constant pool table used by the new copy.
157      * @param classnames pairs of replaced and substituted
158      * class names.
159      */

160     public AttributeInfo copy(ConstPool newCp, Map JavaDoc classnames) {
161         int s = info.length;
162         byte[] newInfo = new byte[s];
163         for (int i = 0; i < s; ++i)
164             newInfo[i] = info[i];
165
166         return new AttributeInfo(newCp, getName(), newInfo);
167     }
168
169     void write(DataOutputStream JavaDoc out) throws IOException JavaDoc {
170         out.writeShort(name);
171         out.writeInt(info.length);
172         if (info.length > 0)
173             out.write(info);
174     }
175
176     static int getLength(LinkedList JavaDoc list) {
177         int size = 0;
178         int n = list.size();
179         for (int i = 0; i < n; ++i) {
180             AttributeInfo attr = (AttributeInfo)list.get(i);
181             size += attr.length();
182         }
183
184         return size;
185     }
186
187     static AttributeInfo lookup(LinkedList JavaDoc list, String JavaDoc name) {
188         if (list == null)
189             return null;
190
191         ListIterator JavaDoc iterator = list.listIterator();
192         while (iterator.hasNext()) {
193             AttributeInfo ai = (AttributeInfo)iterator.next();
194             if (ai.getName().equals(name))
195                 return ai;
196         }
197
198         return null; // no such attribute
199
}
200
201     static synchronized void remove(LinkedList JavaDoc list, String JavaDoc name) {
202         if (list == null)
203             return;
204
205         ListIterator JavaDoc iterator = list.listIterator();
206         while (iterator.hasNext()) {
207             AttributeInfo ai = (AttributeInfo)iterator.next();
208             if (ai.getName().equals(name))
209                 iterator.remove();
210         }
211     }
212
213     static void writeAll(LinkedList JavaDoc list, DataOutputStream JavaDoc out)
214         throws IOException JavaDoc
215     {
216         if (list == null)
217             return;
218
219         int n = list.size();
220         for (int i = 0; i < n; ++i) {
221             AttributeInfo attr = (AttributeInfo)list.get(i);
222             attr.write(out);
223         }
224     }
225
226     static LinkedList JavaDoc copyAll(LinkedList JavaDoc list, ConstPool cp) {
227         if (list == null)
228             return null;
229
230         LinkedList JavaDoc newList = new LinkedList JavaDoc();
231         int n = list.size();
232         for (int i = 0; i < n; ++i) {
233             AttributeInfo attr = (AttributeInfo)list.get(i);
234             newList.add(attr.copy(cp, null));
235         }
236
237         return newList;
238     }
239 }
240
Popular Tags