Class FieldHandles

java.lang.Object
com.kamikazejam.kamicommon.nms.reflection.FieldHandles

public class FieldHandles extends Object
Factory and cache manager for FieldHandle instances.

This utility class provides a centralized way to create and cache FieldHandle instances to avoid the overhead of repeated reflection operations. Each unique combination of field name and class is cached, so subsequent requests for the same field handle return the cached instance.

The caching is based on a serialized key that combines the field name and the fully qualified class name, ensuring that field handles for fields with the same name in different classes are properly distinguished.

Example usage:


 // Get a cached field handle
 FieldHandle<?> handle = FieldHandles.getHandle("myField", MyClass.class);
 
 // Use the handle
 Object value = handle.get(someInstance);
 handle.set(someInstance, newValue);
 

  • Constructor Details

    • FieldHandles

      public FieldHandles()
  • Method Details

    • getHandle

      public static FieldHandle<?> getHandle(String fieldName, Class<?> clazz)
      Retrieves a cached FieldHandle or creates a new one if not cached.

      This method first checks the cache for an existing field handle with the specified field name and class. If found, the cached instance is returned. If not found, a new FieldHandle is created, cached, and returned.

      This approach ensures that the reflection setup cost is only paid once per unique field, improving performance for repeated field access operations.

      Parameters:
      fieldName - the name of the field to access
      clazz - the Class containing the field
      Returns:
      a FieldHandle for the specified field, either from cache or newly created
      Throws:
      RuntimeException - if the field cannot be found in the specified class (thrown by FieldHandle constructor)