TField
- Type of the fields to retrieve.public class StructureModifier<TField>
extends java.lang.Object
Implemented by using reflection. Use a CompiledStructureModifier, if speed is essential.
Modifier and Type | Field and Description |
---|---|
protected EquivalentConverter<TField> |
converter |
protected boolean |
customConvertHandling |
protected java.util.List<java.lang.reflect.Field> |
data |
protected java.util.Map<java.lang.reflect.Field,java.lang.Integer> |
defaultFields |
protected java.lang.Class |
fieldType |
protected java.util.Map<java.lang.Class,StructureModifier> |
subtypeCache |
protected java.lang.Object |
target |
protected java.lang.Class |
targetType |
protected boolean |
useStructureCompiler |
Modifier | Constructor and Description |
---|---|
protected |
StructureModifier()
Consumers of this method should call "initialize".
|
|
StructureModifier(java.lang.Class targetType)
Creates a structure modifier.
|
|
StructureModifier(java.lang.Class targetType,
java.lang.Class superclassExclude,
boolean requireDefault)
Creates a structure modifier.
|
|
StructureModifier(java.lang.Class targetType,
java.lang.Class superclassExclude,
boolean requireDefault,
boolean useStructureCompiler)
Creates a structure modifier.
|
Modifier and Type | Method and Description |
---|---|
java.lang.reflect.Field |
getField(int fieldIndex)
Retrieve a field by index.
|
java.util.List<java.lang.reflect.Field> |
getFields()
Retrieves a list of the fields matching the constraints of this structure modifier.
|
java.lang.Class |
getFieldType()
Retrieves the common type of each field.
|
protected java.lang.Class<?> |
getFieldType(int index)
Retrieve the type of a specified field.
|
java.lang.Object |
getTarget()
Retrieves the object we're currently modifying.
|
java.lang.Class |
getTargetType()
Retrieves the type of the object we're modifying.
|
java.util.List<TField> |
getValues()
Retrieve every value stored in the fields of the current type.
|
protected void |
initialize(java.lang.Class targetType,
java.lang.Class fieldType,
java.util.List<java.lang.reflect.Field> data,
java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaultFields,
EquivalentConverter<TField> converter,
java.util.Map<java.lang.Class,StructureModifier> subTypeCache)
Initialize every field of this class.
|
protected void |
initialize(java.lang.Class targetType,
java.lang.Class fieldType,
java.util.List<java.lang.reflect.Field> data,
java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaultFields,
EquivalentConverter<TField> converter,
java.util.Map<java.lang.Class,StructureModifier> subTypeCache,
boolean useStructureCompiler)
Initialize every field of this class.
|
protected void |
initialize(StructureModifier<TField> other)
Initialize using the same field types.
|
boolean |
isPublic(int fieldIndex)
Determine if a given field is public or not.
|
boolean |
isReadOnly(int fieldIndex)
Determine whether or not a field is read-only (final).
|
StructureModifier<TField> |
modify(int fieldIndex,
com.google.common.base.Function<TField,TField> select)
Correctly modifies the value of a field.
|
TField |
read(int fieldIndex)
Reads the value of a field given its index.
|
TField |
readSafely(int fieldIndex)
Reads the value of a field if and ONLY IF it exists.
|
protected void |
setConverter(EquivalentConverter<TField> converter)
Set the current object converter.
|
protected static void |
setFinalState(java.lang.reflect.Field field,
boolean isReadOnly)
Alter the final status of a field.
|
void |
setReadOnly(int fieldIndex,
boolean value)
Set whether or not a field should be treated as read only.
|
int |
size()
Retrieve the number of readable types.
|
protected <T> StructureModifier<T> |
withFieldType(java.lang.Class fieldType,
java.util.List<java.lang.reflect.Field> filtered,
java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaults,
EquivalentConverter<T> converter)
Create a new structure modifier for the new field type.
|
StructureModifier<TField> |
withTarget(java.lang.Object target)
Retrieves a structure modifier of the same type for a different object target.
|
<T> StructureModifier<T> |
withType(java.lang.Class fieldType)
Retrieves a structure modifier that only reads and writes fields of a given type.
|
<T> StructureModifier<T> |
withType(java.lang.Class fieldType,
EquivalentConverter<T> converter)
Retrieves a structure modifier that only reads and writes fields of a given type.
|
StructureModifier<TField> |
write(int fieldIndex,
TField value)
Writes the value of a field given its index.
|
StructureModifier<TField> |
writeDefaults()
Sets all non-primitive fields to a more fitting default value.
|
StructureModifier<TField> |
writeSafely(int fieldIndex,
TField value)
Writes the value of a given field IF and ONLY if it exists.
|
protected java.lang.Class targetType
protected java.lang.Object target
protected EquivalentConverter<TField> converter
protected java.lang.Class fieldType
protected java.util.List<java.lang.reflect.Field> data
protected java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaultFields
protected java.util.Map<java.lang.Class,StructureModifier> subtypeCache
protected boolean customConvertHandling
protected boolean useStructureCompiler
public StructureModifier(java.lang.Class targetType)
targetType
- - the structure to modify.public StructureModifier(java.lang.Class targetType, java.lang.Class superclassExclude, boolean requireDefault)
targetType
- - the structure to modify.superclassExclude
- - a superclass to exclude.requireDefault
- - whether or not we will be using writeDefaults().public StructureModifier(java.lang.Class targetType, java.lang.Class superclassExclude, boolean requireDefault, boolean useStructureCompiler)
targetType
- - the structure to modify.superclassExclude
- - a superclass to exclude.requireDefault
- - whether or not we will be using writeDefaults().useStructureCompiler
- - whether or not to automatically compile this structure modifier.protected StructureModifier()
protected void initialize(StructureModifier<TField> other)
other
- - information to set.protected void initialize(java.lang.Class targetType, java.lang.Class fieldType, java.util.List<java.lang.reflect.Field> data, java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaultFields, EquivalentConverter<TField> converter, java.util.Map<java.lang.Class,StructureModifier> subTypeCache)
targetType
- - type of the object we're reading and writing from.fieldType
- - the common type of the fields we're modifying.data
- - list of fields to modify.defaultFields
- - list of fields that will be automatically initialized.converter
- - converts between the common field type and the actual type the consumer expects.subTypeCache
- - a structure modifier cache.protected void initialize(java.lang.Class targetType, java.lang.Class fieldType, java.util.List<java.lang.reflect.Field> data, java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaultFields, EquivalentConverter<TField> converter, java.util.Map<java.lang.Class,StructureModifier> subTypeCache, boolean useStructureCompiler)
targetType
- - type of the object we're reading and writing from.fieldType
- - the common type of the fields we're modifying.data
- - list of fields to modify.defaultFields
- - list of fields that will be automatically initialized.converter
- - converts between the common field type and the actual type the consumer expects.subTypeCache
- - a structure modifier cache.useStructureCompiler
- - whether or not to automatically compile this structure modifier.public TField read(int fieldIndex) throws FieldAccessException
fieldIndex
- - index of the field.FieldAccessException
- The field doesn't exist, or it cannot be accessed under the current security contraints.public TField readSafely(int fieldIndex) throws FieldAccessException
fieldIndex
- - index of the field.FieldAccessException
- The field cannot be accessed under the current security contraints.public boolean isReadOnly(int fieldIndex)
fieldIndex
- - index of the field.public boolean isPublic(int fieldIndex)
fieldIndex
- - field index.public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessException
Note that changing the read-only state to TRUE will only work if the current field was recently read-only or the current structure modifier hasn't been compiled yet.
fieldIndex
- - index of the field.value
- - TRUE if this field should be read only, FALSE otherwise.FieldAccessException
- If we cannot modify the read-only status.protected static void setFinalState(java.lang.reflect.Field field, boolean isReadOnly) throws java.lang.IllegalAccessException
field
- - the field to change.isReadOnly
- - TRUE if the field should be read only, FALSE otherwise.java.lang.IllegalAccessException
- If an error occured.public StructureModifier<TField> write(int fieldIndex, TField value) throws FieldAccessException
fieldIndex
- - index of the field.value
- - new value of the field.FieldAccessException
- The field doesn't exist, or it cannot be accessed under the current security contraints.protected java.lang.Class<?> getFieldType(int index)
index
- - the index.public StructureModifier<TField> writeSafely(int fieldIndex, TField value) throws FieldAccessException
fieldIndex
- - index of the potential field.value
- - new value of the field.FieldAccessException
- The field cannot be accessed under the current security contraints.public StructureModifier<TField> modify(int fieldIndex, com.google.common.base.Function<TField,TField> select) throws FieldAccessException
fieldIndex
- - index of the field to modify.select
- - the function that modifies the field value.FieldAccessException
- The field cannot be accessed under the current security contraints.public <T> StructureModifier<T> withType(java.lang.Class fieldType)
fieldType
- - the type, or supertype, of every field to modify.public StructureModifier<TField> writeDefaults() throws FieldAccessException
DefaultInstances.getDefault(Class)
.FieldAccessException
- If we're unable to write to the fields due to a security limitation.public <T> StructureModifier<T> withType(java.lang.Class fieldType, EquivalentConverter<T> converter)
fieldType
- - the type, or supertype, of every field to modify.converter
- - converts objects into the given type.public java.lang.Class getFieldType()
public java.lang.Class getTargetType()
public java.lang.Object getTarget()
public int size()
protected <T> StructureModifier<T> withFieldType(java.lang.Class fieldType, java.util.List<java.lang.reflect.Field> filtered, java.util.Map<java.lang.reflect.Field,java.lang.Integer> defaults, EquivalentConverter<T> converter)
fieldType
- - common type of each field.filtered
- - list of fields after filtering the original modifier.defaults
- - list of default values after filtering the original.converter
- - the new converter.public StructureModifier<TField> withTarget(java.lang.Object target)
target
- - different target of the same type.protected void setConverter(EquivalentConverter<TField> converter)
converter
- - current object converter.public java.util.List<java.lang.reflect.Field> getFields()
public java.lang.reflect.Field getField(int fieldIndex)
fieldIndex
- - index of the field to retrieve.java.lang.IllegalArgumentException
- If no field with the given index can be found.public java.util.List<TField> getValues() throws FieldAccessException
FieldAccessException
- Unable to access one or all of the fields