blob: 3b9e5f3a7c26013da1ca24875b827cd0606318d0 (
plain) (
blame)
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
|
/* FJBG -- Fast Java Bytecode Generator
* Copyright 2002-2011 LAMP/EPFL
* @author Michel Schinz
*/
package ch.epfl.lamp.fjbg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
/**
* Abstract superclass for attributes which can be attached to various
* parts of a class file.
*
* Attributes are used for classes (section 4.2), fields (section 4.6),
* methods (section 4.7) and the Code attribute (section 4.8.3).
* See sections 4.2 and later of the JVM specification.
*
* @author Michel Schinz
* @version 1.0
*/
public abstract class JAttribute {
protected final int nameIdx;
static public void writeTo(List/*<JAttribute>*/ attrs, DataOutputStream stream)
throws IOException {
stream.writeShort(attrs.size());
Iterator attrsIt = attrs.iterator();
while (attrsIt.hasNext()) {
JAttribute attr = (JAttribute)attrsIt.next();
attr.writeTo(stream);
}
}
static public List/*<JAttribute>*/ readFrom(FJBGContext context,
JClass clazz,
Object owner,
DataInputStream stream)
throws IOException {
JAttributeFactory factory = context.getJAttributeFactory();
int count = stream.readShort();
ArrayList list = new ArrayList(count);
for (int i = 0; i < count; ++i)
list.add(factory.newInstance(clazz, owner, stream));
return list;
}
public JAttribute(FJBGContext context, JClass clazz) {
this.nameIdx = clazz.getConstantPool().addUtf8(getName());
}
public JAttribute(FJBGContext context, JClass clazz, String name) {
this.nameIdx = clazz.getConstantPool().addUtf8(name);
}
abstract public String getName();
/**
* Write the attribute to a stream.
*/
public void writeTo(DataOutputStream stream) throws IOException {
int contentsSize = getSize();
stream.writeShort(nameIdx);
stream.writeInt(contentsSize);
int streamSizeBefore = stream.size();
writeContentsTo(stream);
int streamSizeDiff = stream.size() - streamSizeBefore;
assert contentsSize == streamSizeDiff
: "invalid size for attribute " + getName()
+ " given: " + contentsSize
+ " actual: " + streamSizeDiff;
}
// Note: it is not legal to add data to the constant pool during
// the execution of any of the following two methods.
protected abstract int getSize();
protected abstract void writeContentsTo(DataOutputStream stream)
throws IOException;
}
|