Class StructureModifier<TField>
- Type Parameters:
TField- Type of the fields to retrieve.
- Direct Known Subclasses:
CompiledStructureModifier
Implemented by using reflection. Use a CompiledStructureModifier, if speed is essential.
- Author:
- Kristian
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected EquivalentConverter<TField>protected booleanprotected Classprotected Map<Class,StructureModifier>protected Objectprotected Classprotected boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConsumers of this method should call "initialize".StructureModifier(Class targetType)Creates a structure modifier.StructureModifier(Class targetType, boolean useStructureCompiler)Creates a structure modifier.StructureModifier(Class targetType, Class superclassExclude, boolean requireDefault)Creates a structure modifier.StructureModifier(Class targetType, Class superclassExclude, boolean requireDefault, boolean useStructureCompiler)Creates a structure modifier. -
Method Summary
Modifier and TypeMethodDescriptiongetField(int fieldIndex)Retrieve a field by index.Retrieves a list of the fields matching the constraints of this structure modifier.Retrieves the common type of each field.protected Class<?>getFieldType(int index)Retrieve the type of a specified field.Retrieves the object we're currently modifying.Retrieves the type of the object we're modifying.Retrieve every value stored in the fields of the current type.protected voidinitialize(StructureModifier<TField> other)Initialize using the same field types.protected voidinitialize(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<Class,StructureModifier> subTypeCache)Initialize every field of this class.protected voidinitialize(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<Class,StructureModifier> subTypeCache, boolean useStructureCompiler)Initialize every field of this class.booleanisPublic(int fieldIndex)Determine if a given field is public or not.booleanisReadOnly(int fieldIndex)Determine whether or not a field is read-only (final).Correctly modifies the value of a field.optionRead(int fieldIndex)Reads the value of a field only if it exists.read(int fieldIndex)Reads the value of a field given its index.readSafely(int fieldIndex)Reads the value of a field only if it exists.protected voidsetConverter(EquivalentConverter<TField> converter)Set the current object converter.protected static voidsetFinalState(Field field, boolean isReadOnly)Alter the final status of a field.voidsetReadOnly(int fieldIndex, boolean value)Set whether or not a field should be treated as read only.intsize()Retrieve the number of readable types.toString()protected <T> StructureModifier<T>Create a new structure modifier for the new field type.protected <T> StructureModifier<T>withFieldType(Class fieldType, List<Field> filtered, Map<Field,Integer> defaults, EquivalentConverter<T> converter)Create a new structure modifier for the new field type.<T> StructureModifier<T>withParamType(Class fieldType, EquivalentConverter<T> converter, Class... paramTypes)Retrieves a structure modifier that only reads and writes fields of a given type.withTarget(Object target)Retrieves a structure modifier of the same type for a different object target.<T> StructureModifier<T>Retrieves a structure modifier that only reads and writes fields of a given type.<T> StructureModifier<T>withType(Class fieldType, EquivalentConverter<T> converter)Retrieves a structure modifier that only reads and writes fields of a given type.Writes the value of a field given its index.Sets all non-primitive fields to a more fitting default value.writeSafely(int fieldIndex, TField value)Writes the value of a given field IF and ONLY if it exists.
-
Field Details
-
targetType
-
target
-
converter
-
fieldType
-
data
-
defaultFields
-
subtypeCache
-
customConvertHandling
protected boolean customConvertHandling -
useStructureCompiler
protected boolean useStructureCompiler
-
-
Constructor Details
-
StructureModifier
Creates a structure modifier.- Parameters:
targetType- - the structure to modify.
-
StructureModifier
Creates a structure modifier.- Parameters:
targetType- - the structure to modify.useStructureCompiler- - whether or not to use a structure compiler.
-
StructureModifier
Creates a structure modifier.- Parameters:
targetType- - the structure to modify.superclassExclude- - a superclass to exclude.requireDefault- - whether or not we will be using writeDefaults().
-
StructureModifier
public StructureModifier(Class targetType, Class superclassExclude, boolean requireDefault, boolean useStructureCompiler)Creates a structure modifier.- Parameters:
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.
-
StructureModifier
protected StructureModifier()Consumers of this method should call "initialize".
-
-
Method Details
-
initialize
Initialize using the same field types.- Parameters:
other- - information to set.
-
initialize
protected void initialize(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<Class,StructureModifier> subTypeCache)Initialize every field of this class.- Parameters:
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.
-
initialize
protected void initialize(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<Class,StructureModifier> subTypeCache, boolean useStructureCompiler)Initialize every field of this class.- Parameters:
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.
-
read
Reads the value of a field given its index.Note: This method is prone to exceptions (there are currently 5 total throw statements). It is recommended that you use
readSafely(int), which returnsnullif the field doesn't exist, instead of throwing an exception.- Parameters:
fieldIndex- - index of the field.- Returns:
- Value of the field.
- Throws:
FieldAccessException- if the field doesn't exist, or it cannot be accessed under the current security contraints.
-
readSafely
Reads the value of a field only if it exists. If the field does not exist,nullis returned.As its name implies, this method is a much safer alternative to
read(int). In addition to throwing less exceptions and thereby causing less console spam, this method makes providing backwards compatiblity signficiantly easier, as shown below:BlockPosition position = packet.getBlockPositionModifier().readSafely(0); if (position != null) { // Handle 1.8+ } else { // Handle 1.7- }- Parameters:
fieldIndex- - index of the field.- Returns:
- Value of the field, or NULL if it doesn't exist.
- Throws:
FieldAccessException- if the field cannot be accessed under the current security constraints.
-
optionRead
Reads the value of a field only if it exists. If the field does not exist, an emptyOptionalis returned.This method has the same functionality as
readSafely(int), but enforces null checks by way of an Optional. It will eventually become the preferred method of reading fields.- Parameters:
fieldIndex- index of the field- Returns:
- An optional that may contain the value of the field
- See Also:
readSafely(int)
-
isReadOnly
public boolean isReadOnly(int fieldIndex)Determine whether or not a field is read-only (final).- Parameters:
fieldIndex- - index of the field.- Returns:
- TRUE if the field by the given index is read-only, FALSE otherwise.
-
isPublic
public boolean isPublic(int fieldIndex)Determine if a given field is public or not.- Parameters:
fieldIndex- - field index.- Returns:
- TRUE if the field is public, FALSE otherwise.
-
setReadOnly
Set whether or not a field should be treated as read only.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.
- Parameters:
fieldIndex- - index of the field.value- - TRUE if this field should be read only, FALSE otherwise.- Throws:
FieldAccessException- If we cannot modify the read-only status.
-
setFinalState
Alter the final status of a field.- Parameters:
field- - the field to change.isReadOnly- - TRUE if the field should be read only, FALSE otherwise.- Throws:
IllegalAccessException- If an error occured.
-
write
Writes the value of a field given its index.- Parameters:
fieldIndex- - index of the field.value- - new value of the field.- Returns:
- This structure modifier - for chaining.
- Throws:
FieldAccessException- The field doesn't exist, or it cannot be accessed under the current security contraints.
-
getFieldType
Retrieve the type of a specified field.- Parameters:
index- - the index.- Returns:
- The type of the given field.
-
writeSafely
public StructureModifier<TField> writeSafely(int fieldIndex, TField value) throws FieldAccessExceptionWrites the value of a given field IF and ONLY if it exists.- Parameters:
fieldIndex- - index of the potential field.value- - new value of the field.- Returns:
- This structure modifer - for chaining.
- Throws:
FieldAccessException- The field cannot be accessed under the current security contraints.
-
modify
public StructureModifier<TField> modify(int fieldIndex, com.google.common.base.Function<TField,TField> select) throws FieldAccessExceptionCorrectly modifies the value of a field.- Parameters:
fieldIndex- - index of the field to modify.select- - the function that modifies the field value.- Returns:
- This structure modifier - for chaining.
- Throws:
FieldAccessException- The field cannot be accessed under the current security contraints.
-
withType
Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
T- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.- Returns:
- A structure modifier for fields of this type.
-
writeDefaults
Sets all non-primitive fields to a more fitting default value. SeeDefaultInstances.getDefault(Class).- Returns:
- The current structure modifier - for chaining.
- Throws:
FieldAccessException- If we're unable to write to the fields due to a security limitation.
-
withType
Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
T- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.converter- - converts objects into the given type.- Returns:
- A structure modifier for fields of this type.
-
withParamType
public <T> StructureModifier<T> withParamType(Class fieldType, EquivalentConverter<T> converter, Class... paramTypes)Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
T- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.converter- - converts objects into the given type.paramTypes- - field type parameters- Returns:
- A structure modifier for fields of this type.
-
getFieldType
Retrieves the common type of each field.- Returns:
- Common type of each field.
-
getTargetType
Retrieves the type of the object we're modifying.- Returns:
- Type of the object.
-
getTarget
Retrieves the object we're currently modifying.- Returns:
- Object we're modifying.
-
size
public int size()Retrieve the number of readable types.- Returns:
- Readable types.
-
withFieldType
protected <T> StructureModifier<T> withFieldType(Class fieldType, List<Field> filtered, Map<Field,Integer> defaults)Create a new structure modifier for the new field type.- Type Parameters:
T- Type- Parameters:
fieldType- - common type of each field.filtered- - list of fields after filtering the original modifier.defaults- - list of default values after filtering the original.- Returns:
- A new structure modifier.
-
withFieldType
protected <T> StructureModifier<T> withFieldType(Class fieldType, List<Field> filtered, Map<Field,Integer> defaults, EquivalentConverter<T> converter)Create a new structure modifier for the new field type.- Type Parameters:
T- Type- Parameters:
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, or NULL.- Returns:
- A new structure modifier.
-
withTarget
Retrieves a structure modifier of the same type for a different object target.- Parameters:
target- - different target of the same type.- Returns:
- Structure modifier with the new target.
-
setConverter
Set the current object converter. Should only be called during construction.- Parameters:
converter- - current object converter.
-
getFields
Retrieves a list of the fields matching the constraints of this structure modifier.- Returns:
- List of fields.
-
getField
Retrieve a field by index.- Parameters:
fieldIndex- - index of the field to retrieve.- Returns:
- The field represented with the given index.
- Throws:
IllegalArgumentException- If no field with the given index can be found.
-
getValues
Retrieve every value stored in the fields of the current type.- Returns:
- Every field value.
- Throws:
FieldAccessException- Unable to access one or all of the fields
-
toString
-