Skip to content

Commit 1b8d622

Browse files
committed
Streamline the ByteCodeAnalyzer
As far as the annotation indexer is concerned, we need not parse interfaces, fields nor methods. So let's skip that part and cut to the chase. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 5463d7e commit 1b8d622

File tree

1 file changed

+22
-107
lines changed

1 file changed

+22
-107
lines changed

src/main/java/org/scijava/annotations/ByteCodeAnalyzer.java

Lines changed: 22 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,27 @@ class ByteCodeAnalyzer {
5050

5151
private byte[] buffer;
5252
private int[] poolOffsets;
53-
private int endOffset, interfacesOffset, fieldsOffset, methodsOffset,
54-
attributesOffset;
55-
private Interface[] interfaces;
56-
private Field[] fields;
57-
private Method[] methods;
53+
private int endOffset;
5854
private Attribute[] attributes;
5955

6056
private ByteCodeAnalyzer(final byte[] buffer) {
6157
this.buffer = buffer;
6258
if ((int) getU4(0) != 0xcafebabe) throw new RuntimeException("No class");
6359
getConstantPoolOffsets();
64-
getAllInterfaces();
65-
getAllFields();
66-
getAllMethods();
60+
// skip interfaces
61+
endOffset += 8 + 2 * getU2(endOffset + 6);
62+
// skip fields
63+
int fieldCount = getU2(endOffset);
64+
endOffset += 2;
65+
for (int i = 0; i < fieldCount; i++) {
66+
endOffset = skipAttributes(endOffset + 6);
67+
}
68+
// skip methods
69+
int methodCount = getU2(endOffset);
70+
endOffset += 2;
71+
for (int i = 0; i < methodCount; i++) {
72+
endOffset = skipAttributes(endOffset + 6);
73+
}
6774
getAllAttributes();
6875
}
6976

@@ -120,26 +127,6 @@ else if (tag == 5 || tag == 6) {
120127
endOffset = offset;
121128
}
122129

123-
@Override
124-
public String toString() {
125-
String result = "";
126-
for (int i = 0; i < poolOffsets.length; i++) {
127-
final int offset = poolOffsets[i];
128-
result += "index #" + (i + 1) + ": " + format(offset) + "\n";
129-
final int tag = getU1(offset);
130-
if (tag == 5 || tag == 6) i++;
131-
}
132-
if (interfaces != null) for (int i = 0; i < interfaces.length; i++)
133-
result += "interface #" + (i + 1) + ": " + interfaces[i] + "\n";
134-
if (fields != null) for (int i = 0; i < fields.length; i++)
135-
result += "field #" + (i + 1) + ": " + fields[i] + "\n";
136-
if (methods != null) for (int i = 0; i < methods.length; i++)
137-
result += "method #" + (i + 1) + ": " + methods[i] + "\n";
138-
if (attributes != null) for (int i = 0; i < attributes.length; i++)
139-
result += "attribute #" + (i + 1) + ": " + attributes[i] + "\n";
140-
return result;
141-
}
142-
143130
private int getU1(final int offset) {
144131
return getU1(buffer, offset);
145132
}
@@ -173,89 +160,17 @@ private String getString(final int offset) {
173160
}
174161
}
175162

176-
private String format(final int offset) {
177-
final int tag = getU1(offset);
178-
final int u2 = getU2(offset + 1);
179-
final String result = "offset: " + offset + "(" + tag + "), ";
180-
if (tag == 7) return result + "class #" + u2;
181-
if (tag == 9) return result + "field #" + u2 + ", #" + getU2(offset + 3);
182-
if (tag == 10) return result + "method #" + u2 + ", #" + getU2(offset + 3);
183-
if (tag == 11) return result + "interface method #" + u2 + ", #" +
184-
getU2(offset + 3);
185-
if (tag == 8) return result + "string #" + u2;
186-
if (tag == 3) return result + "integer " + getU4(offset + 1);
187-
if (tag == 4) return result + "float " + getU4(offset + 1);
188-
if (tag == 12) return result + "name and type #" + u2 + ", #" +
189-
getU2(offset + 3);
190-
if (tag == 5) return result + "long " + getU4(offset + 1) + ", " +
191-
getU4(offset + 5);
192-
if (tag == 6) return result + "double " + getU4(offset + 1) + ", " +
193-
getU4(offset + 5);
194-
if (tag == 1) return result + "utf8 " + u2 + " " + getString(offset);
195-
return result + "unknown";
196-
}
197-
198-
private void getAllInterfaces() {
199-
interfacesOffset = endOffset + 6;
200-
interfaces = new Interface[getU2(interfacesOffset)];
201-
for (int i = 0; i < interfaces.length; i++)
202-
interfaces[i] = new Interface(interfacesOffset + 2 + i * 2);
203-
}
204-
205-
private class Interface {
206-
207-
private Interface(final int offset) {
208-
}
209-
}
210-
211-
private void getAllFields() {
212-
fieldsOffset = interfacesOffset + 2 + 2 * interfaces.length;
213-
fields = new Field[getU2(fieldsOffset)];
214-
for (int i = 0; i < fields.length; i++)
215-
fields[i] =
216-
new Field(i == 0 ? fieldsOffset + 2 : fields[i - 1].getFieldEndOffset());
217-
}
218-
219-
private class Field {
220-
221-
private Attribute[] fieldAttributes;
222-
private int fieldEndOffset;
223-
224-
private Field(final int offset) {
225-
fieldAttributes = getAttributes(offset + 6);
226-
fieldEndOffset =
227-
fieldAttributes.length == 0 ? offset + 8
228-
: fieldAttributes[fieldAttributes.length - 1].attributeEndOffset;
229-
}
230-
231-
protected int getFieldEndOffset() {
232-
return fieldEndOffset;
233-
}
234-
}
235-
236-
private void getAllMethods() {
237-
methodsOffset =
238-
fields.length == 0 ? fieldsOffset + 2 : fields[fields.length - 1]
239-
.getFieldEndOffset();
240-
methods = new Method[getU2(methodsOffset)];
241-
for (int i = 0; i < methods.length; i++)
242-
methods[i] =
243-
new Method(i == 0 ? methodsOffset + 2 : methods[i - 1]
244-
.getFieldEndOffset());
245-
}
246-
247-
private class Method extends Field {
248-
249-
private Method(final int offset) {
250-
super(offset);
163+
private int skipAttributes(int offset) {
164+
int count = getU2(offset);
165+
offset += 2;
166+
for (int i = 0; i < count; i++) {
167+
offset += 6 + getU4(offset + 2);
251168
}
169+
return offset;
252170
}
253171

254172
private void getAllAttributes() {
255-
attributesOffset =
256-
methods.length == 0 ? methodsOffset + 2 : methods[methods.length - 1]
257-
.getFieldEndOffset();
258-
attributes = getAttributes(attributesOffset);
173+
attributes = getAttributes(endOffset);
259174
}
260175

261176
private Attribute[] getAttributes(final int offset) {

0 commit comments

Comments
 (0)