From 9a70a5b70fe27eee30d3db76a6ba84c8cb946cad Mon Sep 17 00:00:00 2001 From: Evan Saulpaugh Date: Sat, 24 Aug 2024 01:52:34 -0500 Subject: [PATCH] refactor friendlyClassName --- src/main/java/com/esaulpaugh/headlong/abi/ABIType.java | 10 +--------- .../java/com/esaulpaugh/headlong/abi/ArrayType.java | 9 ++++++++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/esaulpaugh/headlong/abi/ABIType.java b/src/main/java/com/esaulpaugh/headlong/abi/ABIType.java index 7af00a3f..88392a82 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/ABIType.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/ABIType.java @@ -23,7 +23,6 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.function.IntFunction; -import java.util.regex.Pattern; import static com.esaulpaugh.headlong.abi.UnitType.UNIT_LENGTH_BYTES; @@ -161,7 +160,7 @@ final void validateClass(J value) { } throw mismatchErr("class", value.getClass().getName(), clazz.getName(), - friendlyClassName(clazz, -1), friendlyClassName(value.getClass(), -1)); + clazz.getSimpleName(), value.getClass().getSimpleName()); } } @@ -362,11 +361,4 @@ static void appendPadded(final int leftPadding, String unpadded, StringBuilder s sb.append(' '); } } - - static String friendlyClassName(Class clazz, int arrayLen) { - final String simple = clazz.getSimpleName(); - return arrayLen > 0 && simple.indexOf('[') > 0 - ? simple.replaceFirst(Pattern.quote("[]"), "[" + arrayLen + "]") - : simple; - } } diff --git a/src/main/java/com/esaulpaugh/headlong/abi/ArrayType.java b/src/main/java/com/esaulpaugh/headlong/abi/ArrayType.java index 564b29f1..a1c6f27c 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/ArrayType.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/ArrayType.java @@ -21,6 +21,7 @@ import java.lang.reflect.Array; import java.nio.ByteBuffer; import java.util.function.IntUnaryOperator; +import java.util.regex.Pattern; import static com.esaulpaugh.headlong.abi.TupleType.countBytes; import static com.esaulpaugh.headlong.abi.TupleType.totalLen; @@ -226,12 +227,18 @@ private int measureByteLengthPacked(E[] arr) { // don't count offsets private int checkLength(final int valueLen, Object value) { if (length != DYNAMIC_LENGTH && length != valueLen) { throw mismatchErr("array length", - friendlyClassName(value.getClass(), valueLen), friendlyClassName(clazz, length), + simpleName(value.getClass(), valueLen), simpleName(clazz, length), "length " + length, "" + valueLen); } return valueLen; } + private static final Pattern BRACKETS = Pattern.compile(Pattern.quote("[]")); + + private static String simpleName(Class clazz, int arrayLen) { + return BRACKETS.matcher(clazz.getSimpleName()).replaceFirst("[" + arrayLen + "]"); + } + @SuppressWarnings("unchecked") @Override void encodeTail(A value, ByteBuffer dest) {