This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push: new 4637826 Initialize thread local with a lambda. 4637826 is described below commit 4637826ac76db6cd4ff7eb56d1b13e16d4b43320 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Wed Jan 13 16:17:27 2021 -0500 Initialize thread local with a lambda. --- .../java/org/apache/bcel/classfile/Utility.java | 67 ++++++++++------------ src/main/java/org/apache/bcel/generic/Type.java | 22 +++---- 2 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/apache/bcel/classfile/Utility.java b/src/main/java/org/apache/bcel/classfile/Utility.java index 094e3db..d5b93b2 100644 --- a/src/main/java/org/apache/bcel/classfile/Utility.java +++ b/src/main/java/org/apache/bcel/classfile/Utility.java @@ -58,12 +58,7 @@ public abstract class Utility { * Read by methodSignatureToString(). * Set by side effect, but only internally. */ - private static ThreadLocal<Integer> consumed_chars = new ThreadLocal<Integer>() { - @Override - protected Integer initialValue() { - return Integer.valueOf(0); - } - }; + private static final ThreadLocal<Integer> CONSUMER_CHARS = ThreadLocal.withInitial(() -> Integer.valueOf(0)); /* The `WIDE' instruction is used in the * byte code to allow 16-bit wide indices @@ -593,7 +588,7 @@ public abstract class Utility { while (signature.charAt(index) != ')') { vec.add(typeSignatureToString(signature.substring(index), chopit)); //corrected concurrent private static field acess - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e); @@ -709,7 +704,7 @@ public abstract class Utility { } buf.append(", "); //corrected concurrent private static field acess - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } index++; // update position // Read return type after `)' @@ -820,22 +815,22 @@ public abstract class Utility { if (signature.charAt(0) == '<') { // we have type paramters typeParams = typeParamTypesToString(signature, chopit); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } if (signature.charAt(index) == '(') { // We have a Method signature. // add types of arguments type = typeParams + typeSignaturesToString(signature.substring(index), chopit, ')'); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position // add return type type = type + typeSignatureToString(signature.substring(index), chopit); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position // ignore any throws information in the signature return type; } // Could be Class or Type... type = typeSignatureToString(signature.substring(index), chopit); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position if ((typeParams.length() == 0) && (index == signature.length())) { // We have a Type signature. return type; @@ -847,12 +842,12 @@ public abstract class Utility { if (index < signature.length()) { typeClass.append(" implements "); typeClass.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } while (index < signature.length()) { typeClass.append(", "); typeClass.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } return typeClass.toString(); } @@ -871,14 +866,14 @@ public abstract class Utility { int index = 1; // skip the '<' // get the first TypeParameter typeParams.append(typeParamTypeToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position // are there more TypeParameters? while (signature.charAt(index) != '>') { typeParams.append(", "); typeParams.append(typeParamTypeToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } - wrap(consumed_chars, index + 1); // account for the '>' char + wrap(CONSUMER_CHARS, index + 1); // account for the '>' char return typeParams.append(">").toString(); } @@ -902,16 +897,16 @@ public abstract class Utility { // we have a class bound typeParam.append(" extends "); typeParam.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } // look for interface bounds while (signature.charAt(index) == ':') { index++; // skip over the ':' typeParam.append(" & "); typeParam.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } - wrap(consumed_chars, index); + wrap(CONSUMER_CHARS, index); return typeParam.toString(); } @@ -931,15 +926,15 @@ public abstract class Utility { // get the first Type in the list if (signature.charAt(index) != term) { typeList.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } // are there more types in the list? while (signature.charAt(index) != term) { typeList.append(", "); typeList.append(typeSignatureToString(signature.substring(index), chopit)); - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMER_CHARS); // update position } - wrap(consumed_chars, index + 1); // account for the term char + wrap(CONSUMER_CHARS, index + 1); // account for the term char return typeList.append(term).toString(); } @@ -957,7 +952,7 @@ public abstract class Utility { */ public static String typeSignatureToString( final String signature, final boolean chopit ) throws ClassFormatException { //corrected concurrent private static field acess - wrap(consumed_chars, 1); // This is the default, read just one char like `B' + wrap(CONSUMER_CHARS, 1); // This is the default, read just one char like `B' try { switch (signature.charAt(0)) { case 'B': @@ -978,7 +973,7 @@ public abstract class Utility { throw new ClassFormatException("Invalid type variable signature: " + signature); } //corrected concurrent private static field acess - wrap(consumed_chars, index + 1); // "Tblabla;" `T' and `;' are removed + wrap(CONSUMER_CHARS, index + 1); // "Tblabla;" `T' and `;' are removed return compactClassName(signature.substring(1, index), chopit); } case 'L': { // Full class name @@ -1002,7 +997,7 @@ public abstract class Utility { final int bracketIndex = signature.substring(0, index).indexOf('<'); if (bracketIndex < 0) { // just a class identifier - wrap(consumed_chars, index + 1); // "Lblabla;" `L' and `;' are removed + wrap(CONSUMER_CHARS, index + 1); // "Lblabla;" `L' and `;' are removed return compactClassName(signature.substring(1, index), chopit); } // but make sure we are not looking past the end of the current item @@ -1012,7 +1007,7 @@ public abstract class Utility { } if (fromIndex < bracketIndex) { // just a class identifier - wrap(consumed_chars, fromIndex + 1); // "Lblabla;" `L' and `;' are removed + wrap(CONSUMER_CHARS, fromIndex + 1); // "Lblabla;" `L' and `;' are removed return compactClassName(signature.substring(1, fromIndex), chopit); } @@ -1037,8 +1032,8 @@ public abstract class Utility { } else { type.append(typeSignatureToString(signature.substring(consumed_chars), chopit)); // update our consumed count by the number of characters the for type argument - consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars; - wrap(Utility.consumed_chars, consumed_chars); + consumed_chars = unwrap(Utility.CONSUMER_CHARS) + consumed_chars; + wrap(Utility.CONSUMER_CHARS, consumed_chars); } // are there more TypeArguments? @@ -1058,8 +1053,8 @@ public abstract class Utility { } else { type.append(typeSignatureToString(signature.substring(consumed_chars), chopit)); // update our consumed count by the number of characters the for type argument - consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars; - wrap(Utility.consumed_chars, consumed_chars); + consumed_chars = unwrap(Utility.CONSUMER_CHARS) + consumed_chars; + wrap(Utility.CONSUMER_CHARS, consumed_chars); } } @@ -1076,14 +1071,14 @@ public abstract class Utility { // update our consumed count by the number of characters the for type argument // note that this count includes the "L" we added, but that is ok // as it accounts for the "." we didn't consume - consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars; - wrap(Utility.consumed_chars, consumed_chars); + consumed_chars = unwrap(Utility.CONSUMER_CHARS) + consumed_chars; + wrap(Utility.CONSUMER_CHARS, consumed_chars); return type.toString(); } if (signature.charAt(consumed_chars) != ';') { throw new ClassFormatException("Invalid signature: " + signature); } - wrap(Utility.consumed_chars, consumed_chars + 1); // remove final ";" + wrap(Utility.CONSUMER_CHARS, consumed_chars + 1); // remove final ";" return type.toString(); } case 'S': @@ -1105,8 +1100,8 @@ public abstract class Utility { type = typeSignatureToString(signature.substring(n), chopit); //corrected concurrent private static field acess //Utility.consumed_chars += consumed_chars; is replaced by: - final int _temp = unwrap(Utility.consumed_chars) + consumed_chars; - wrap(Utility.consumed_chars, _temp); + final int _temp = unwrap(Utility.CONSUMER_CHARS) + consumed_chars; + wrap(Utility.CONSUMER_CHARS, _temp); return type + brackets.toString(); } case 'V': diff --git a/src/main/java/org/apache/bcel/generic/Type.java b/src/main/java/org/apache/bcel/generic/Type.java index 93fb632..956eda9 100644 --- a/src/main/java/org/apache/bcel/generic/Type.java +++ b/src/main/java/org/apache/bcel/generic/Type.java @@ -167,25 +167,17 @@ public abstract class Type { return buf.toString(); } - private static final ThreadLocal<Integer> consumed_chars = new ThreadLocal<Integer>() { - - @Override - protected Integer initialValue() { - return Integer.valueOf(0); - } - };//int consumed_chars=0; // Remember position in string, see getArgumentTypes - + private static final ThreadLocal<Integer> CONSUMED_CHARS = ThreadLocal.withInitial(() -> Integer.valueOf(0)); + //int consumed_chars=0; // Remember position in string, see getArgumentTypes private static int unwrap( final ThreadLocal<Integer> tl ) { return tl.get().intValue(); } - private static void wrap( final ThreadLocal<Integer> tl, final int value ) { tl.set(Integer.valueOf(value)); } - /** * Convert signature to a Type object. * @param signature signature string such as Ljava/lang/String; @@ -196,7 +188,7 @@ public abstract class Type { final byte type = Utility.typeOfSignature(signature); if (type <= Const.T_VOID) { //corrected concurrent private static field acess - wrap(consumed_chars, 1); + wrap(CONSUMED_CHARS, 1); return BasicType.getType(type); } else if (type == Const.T_ARRAY) { int dim = 0; @@ -207,13 +199,13 @@ public abstract class Type { final Type t = getType(signature.substring(dim)); //corrected concurrent private static field acess // consumed_chars += dim; // update counter - is replaced by - final int _temp = unwrap(consumed_chars) + dim; - wrap(consumed_chars, _temp); + final int _temp = unwrap(CONSUMED_CHARS) + dim; + wrap(CONSUMED_CHARS, _temp); return new ArrayType(t, dim); } else { // type == T_REFERENCE // Utility.typeSignatureToString understands how to parse generic types. final String parsedSignature = Utility.typeSignatureToString(signature, false); - wrap(consumed_chars, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed + wrap(CONSUMED_CHARS, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed return ObjectType.getInstance(parsedSignature.replace('/', '.')); } } @@ -254,7 +246,7 @@ public abstract class Type { while (signature.charAt(index) != ')') { vec.add(getType(signature.substring(index))); //corrected concurrent private static field acess - index += unwrap(consumed_chars); // update position + index += unwrap(CONSUMED_CHARS); // update position } } catch (final StringIndexOutOfBoundsException e) { // Should never occur throw new ClassFormatException("Invalid method signature: " + signature, e);