Deprecate the StableAPI annotations, switch to JB annotations

This commit is contained in:
FalsePattern committed 2025-05-26 09:45:57 +02:00
1 parent 8645d20a16
commit df1f294776
96 files changed
+1079 -762

No files matched your search

@@ -22,8 +22,6 @@
package com.falsepattern.lib.turboasm;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.StableAPI.Expose;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -36,42 +34,28 @@ import java.io.IOException;
/**
* Utilities for quickly processing class files without fully parsing them.
*/
@StableAPI(since = "__EXPERIMENTAL__")
public final class ClassHeaderMetadata implements FastClassAccessor {
@Expose
public final int minorVersion;
@Expose
public final int majorVersion;
@Expose
public final int constantPoolEntryCount;
/** Byte offsets of where each constant pool entry starts (index of the tag byte), zero-indexed! */
@Expose
public final int @NotNull [] constantPoolEntryOffsets;
/** Type of each parsed constant pool entry, zero-indexed! */
@Expose
public final ConstantPoolEntryTypes @NotNull [] constantPoolEntryTypes;
@Expose
public final int constantPoolEndOffset;
@Expose
public final int accessFlags;
@Expose
public final int thisClassIndex;
@Expose
public final int superClassIndex;
@Expose
public final int interfacesCount;
@Expose
public final @NotNull String binaryThisName;
@Expose
public final @Nullable String binarySuperName;
/**
* Attempts to parse a class header.
* @param bytes The class bytes to parse.
*/
@Expose
public ClassHeaderMetadata(byte @NotNull [] bytes) {
if (!isValidClass(bytes)) {
throw new IllegalArgumentException("Invalid class detected");
@@ -143,7 +127,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* @param off Offset to the 16-bit length field.
* @return The decoded String.
*/
@Expose
public static @NotNull String modifiedUtf8(byte @NotNull [] arr, int off) {
try (ByteArrayInputStream bais = new ByteArrayInputStream(arr, off, arr.length - off);
DataInputStream dis = new DataInputStream(bais)) {
@@ -174,85 +157,57 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* attribute_info attributes[attributes_count];
* }
*/
@StableAPI(since = "__EXPERIMENTAL__")
public static final class Offsets {
private Offsets() {}
/** The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE */
@Expose
public static final int magicU32 = 0;
/** The values of the minor_version and major_version items are the minor and major version numbers of this class file. Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m */
@Expose
public static final int minorVersionU16 = magicU32 + 4;
/** The values of the minor_version and major_version items are the minor and major version numbers of this class file. Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m */
@Expose
public static final int majorVersionU16 = minorVersionU16 + 2;
/** The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in §4.4.5. */
@Expose
public static final int constantPoolCountU16 = majorVersionU16 + 2;
/**
* The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte.
* <p>
* The constant_pool table is indexed from 1 to constant_pool_count - 1.
*/
@Expose
public static final int constantPoolStart = constantPoolCountU16 + 2;
/**
* The value of the access_flags item is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation of each flag, when set, is specified in Table 4.1-B.
*/
@Expose
public static final int pastCpAccessFlagsU16 = 0;
/**
* The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing the class or interface defined by this class file.
*/
@Expose
public static final int pastCpThisClassU16 = pastCpAccessFlagsU16 + 2;
/**
* For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a CONSTANT_Class_info structure representing the direct superclass of the class defined by this class file. Neither the direct superclass nor any of its superclasses may have the ACC_FINAL flag set in the access_flags item of its ClassFile structure. <p>
* If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass. <p>
* For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.
*/
@Expose
public static final int pastCpSuperClassU16 = pastCpThisClassU16 + 2;
/** The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type */
@Expose
public static final int pastCpInterfacesCountU16 = pastCpSuperClassU16 + 2;
}
@StableAPI(since = "__EXPERIMENTAL__")
public enum ConstantPoolEntryTypes {
@Expose
Utf8(1, 45, -1),
@Expose
Integer(3, 45, 4),
@Expose
Float(4, 45, 4),
@Expose
Long(5, 45, 8),
@Expose
Double(6, 45, 8),
@Expose
Class(7, 49, 2),
@Expose
String(8, 45, 2),
@Expose
FieldRef(9, 45, 4),
@Expose
MethodRef(10, 45, 4),
@Expose
InterfaceMethodRef(11, 45, 4),
@Expose
NameAndType(12, 45, 4),
@Expose
MethodHandle(15, 51, 3),
@Expose
MethodType(16, 51, 2),
@Expose
Dynamic(17, 55, 4),
@Expose
InvokeDynamic(18, 51, 4),
@Expose
Module(19, 53, 2),
@Expose
Package(20, 53, 2),
;
private static final ConstantPoolEntryTypes[] lookup;
@@ -267,10 +222,8 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
lookup = lut;
}
@Expose
public final int tag, minMajorVersion;
/** Length in bytes of the entry (excluding the 1 byte tag), or -1 if length-encoded */
@Expose
public final int maybeByteLength;
ConstantPoolEntryTypes(int tag, int minMajorVersion, int maybeByteLength) {
@@ -284,7 +237,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* @param offset The offset where the entry starts
* @return The total length of the entry, including the tag byte
*/
@Expose
public int byteLength(final byte @NotNull [] classFile, final int offset) {
if (this == ConstantPoolEntryTypes.Utf8) {
return 3 + u16(classFile, offset + 1);
@@ -295,7 +247,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
return 1 + maybeByteLength;
}
@Expose
public static @NotNull ConstantPoolEntryTypes parse(final byte @NotNull [] classFile, final int offset) {
final int tag = u8(classFile, offset);
final ConstantPoolEntryTypes type = tag >= lookup.length ? null : lookup[tag];
@@ -312,7 +263,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* @return If the class passes simple sanity checks.
*/
@Contract("null -> false")
@Expose
public static boolean isValidClass(byte @Nullable [] classBytes) {
if (classBytes == null) {
return false;
@@ -329,7 +279,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* @param classBytes Class data
* @return The major version number of the class file. See {@link org.objectweb.asm.Opcodes#V1_8}
*/
@Expose
public static int majorVersion(byte @NotNull [] classBytes) {
return u16(classBytes, Offsets.majorVersionU16);
}
@@ -340,7 +289,6 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
* @param substring The short substring to search for.
* @return If the substring was found somewhere in the long string.
*/
@Expose
public static boolean hasSubstring(final byte @Nullable [] classBytes, final byte @NotNull [] substring) {
if (classBytes == null) {
return false;
@@ -22,8 +22,6 @@
package com.falsepattern.lib.turboasm;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.StableAPI.Expose;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassReader;
@@ -31,7 +29,6 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
/** A simple handle to a mutable ClassNode and flags for ClassWriter. */
@StableAPI(since = "__EXPERIMENTAL__")
public final class ClassNodeHandle {
private final byte @Nullable [] originalBytes;
private final @Nullable ClassHeaderMetadata originalMetadata;
@@ -42,13 +39,11 @@ public final class ClassNodeHandle {
private int writerFlags = 0;
/** Parse the class data with no reader options (for fastest speed). */
@Expose
public ClassNodeHandle(byte @Nullable [] classData) {
this(classData, 0);
}
/** Parse the class data with custom reader options. */
@Expose
public ClassNodeHandle(
byte @Nullable [] classData, @MagicConstant(flagsFromClass = ClassReader.class) int readerOptions) {
@Nullable ClassHeaderMetadata originalMetadata;
@@ -68,31 +63,26 @@ public final class ClassNodeHandle {
}
/** Gets the original pre-transformer-phase bytes of the class. */
@Expose
public byte @Nullable [] getOriginalBytes() {
return originalBytes;
}
/** Gets the original pre-transformer-phase header metadata of the class, or null if invalid/not present. */
@Expose
public @Nullable ClassHeaderMetadata getOriginalMetadata() {
return originalMetadata;
}
/** Gets the fast class metadata accessor of the class, that can access the current state of various class attributes without (re)parsing. */
@Expose
public @Nullable FastClassAccessor getFastAccessor() {
return accessor;
}
/** @return If the class was not yet turned into a ClassNode object, and the original bytes still represent the class. */
@Expose
public boolean isOriginal() {
return !initialized;
}
/** If the class currently has any bytes or a node associated with it. */
@Expose
public boolean isPresent() {
if (initialized) {
return node != null;
@@ -102,14 +92,12 @@ public final class ClassNodeHandle {
}
/** Gets the parsed node of the currently processed class. This can cause full class parsing! */
@Expose
public @Nullable ClassNode getNode() {
ensureInitialized();
return node;
}
/** Overwrites the parsed node of the currently processed class. */
@Expose
public void setNode(@Nullable ClassNode node) {
initialized = true;
this.node = node;
@@ -121,7 +109,6 @@ public final class ClassNodeHandle {
}
/** Computes the byte[] array of the transformed class. Returns the original bytes if {@link ClassNodeHandle#getNode()} was never called. */
@Expose
public byte @Nullable [] computeBytes() {
if (!initialized) {
return originalBytes;
@@ -135,31 +122,26 @@ public final class ClassNodeHandle {
}
/** Gets the ClassWriter flags for the current class. */
@Expose
public int getWriterFlags() {
return writerFlags;
}
/** Set the ClassWriter flags for the current class. */
@Expose
public void setWriterFlags(@MagicConstant(flagsFromClass = ClassWriter.class) int flags) {
this.writerFlags = flags;
}
/** Combine the currently set writer flags with the given flags using bitwise OR. */
@Expose
public void orWriterFlags(@MagicConstant(flagsFromClass = ClassWriter.class) int flags) {
this.writerFlags |= flags;
}
/** Set ClassWriter.COMPUTE_MAXS on the writer flags. */
@Expose
public void computeMaxs() {
this.writerFlags |= ClassWriter.COMPUTE_MAXS;
}
/** Set ClassWriter.COMPUTE_FRAMES on the writer flags. */
@Expose
public void computeFrames() {
this.writerFlags |= ClassWriter.COMPUTE_FRAMES;
}
@@ -22,8 +22,6 @@
package com.falsepattern.lib.turboasm;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.StableAPI.Expose;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Opcodes;
@@ -32,49 +30,36 @@ import org.objectweb.asm.tree.ClassNode;
import java.lang.reflect.Modifier;
/** An accessor to metadata about a class that is quickly accessible without fully parsing one. */
@StableAPI(since = "__EXPERIMENTAL__")
public interface FastClassAccessor {
/** Accessible from outside its package */
@Expose
boolean isPublic();
/** No subclasses allowed */
@Expose
boolean isFinal();
/** Is an interface instead of a class */
@Expose
boolean isInterface();
/** Is an abstract class that should not be instantiated */
@Expose
boolean isAbstract();
/** Is not present in source code (often used by obfuscated code too) */
@Expose
boolean isSynthetic();
/** Is an annotation interface */
@Expose
boolean isAnnotation();
/** Is an enum class */
@Expose
boolean isEnum();
/** Binary (slash-separated packages) name of the class */
@NotNull
@Expose
String binaryThisName();
/** Binary (slash-separated packages) name of the super-class, null for the Object class */
@Nullable
@Expose
String binarySuperName();
@Expose
static OfLoaded ofLoaded(Class<?> loadedClass) {
return new OfLoaded(loadedClass);
}
@Expose
static OfAsmNode ofAsmNode(ClassNode handle) {
return new OfAsmNode(handle);
}
@StableAPI(since = "__EXPERIMENTAL__")
final class OfAsmNode implements FastClassAccessor {
public final ClassNode handle;
@@ -128,7 +113,6 @@ public interface FastClassAccessor {
}
}
@StableAPI(since = "__EXPERIMENTAL__")
final class OfLoaded implements FastClassAccessor {
public final Class<?> handle;
@@ -21,7 +21,6 @@
*/
package com.falsepattern.lib.turboasm;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.asm.CoreLoadingPlugin;
import lombok.val;
@@ -30,7 +29,6 @@ import net.minecraft.launchwrapper.IClassTransformer;
import java.util.ArrayList;
import java.util.List;
@StableAPI(since = "__EXPERIMENTAL__")
public class MergeableTurboTransformer implements IClassTransformer {
private final List<TurboClassTransformer> transformers;
@@ -26,9 +26,11 @@ import com.falsepattern.lib.internal.Tags;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
import java.util.List;
@ApiStatus.Internal
public class TransformerUtil {
private static final boolean DEBUG_VERBOSE_TRANSFORMERS = Boolean.parseBoolean(System.getProperty(Tags.MODID + ".debug.verboseTransformers", "false"));
private static final Logger LOG = LogManager.getLogger("ASM");
@@ -22,26 +22,21 @@
package com.falsepattern.lib.turboasm;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.StableAPI.Expose;
import org.jetbrains.annotations.NotNull;
/**
* A simple transformer that takes in class bytes and outputs different class bytes.
* It should be thread-safe, and not change class names. It should also have a public no-arguments constructor.
*/
@StableAPI(since = "__EXPERIMENTAL__")
public interface TurboClassTransformer {
/**
* @return The user-friendly owner name of this transformer. Usually a mod id.
*/
@Expose
String owner();
/**
* @return The user-friendly name of this transformer. Used in logs.
*/
@Expose
String name();
/**
@@ -50,7 +45,6 @@ public interface TurboClassTransformer {
* @param classNode The handle to the class data and parsed metadata, try to avoid triggering the lazy ASM parse if possible for performance.
* @return true if the class will be transformed by this class transformer.
*/
@Expose
boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode);
/**
@@ -60,6 +54,5 @@ public interface TurboClassTransformer {
* @return True if the class has been modified in any way by this transformer. If all transformers return false,
* then the ClassNode instance will not be re-serialized.
*/
@Expose
boolean transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode);
}
@@ -0,0 +1,25 @@
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
@ApiStatus.Experimental
package com.falsepattern.lib.turboasm;
import org.jetbrains.annotations.ApiStatus;