Skip to content

Commit 45936f0

Browse files
committed
ClassReader revision from ASM master
Issue: SPR-17267
1 parent 43a814b commit 45936f0

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

spring-core/src/main/java/org/springframework/asm/ClassReader.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ public class ClassReader {
109109
private final int[] cpInfoOffsets;
110110

111111
/**
112-
* The value of each cp_info entry of the ClassFile's constant_pool array, <i>for Constant_Utf8
113-
* and Constant_Dynamic constants only</i>. The value of constant pool entry i is given by
114-
* cpInfoValues[i]. This cache avoids multiple parsing of those constant pool items.
112+
* The String objects corresponding to the CONSTANT_Utf8 constant pool items. This cache avoids
113+
* multiple parsing of a given CONSTANT_Utf8 constant pool item.
115114
*/
116-
private final Object[] cpInfoValues;
115+
private final String[] constantUtf8Values;
116+
117+
/**
118+
* The ConstantDynamic objects corresponding to the CONSTANT_Dynamic constant pool items. This
119+
* cache avoids multiple parsing of a given CONSTANT_Dynamic constant pool item.
120+
*/
121+
private final ConstantDynamic[] constantDynamicValues;
117122

118123
/**
119124
* The start offsets in {@link #b} of each element of the bootstrap_methods array (in the
@@ -168,7 +173,7 @@ public ClassReader(
168173
*/
169174
ClassReader(
170175
final byte[] classFileBuffer, final int classFileOffset, final boolean checkClassVersion) {
171-
this.b = classFileBuffer;
176+
b = classFileBuffer;
172177
// Check the class' major_version. This field is after the magic and minor_version fields, which
173178
// use 4 and 2 bytes respectively.
174179
if (checkClassVersion && readShort(classFileOffset + 6) > Opcodes.V12) {
@@ -179,15 +184,16 @@ public ClassReader(
179184
// minor_version and major_version fields, which use 4, 2 and 2 bytes respectively.
180185
int constantPoolCount = readUnsignedShort(classFileOffset + 8);
181186
cpInfoOffsets = new int[constantPoolCount];
182-
cpInfoValues = new Object[constantPoolCount];
187+
constantUtf8Values = new String[constantPoolCount];
183188
// Compute the offset of each constant pool entry, as well as a conservative estimate of the
184189
// maximum length of the constant pool strings. The first constant pool entry is after the
185190
// magic, minor_version, major_version and constant_pool_count fields, which use 4, 2, 2 and 2
186191
// bytes respectively.
187192
int currentCpInfoIndex = 1;
188193
int currentCpInfoOffset = classFileOffset + 10;
189194
int currentMaxStringLength = 0;
190-
boolean hasBootstrapMethods = false;
195+
boolean hasConstantDynamic = false;
196+
boolean hasConstantInvokeDynamic = false;
191197
// The offset of the other entries depend on the total size of all the previous entries.
192198
while (currentCpInfoIndex < constantPoolCount) {
193199
cpInfoOffsets[currentCpInfoIndex++] = currentCpInfoOffset + 1;
@@ -201,10 +207,13 @@ public ClassReader(
201207
case Symbol.CONSTANT_NAME_AND_TYPE_TAG:
202208
cpInfoSize = 5;
203209
break;
204-
case Symbol.CONSTANT_INVOKE_DYNAMIC_TAG:
205210
case Symbol.CONSTANT_DYNAMIC_TAG:
206211
cpInfoSize = 5;
207-
hasBootstrapMethods = true;
212+
hasConstantDynamic = true;
213+
break;
214+
case Symbol.CONSTANT_INVOKE_DYNAMIC_TAG:
215+
cpInfoSize = 5;
216+
hasConstantInvokeDynamic = true;
208217
break;
209218
case Symbol.CONSTANT_LONG_TAG:
210219
case Symbol.CONSTANT_DOUBLE_TAG:
@@ -235,13 +244,18 @@ public ClassReader(
235244
}
236245
currentCpInfoOffset += cpInfoSize;
237246
}
238-
this.maxStringLength = currentMaxStringLength;
247+
maxStringLength = currentMaxStringLength;
239248
// The Classfile's access_flags field is just after the last constant pool entry.
240-
this.header = currentCpInfoOffset;
249+
header = currentCpInfoOffset;
250+
251+
// Allocate the cache of ConstantDynamic values, if there is at least one.
252+
constantDynamicValues = hasConstantDynamic ? new ConstantDynamic[constantPoolCount] : null;
241253

242254
// Read the BootstrapMethods attribute, if any (only get the offset of each method).
243-
this.bootstrapMethodOffsets =
244-
hasBootstrapMethods ? readBootstrapMethodsAttribute(currentMaxStringLength) : null;
255+
bootstrapMethodOffsets =
256+
(hasConstantDynamic | hasConstantInvokeDynamic)
257+
? readBootstrapMethodsAttribute(currentMaxStringLength)
258+
: null;
245259
}
246260

247261
/**
@@ -3403,14 +3417,13 @@ public String readUTF8(final int offset, final char[] charBuffer) {
34033417
* @return the String corresponding to the specified CONSTANT_Utf8 entry.
34043418
*/
34053419
final String readUTF(final int constantPoolEntryIndex, final char[] charBuffer) {
3406-
String value = (String) cpInfoValues[constantPoolEntryIndex];
3420+
String value = constantUtf8Values[constantPoolEntryIndex];
34073421
if (value != null) {
34083422
return value;
34093423
}
34103424
int cpInfoOffset = cpInfoOffsets[constantPoolEntryIndex];
3411-
value = readUTF(cpInfoOffset + 2, readUnsignedShort(cpInfoOffset), charBuffer);
3412-
cpInfoValues[constantPoolEntryIndex] = value;
3413-
return value;
3425+
return constantUtf8Values[constantPoolEntryIndex] =
3426+
readUTF(cpInfoOffset + 2, readUnsignedShort(cpInfoOffset), charBuffer);
34143427
}
34153428

34163429
/**
@@ -3516,7 +3529,7 @@ public String readPackage(final int offset, final char[] charBuffer) {
35163529
*/
35173530
private ConstantDynamic readConstantDynamic(
35183531
final int constantPoolEntryIndex, final char[] charBuffer) {
3519-
ConstantDynamic constantDynamic = (ConstantDynamic) cpInfoValues[constantPoolEntryIndex];
3532+
ConstantDynamic constantDynamic = constantDynamicValues[constantPoolEntryIndex];
35203533
if (constantDynamic != null) {
35213534
return constantDynamic;
35223535
}
@@ -3532,9 +3545,8 @@ private ConstantDynamic readConstantDynamic(
35323545
bootstrapMethodArguments[i] = readConst(readUnsignedShort(bootstrapMethodOffset), charBuffer);
35333546
bootstrapMethodOffset += 2;
35343547
}
3535-
constantDynamic = new ConstantDynamic(name, descriptor, handle, bootstrapMethodArguments);
3536-
cpInfoValues[constantPoolEntryIndex] = constantDynamic;
3537-
return constantDynamic;
3548+
return constantDynamicValues[constantPoolEntryIndex] =
3549+
new ConstantDynamic(name, descriptor, handle, bootstrapMethodArguments);
35383550
}
35393551

35403552
/**

0 commit comments

Comments
 (0)