TField - Type of the fields to retrieve.public class StructureModifier<TField> extends 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 List<Field> |
data |
protected Map<Field,Integer> |
defaultFields |
protected Class |
fieldType |
protected Map<Class,StructureModifier> |
subtypeCache |
protected Object |
target |
protected Class |
targetType |
protected boolean |
useStructureCompiler |
| Modifier | Constructor and Description |
|---|---|
protected |
StructureModifier()
Consumers 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.
|
| Modifier and Type | Method and Description |
|---|---|
Field |
getField(int fieldIndex)
Retrieve a field by index.
|
List<Field> |
getFields()
Retrieves a list of the fields matching the constraints of this structure modifier.
|
Class |
getFieldType()
Retrieves the common type of each field.
|
protected Class<?> |
getFieldType(int index)
Retrieve the type of a specified field.
|
Object |
getTarget()
Retrieves the object we're currently modifying.
|
Class |
getTargetType()
Retrieves the type of the object we're modifying.
|
List<TField> |
getValues()
Retrieve every value stored in the fields of the current type.
|
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.
|
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.
|
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 only if it exists.
|
protected void |
setConverter(EquivalentConverter<TField> converter)
Set the current object converter.
|
protected static void |
setFinalState(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.
|
String |
toString() |
protected <T> StructureModifier<T> |
withFieldType(Class fieldType,
List<Field> filtered,
Map<Field,Integer> defaults)
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.
|
StructureModifier<TField> |
withTarget(Object target)
Retrieves a structure modifier of the same type for a different object target.
|
<T> StructureModifier<T> |
withType(Class fieldType)
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.
|
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 Class targetType
protected Object target
protected EquivalentConverter<TField> converter
protected Class fieldType
protected Map<Class,StructureModifier> subtypeCache
protected boolean customConvertHandling
protected boolean useStructureCompiler
public StructureModifier(Class targetType)
targetType - - the structure to modify.public StructureModifier(Class targetType, boolean useStructureCompiler)
targetType - - the structure to modify.useStructureCompiler - - whether or not to use a structure compiler.public StructureModifier(Class targetType, 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(Class targetType, 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(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<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(Class targetType, Class fieldType, List<Field> data, Map<Field,Integer> defaultFields, EquivalentConverter<TField> converter, Map<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
Note: This method is prone to exceptions (there are currently 5 total throw statements). It is recommended that you
use readSafely(int), which returns null if the field doesn't exist, instead of throwing an exception.
fieldIndex - - index of the field.FieldAccessException - if the field doesn't exist, or it cannot be accessed under the current security contraints.public TField readSafely(int fieldIndex) throws FieldAccessException
null is 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-
}
fieldIndex - - index of the field.FieldAccessException - if the field cannot be accessed under the current security constraints.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(Field field, boolean isReadOnly) throws IllegalAccessException
field - - the field to change.isReadOnly - - TRUE if the field should be read only, FALSE otherwise.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 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(Class fieldType)
T - TypefieldType - - 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(Class fieldType, EquivalentConverter<T> converter)
T - TypefieldType - - the type, or supertype, of every field to modify.converter - - converts objects into the given type.public Class getFieldType()
public Class getTargetType()
public Object getTarget()
public int size()
protected <T> StructureModifier<T> withFieldType(Class fieldType, List<Field> filtered, Map<Field,Integer> defaults)
T - TypefieldType - - common type of each field.filtered - - list of fields after filtering the original modifier.defaults - - list of default values after filtering the original.protected <T> StructureModifier<T> withFieldType(Class fieldType, List<Field> filtered, Map<Field,Integer> defaults, EquivalentConverter<T> converter)
T - TypefieldType - - 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.public StructureModifier<TField> withTarget(Object target)
target - - different target of the same type.protected void setConverter(EquivalentConverter<TField> converter)
converter - - current object converter.public List<Field> getFields()
public Field getField(int fieldIndex)
fieldIndex - - index of the field to retrieve.IllegalArgumentException - If no field with the given index can be found.public List<TField> getValues() throws FieldAccessException
FieldAccessException - Unable to access one or all of the fieldsCopyright © 2012–2017 Comphenix and dmulloy2. Licensed under the GNU GPL v2.