-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertContendedIntoClassFile.java
159 lines (128 loc) · 5.39 KB
/
InsertContendedIntoClassFile.java
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
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.RuntimeVisibleAnnotations;
import org.apache.bcel.generic.ConstantPoolGen;
public class InsertContendedIntoClassFile{
private static String inputClassFile, outputClassFile;
private static String
fieldName; // name of the field to be annotated, specified from command line argument
private static int fieldIndex; // index of the field to be annotated
private static int indexRuntimeVisibleAnnotations;
private static int indexContendedAnnotation;
private static JavaClass java_class;
private static RuntimeVisibleAnnotations runtimeVisibleAnnotation;
public static void writeConstantPool() {
try {
ConstantPool constants = java_class.getConstantPool();
ConstantPoolGen cp = new ConstantPoolGen(constants);
indexRuntimeVisibleAnnotations = cp.addUtf8("RuntimeVisibleAnnotations");
indexContendedAnnotation = cp.addUtf8("Ljdk/internal/vm/annotation/Contended;");
java_class.setConstantPool(cp.getFinalConstantPool());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void findFieldIndex() {
Field[] fields = java_class.getFields();
for (int i = 0; i < fields.length; ++i) {
if (fieldName.equals(fields[i].getName())) {
fieldIndex = i;
return;
}
}
}
public static void createRuntimeVisibleAnnotationAttribute() {
AnnotationEntry contendedAnnotation;
// Create an annotation entry for @Contended annotation
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeShort(indexContendedAnnotation); // index into the constant pool
dataOutputStream.writeShort(0); // number of element-value pairs
byte[] byteArray = byteArrayOutputStream.toByteArray();
// Create annotation with 'AnnotationEntry.read' instead of the constructor so that
// field 'elementValuePairs' is initialized, the 'dump' method can be executed later
contendedAnnotation =
AnnotationEntry.read(
new DataInputStream(new ByteArrayInputStream(byteArray)),
java_class.getConstantPool(),
true);
} catch (Exception e) {
e.printStackTrace();
return;
}
// Create the RuntimeVisibleAnnotations attribute
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
// Write the first 2 bytes (the number of annotation entries)
// In this case, we only need 1 annotation, @Contended annotation
dataOutputStream.writeShort(1);
// Write the Contended annotation to the output stream
contendedAnnotation.dump(dataOutputStream);
// get the internal byte array to create input stream
byte[] byteArray = byteArrayOutputStream.toByteArray();
runtimeVisibleAnnotation =
new RuntimeVisibleAnnotations(
indexRuntimeVisibleAnnotations,
6,
new DataInputStream(new ByteArrayInputStream(byteArray)),
java_class.getConstantPool());
} catch (Exception e) {
e.printStackTrace();
}
}
// We assume the field does not have runtime-visible annotation yet
public static void writeFieldAttributes() {
Field[] fields = java_class.getFields();
Attribute[] currentAttributes = fields[fieldIndex].getAttributes();
Attribute[] newAttributes = new Attribute[currentAttributes.length + 1];
for (int i = 0; i < currentAttributes.length; ++i) {
newAttributes[i] = currentAttributes[i];
}
newAttributes[newAttributes.length - 1] = runtimeVisibleAnnotation;
fields[fieldIndex].setAttributes(newAttributes);
}
public static void writeClassAttributes() {
Attribute[] currentAttributes = java_class.getAttributes();
Attribute[] newAttributes = new Attribute[currentAttributes.length + 1];
for (int i = 0; i < currentAttributes.length; ++i) {
newAttributes[i] = currentAttributes[i];
}
newAttributes[newAttributes.length - 1] = runtimeVisibleAnnotation;
java_class.setAttributes(newAttributes);
}
public static void dump() {
try {
java_class.dump(outputClassFile);
} catch (IOException e) {
}
}
public static void main(String[] args) {
inputClassFile = args[0];
fieldName = args.length > 1 ? args[1] : "";
outputClassFile = "modified" + inputClassFile + ".class";
try {
java_class = Repository.lookupClass(inputClassFile);
} catch (Exception e) {
}
InsertContendedIntoClassFile.writeConstantPool();
InsertContendedIntoClassFile.createRuntimeVisibleAnnotationAttribute();
if (args.length > 1) {
InsertContendedIntoClassFile.findFieldIndex();
InsertContendedIntoClassFile.writeFieldAttributes();
} else {
InsertContendedIntoClassFile.writeClassAttributes();
}
InsertContendedIntoClassFile.dump();
}
}