1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package javassist.bytecode;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.LinkedList;
import java.util.ListIterator;
// Note: if you define a new subclass of AttributeInfo, then
// update AttributeInfo.read().
/**
* <code>attribute_info</code> structure.
*/
public class AttributeInfo {
protected ConstPool constPool;
int name;
byte[] info;
protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) {
constPool = cp;
name = attrname;
info = attrinfo;
}
protected AttributeInfo(ConstPool cp, String attrname) {
this(cp, attrname, (byte[])null);
}
/**
* Constructs an <code>attribute_info</code> structure.
*
* @param cp constant pool table
* @param attrname attribute name
* @param attrinfo <code>info</code> field
* of <code>attribute_info</code> structure.
*/
public AttributeInfo(ConstPool cp, String attrname, byte[] attrinfo) {
this(cp, cp.addUtf8Info(attrname), attrinfo);
}
protected AttributeInfo(ConstPool cp, int n, DataInputStream in)
throws IOException
{
constPool = cp;
name = n;
int len = in.readInt();
info = new byte[len];
if (len > 0)
in.readFully(info);
}
static AttributeInfo read(ConstPool cp, DataInputStream in)
throws IOException
{
int name = in.readUnsignedShort();
String nameStr = cp.getUtf8Info(name);
if (nameStr.equals(CodeAttribute.tag))
return new CodeAttribute(cp, name, in);
else if (nameStr.equals(ExceptionsAttribute.tag))
return new ExceptionsAttribute(cp, name, in);
else if (nameStr.equals(ConstantAttribute.tag))
return new ConstantAttribute(cp, name, in);
else if (nameStr.equals(SourceFileAttribute.tag))
return new SourceFileAttribute(cp, name, in);
else if (nameStr.equals(LineNumberAttribute.tag))
return new LineNumberAttribute(cp, name, in);
else if (nameStr.equals(SyntheticAttribute.tag))
return new SyntheticAttribute(cp, name, in);
else if (nameStr.equals(InnerClassesAttribute.tag))
return new InnerClassesAttribute(cp, name, in);
else
return new AttributeInfo(cp, name, in);
}
/**
* Returns an attribute name.
*/
public String getName() {
return constPool.getUtf8Info(name);
}
/**
* Returns a constant pool table.
*/
public ConstPool getConstPool() { return constPool; }
/**
* Returns the length of this <code>attribute_info</code>
* structure.
* The returned value is <code>attribute_length + 6</code>.
*/
public int length() {
return info.length + 6;
}
/**
* Returns the <code>info</code> field
* of this <code>attribute_info</code> structure.
*
* <p>This method is not available if the object is an instance
* of <code>CodeAttribute</code>.
*/
public byte[] get() { return info; }
/**
* Sets the <code>info</code> field
* of this <code>attribute_info</code> structure.
*
* <p>This method is not available if the object is an instance
* of <code>CodeAttribute</code>.
*/
public void set(byte[] newinfo) { info = newinfo; }
/**
* Makes a copy. Class names are replaced according to the
* given <code>Map</code> object.
*
* @param newCp the constant pool table used by the new copy.
* @param classnames pairs of replaced and substituted
* class names.
*/
public AttributeInfo copy(ConstPool newCp, Map classnames) {
int s = info.length;
byte[] newInfo = new byte[s];
for (int i = 0; i < s; ++i)
newInfo[i] = info[i];
return new AttributeInfo(newCp, getName(), newInfo);
}
void write(DataOutputStream out) throws IOException {
out.writeShort(name);
out.writeInt(info.length);
if (info.length > 0)
out.write(info);
}
static int getLength(LinkedList list) {
int size = 0;
int n = list.size();
for (int i = 0; i < n; ++i) {
AttributeInfo attr = (AttributeInfo)list.get(i);
size += attr.length();
}
return size;
}
static AttributeInfo lookup(LinkedList list, String name) {
if (list == null)
return null;
ListIterator iterator = list.listIterator();
while (iterator.hasNext()) {
AttributeInfo ai = (AttributeInfo)iterator.next();
if (ai.getName().equals(name))
return ai;
}
return null; // no such attribute
}
static AttributeInfo lookup(LinkedList list, Class type) {
if (list == null)
return null;
ListIterator iterator = list.listIterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (type.isInstance(obj))
return (AttributeInfo)obj;
}
return null; // no such attribute
}
static synchronized void remove(LinkedList list, String name) {
if (list == null)
return;
ListIterator iterator = list.listIterator();
while (iterator.hasNext()) {
AttributeInfo ai = (AttributeInfo)iterator.next();
if (ai.getName().equals(name))
iterator.remove();
}
}
static synchronized void remove(LinkedList list, Class type) {
if (list == null)
return;
ListIterator iterator = list.listIterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (type.isInstance(obj))
iterator.remove();
}
}
static void writeAll(LinkedList list, DataOutputStream out)
throws IOException
{
if (list == null)
return;
int n = list.size();
for (int i = 0; i < n; ++i) {
AttributeInfo attr = (AttributeInfo)list.get(i);
attr.write(out);
}
}
}
|