Skip to content

Commit

Permalink
refactor friendlyClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Aug 24, 2024
1 parent f7c8648 commit 9a70a5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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;
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9a70a5b

Please sign in to comment.