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 |
Modifier | Constructor and Description |
---|---|
protected |
StructureModifier()
Consumers of this method should call "initialize".
|
|
StructureModifier(java.lang.Class targetType,
java.lang.Class superclassExclude,
boolean requireDefault)
Creates a structure modifier.
|
Modifier and Type | Method and Description |
---|---|
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.
|
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(StructureModifier<TField> other)
Initialize using the same field types.
|
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.
|
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
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().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.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 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.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.util.List<TField> getValues() throws FieldAccessException
FieldAccessException
- Unable to access one or all of the fields