Class FieldHandles
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FieldHandle
<?> Retrieves a cachedFieldHandle
or creates a new one if not cached.
-
Constructor Details
-
FieldHandles
public FieldHandles()
-
-
Method Details
-
getHandle
Retrieves a cachedFieldHandle
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 accessclazz
- theClass
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 byFieldHandle
constructor)
-