Prevent resizing the ClassConstantPoolParser during runtime (#288)

This commit is contained in:
Alexdoru authored and GitHub committed 2026-02-24 16:01:40 +01:00
1 parent 54c0d41663
commit 30b25eb3db
1 file changed
+8 -11
@@ -23,7 +23,6 @@
package com.gtnewhorizon.gtnhlib.asm;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* Using this class to search for a (single) String reference is > 40 times faster than parsing a class with a
@@ -32,7 +31,7 @@ import java.util.Arrays;
// This class might be loaded by different class loaders,
// it should not reference any code from the main mod.
// See {@link com.gtnewhorizon.gtnhlib.core.shared.package-info}
public class ClassConstantPoolParser {
public final class ClassConstantPoolParser {
enum ConstantTypes {
@@ -69,13 +68,16 @@ public class ClassConstantPoolParser {
//spotless:on
static ConstantTypes toType(byte code) {
var ret = MAP[Byte.toUnsignedInt(code)];
ConstantTypes ret = MAP[Byte.toUnsignedInt(code)];
if (ret == INVALID) throw new RuntimeException("Invalid constant type: " + code);
return ret;
}
}
private byte[][] BYTES_TO_SEARCH;
// the strings to search should not be resized
// during runtime to avoid becoming less performant
// than using ASM directly
private final byte[][] BYTES_TO_SEARCH;
public ClassConstantPoolParser(String... strings) {
BYTES_TO_SEARCH = new byte[strings.length][];
@@ -84,11 +86,6 @@ public class ClassConstantPoolParser {
}
}
public void addString(String string) {
BYTES_TO_SEARCH = Arrays.copyOf(BYTES_TO_SEARCH, BYTES_TO_SEARCH.length + 1);
BYTES_TO_SEARCH[BYTES_TO_SEARCH.length - 1] = string.getBytes(StandardCharsets.UTF_8);
}
/**
* Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are
* looking for
@@ -110,8 +107,8 @@ public class ClassConstantPoolParser {
}
// checks the class version
final var maxSupported = 69; // Java 25
var major = readShort(6, basicClass);
final int maxSupported = 69; // Java 25
short major = readShort(6, basicClass);
if (major > maxSupported || (major == maxSupported && readShort(4, basicClass) > 0)) {
return false;
}