![JAR search and dependency download from the Maven repository](/logo.png)
com.github.romanqed.jeflect.fields.FieldAccessorFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jeflect Show documentation
Show all versions of jeflect Show documentation
A set of utilities designed to interact with reflection and speed up it.
package com.github.romanqed.jeflect.fields;
import com.github.romanqed.jeflect.DefineClassLoader;
import com.github.romanqed.jeflect.DefineLoader;
import com.github.romanqed.jeflect.DefineObjectFactory;
import com.github.romanqed.jeflect.ObjectFactory;
import java.lang.reflect.Field;
import java.util.Objects;
/**
* A class representing a factory that creates
* {@link FieldAccessor} instances for subsequent access to the field.
* Access occurs at almost native speed, minus the time to call the proxy class method.
*/
public final class FieldAccessorFactory {
private static final String ACCESSOR = "Accessor";
private final ObjectFactory factory;
public FieldAccessorFactory(ObjectFactory factory) {
this.factory = Objects.requireNonNull(factory);
}
public FieldAccessorFactory(DefineLoader loader) {
this(new DefineObjectFactory<>(loader));
}
public FieldAccessorFactory() {
this(new DefineClassLoader());
}
/**
* Creates a proxy implementation of the {@link FieldAccessor} interface for the specified field.
*
* @param field the target field
* @return object of the generated proxy class implementing the {@link FieldAccessor} interface
*/
public FieldAccessor packField(Field field) {
var toHash = field.getDeclaringClass().getName() + field.getName();
var name = ACCESSOR + toHash.hashCode();
return factory.create(name, () -> FieldUtil.createAccessor(name, field));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy