From 30b25eb3db9b29bc9996ea0095e6bf7b6189a04d Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Tue, 24 Feb 2026 16:01:40 +0100 Subject: [PATCH] Prevent resizing the ClassConstantPoolParser during runtime (#288) --- .../gtnhlib/asm/ClassConstantPoolParser.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java index 47446ab3..5ce7f5b0 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/asm/ClassConstantPoolParser.java @@ -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; }