2012-09-13 21:04:46 +02:00
|
|
|
/*
|
|
|
|
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
|
|
|
|
* Copyright (C) 2012 Kristian S. Stangeland
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program;
|
|
|
|
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
package com.comphenix.protocol.reflect;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2012-09-27 03:24:34 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-09-27 05:28:13 +02:00
|
|
|
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler;
|
2012-09-15 19:12:59 +02:00
|
|
|
import com.comphenix.protocol.reflect.instances.DefaultInstances;
|
2012-09-12 19:04:53 +02:00
|
|
|
import com.google.common.base.Function;
|
2012-09-13 11:01:08 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-09-27 03:24:34 +02:00
|
|
|
/**
|
|
|
|
* Provides list-oriented access to the fields of a Minecraft packet.
|
|
|
|
* <p>
|
|
|
|
* Implemented by using reflection. Use a CompiledStructureModifier, if speed is essential.
|
|
|
|
*
|
|
|
|
* @author Kristian
|
|
|
|
* @param <TField> Type of the fields to retrieve.
|
|
|
|
*/
|
2012-09-12 19:04:53 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
public class StructureModifier<TField> {
|
2012-09-18 02:04:26 +02:00
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
// Object and its type
|
2012-09-19 19:25:11 +02:00
|
|
|
protected Class targetType;
|
|
|
|
protected Object target;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
// Converter. May be NULL.
|
2012-09-19 19:25:11 +02:00
|
|
|
protected EquivalentConverter<TField> converter;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
// The fields to read in order
|
2012-09-19 19:25:11 +02:00
|
|
|
protected Class fieldType;
|
|
|
|
protected List<Field> data = new ArrayList<Field>();
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
// Improved default values
|
2012-09-27 03:24:34 +02:00
|
|
|
protected Map<Field, Integer> defaultFields;
|
2012-09-13 11:01:08 +02:00
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
// Cache of previous types
|
2012-09-19 19:25:11 +02:00
|
|
|
protected Map<Class, StructureModifier> subtypeCache;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-10-01 02:17:13 +02:00
|
|
|
// Whether or subclasses should handle conversion
|
|
|
|
protected boolean customConvertHandling;
|
|
|
|
|
2012-09-13 11:34:47 +02:00
|
|
|
/**
|
|
|
|
* Creates a structure modifier.
|
|
|
|
* @param targetType - the structure to modify.
|
|
|
|
* @param superclassExclude - a superclass to exclude.
|
|
|
|
* @param requireDefault - whether or not we will be using writeDefaults().
|
|
|
|
*/
|
|
|
|
public StructureModifier(Class targetType, Class superclassExclude, boolean requireDefault) {
|
2012-09-13 11:01:08 +02:00
|
|
|
List<Field> fields = getFields(targetType, superclassExclude);
|
2012-09-27 03:24:34 +02:00
|
|
|
Map<Field, Integer> defaults = requireDefault ? generateDefaultFields(fields) : new HashMap<Field, Integer>();
|
2012-09-13 11:01:08 +02:00
|
|
|
|
2012-09-27 03:24:34 +02:00
|
|
|
initialize(targetType, Object.class, fields, defaults, null, new ConcurrentHashMap<Class, StructureModifier>());
|
2012-09-13 11:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-09-20 18:35:45 +02:00
|
|
|
/**
|
|
|
|
* Consumers of this method should call "initialize".
|
|
|
|
*/
|
|
|
|
protected StructureModifier() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize using the same field types.
|
|
|
|
* @param other - information to set.
|
|
|
|
*/
|
|
|
|
protected void initialize(StructureModifier<TField> other) {
|
|
|
|
initialize(other.targetType, other.fieldType, other.data,
|
|
|
|
other.defaultFields, other.converter, other.subtypeCache);
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 19:25:11 +02:00
|
|
|
/**
|
|
|
|
* Initialize every field of this class.
|
|
|
|
* @param targetType - type of the object we're reading and writing from.
|
|
|
|
* @param fieldType - the common type of the fields we're modifying.
|
|
|
|
* @param data - list of fields to modify.
|
|
|
|
* @param defaultFields - list of fields that will be automatically initialized.
|
|
|
|
* @param converter - converts between the common field type and the actual type the consumer expects.
|
|
|
|
* @param subTypeCache - a structure modifier cache.
|
|
|
|
*/
|
|
|
|
protected void initialize(Class targetType, Class fieldType,
|
2012-09-27 03:24:34 +02:00
|
|
|
List<Field> data, Map<Field, Integer> defaultFields,
|
2012-09-13 11:01:08 +02:00
|
|
|
EquivalentConverter<TField> converter, Map<Class, StructureModifier> subTypeCache) {
|
2012-09-12 19:04:53 +02:00
|
|
|
this.targetType = targetType;
|
|
|
|
this.fieldType = fieldType;
|
|
|
|
this.data = data;
|
2012-09-13 11:01:08 +02:00
|
|
|
this.defaultFields = defaultFields;
|
2012-09-12 19:04:53 +02:00
|
|
|
this.converter = converter;
|
|
|
|
this.subtypeCache = subTypeCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the value of a field given its index.
|
|
|
|
* @param fieldIndex - index of the field.
|
|
|
|
* @return Value of the field.
|
2012-09-13 14:42:37 +02:00
|
|
|
* @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints.
|
2012-09-12 19:04:53 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2012-09-13 14:42:37 +02:00
|
|
|
public TField read(int fieldIndex) throws FieldAccessException {
|
2012-09-12 19:04:53 +02:00
|
|
|
if (fieldIndex < 0 || fieldIndex >= data.size())
|
2012-09-13 14:42:37 +02:00
|
|
|
throw new FieldAccessException("Field index must be within 0 - count",
|
|
|
|
new IndexOutOfBoundsException("Out of bounds"));
|
2012-09-12 19:04:53 +02:00
|
|
|
if (target == null)
|
|
|
|
throw new IllegalStateException("Cannot read from a NULL target.");
|
|
|
|
|
2012-09-13 14:42:37 +02:00
|
|
|
try {
|
|
|
|
Object result = FieldUtils.readField(data.get(fieldIndex), target, true);
|
|
|
|
|
|
|
|
// Use the converter, if we have it
|
2012-10-01 02:17:13 +02:00
|
|
|
if (needConversion())
|
2012-09-13 14:42:37 +02:00
|
|
|
return converter.getSpecific(result);
|
|
|
|
else
|
|
|
|
return (TField) result;
|
|
|
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
throw new FieldAccessException("Cannot read field due to a security limitation.", e);
|
|
|
|
}
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
2012-09-13 11:01:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the value of a field if and ONLY IF it exists.
|
|
|
|
* @param fieldIndex - index of the field.
|
|
|
|
* @return Value of the field, or NULL if it doesn't exist.
|
2012-09-13 14:42:37 +02:00
|
|
|
* @throws FieldAccessException The field cannot be accessed under the current security contraints.
|
2012-09-13 11:01:08 +02:00
|
|
|
*/
|
2012-09-13 14:42:37 +02:00
|
|
|
public TField readSafely(int fieldIndex) throws FieldAccessException {
|
2012-09-13 11:01:08 +02:00
|
|
|
if (fieldIndex >= 0 && fieldIndex < data.size()) {
|
|
|
|
return read(fieldIndex);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the value of a field given its index.
|
|
|
|
* @param fieldIndex - index of the field.
|
|
|
|
* @param value - new value of the field.
|
|
|
|
* @return This structure modifier - for chaining.
|
2012-09-13 21:15:30 +02:00
|
|
|
* @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints.
|
2012-09-12 19:04:53 +02:00
|
|
|
*/
|
2012-09-13 14:42:37 +02:00
|
|
|
public StructureModifier<TField> write(int fieldIndex, TField value) throws FieldAccessException {
|
2012-09-12 19:04:53 +02:00
|
|
|
if (fieldIndex < 0 || fieldIndex >= data.size())
|
2012-09-13 14:42:37 +02:00
|
|
|
throw new FieldAccessException("Field index must be within 0 - count",
|
|
|
|
new IndexOutOfBoundsException("Out of bounds"));
|
2012-09-12 19:04:53 +02:00
|
|
|
if (target == null)
|
|
|
|
throw new IllegalStateException("Cannot write to a NULL target.");
|
|
|
|
|
|
|
|
// Use the converter, if it exists
|
2012-10-01 02:17:13 +02:00
|
|
|
Object obj = needConversion() ? converter.getGeneric(value) : value;
|
2012-09-13 14:42:37 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
FieldUtils.writeField(data.get(fieldIndex), target, obj, true);
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
throw new FieldAccessException("Cannot read field due to a security limitation.", e);
|
|
|
|
}
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
// Make this method chainable
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-10-01 02:17:13 +02:00
|
|
|
/**
|
|
|
|
* Whether or not we should use the converter instance.
|
|
|
|
* @return TRUE if we should, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
private final boolean needConversion() {
|
|
|
|
return converter != null && !customConvertHandling;
|
|
|
|
}
|
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
/**
|
|
|
|
* Writes the value of a given field IF and ONLY if it exists.
|
|
|
|
* @param fieldIndex - index of the potential field.
|
|
|
|
* @param value - new value of the field.
|
|
|
|
* @return This structure modifer - for chaining.
|
2012-09-13 21:15:30 +02:00
|
|
|
* @throws FieldAccessException The field cannot be accessed under the current security contraints.
|
2012-09-13 11:01:08 +02:00
|
|
|
*/
|
2012-09-13 14:42:37 +02:00
|
|
|
public StructureModifier<TField> writeSafely(int fieldIndex, TField value) throws FieldAccessException {
|
2012-09-13 11:01:08 +02:00
|
|
|
if (fieldIndex >= 0 && fieldIndex < data.size()) {
|
|
|
|
write(fieldIndex, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
/**
|
|
|
|
* Correctly modifies the value of a field.
|
|
|
|
* @param fieldIndex - index of the field to modify.
|
|
|
|
* @param select - the function that modifies the field value.
|
|
|
|
* @return This structure modifier - for chaining.
|
2012-09-13 21:15:30 +02:00
|
|
|
* @throws FieldAccessException The field cannot be accessed under the current security contraints.
|
2012-09-12 19:04:53 +02:00
|
|
|
*/
|
2012-09-13 14:42:37 +02:00
|
|
|
public StructureModifier<TField> modify(int fieldIndex, Function<TField, TField> select) throws FieldAccessException {
|
2012-09-12 19:04:53 +02:00
|
|
|
TField value = read(fieldIndex);
|
|
|
|
return write(fieldIndex, select.apply(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a structure modifier that only reads and writes fields of a given type.
|
|
|
|
* @param fieldType - the type, or supertype, of every field to modify.
|
|
|
|
* @return A structure modifier for fields of this type.
|
|
|
|
*/
|
|
|
|
public <T> StructureModifier<T> withType(Class fieldType) {
|
|
|
|
return withType(fieldType, null);
|
|
|
|
}
|
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
/**
|
2012-09-13 21:15:30 +02:00
|
|
|
* Sets all non-primitive fields to a more fitting default value. See {@link DefaultInstances#getDefault(Class)}.
|
2012-09-13 11:01:08 +02:00
|
|
|
* @return The current structure modifier - for chaining.
|
2012-09-13 21:15:30 +02:00
|
|
|
* @throws FieldAccessException If we're unable to write to the fields due to a security limitation.
|
2012-09-13 11:01:08 +02:00
|
|
|
*/
|
2012-09-13 21:15:30 +02:00
|
|
|
public StructureModifier<TField> writeDefaults() throws FieldAccessException {
|
2012-09-13 11:01:08 +02:00
|
|
|
|
2012-09-15 19:12:59 +02:00
|
|
|
DefaultInstances generator = DefaultInstances.DEFAULT;
|
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
// Write a default instance to every field
|
2012-09-27 03:24:34 +02:00
|
|
|
for (Field field : defaultFields.keySet()) {
|
2012-09-13 21:15:30 +02:00
|
|
|
try {
|
|
|
|
FieldUtils.writeField(field, target,
|
2012-09-15 19:12:59 +02:00
|
|
|
generator.getDefault(field.getType()), true);
|
2012-09-13 21:15:30 +02:00
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
throw new FieldAccessException("Cannot write to field due to a security limitation.", e);
|
|
|
|
}
|
2012-09-13 11:01:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a structure modifier that only reads and writes fields of a given type.
|
|
|
|
* @param fieldType - the type, or supertype, of every field to modify.
|
|
|
|
* @param converter - converts objects into the given type.
|
|
|
|
* @return A structure modifier for fields of this type.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> StructureModifier<T> withType(Class fieldType, EquivalentConverter<T> converter) {
|
|
|
|
|
|
|
|
StructureModifier<T> result = subtypeCache.get(fieldType);
|
|
|
|
|
2012-09-18 02:04:26 +02:00
|
|
|
// Do we need to update the cache?
|
|
|
|
if (result == null) {
|
2012-09-12 19:04:53 +02:00
|
|
|
List<Field> filtered = new ArrayList<Field>();
|
2012-09-27 03:24:34 +02:00
|
|
|
Map<Field, Integer> defaults = new HashMap<Field, Integer>();
|
|
|
|
int index = 0;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
for (Field field : data) {
|
2012-09-13 14:25:01 +02:00
|
|
|
if (fieldType != null && fieldType.isAssignableFrom(field.getType())) {
|
2012-09-12 19:04:53 +02:00
|
|
|
filtered.add(field);
|
2012-09-13 11:01:08 +02:00
|
|
|
|
2012-09-27 03:24:34 +02:00
|
|
|
// Don't use the original index
|
|
|
|
if (defaultFields.containsKey(field))
|
|
|
|
defaults.put(field, index);
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
2012-09-27 03:24:34 +02:00
|
|
|
|
|
|
|
// Keep track of the field index
|
|
|
|
index++;
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache structure modifiers
|
2012-09-19 19:25:11 +02:00
|
|
|
result = withFieldType(fieldType, filtered, defaults, converter);
|
2012-09-13 14:25:01 +02:00
|
|
|
|
2012-09-27 03:24:34 +02:00
|
|
|
if (fieldType != null) {
|
2012-09-13 14:25:01 +02:00
|
|
|
subtypeCache.put(fieldType, result);
|
2012-09-27 03:24:34 +02:00
|
|
|
|
|
|
|
// Automatically compile the structure modifier
|
|
|
|
if (BackgroundCompiler.getInstance() != null)
|
|
|
|
BackgroundCompiler.getInstance().scheduleCompilation(subtypeCache, fieldType);
|
|
|
|
}
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the target too
|
2012-09-18 02:04:26 +02:00
|
|
|
result = result.withTarget(target);
|
|
|
|
|
|
|
|
// And the converter, if it's needed
|
|
|
|
if (!sameConverter(result.converter, converter)) {
|
|
|
|
result = result.withConverter(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean sameConverter(EquivalentConverter<?> a, EquivalentConverter<?> b) {
|
|
|
|
// Compare the converter types
|
|
|
|
if (a == null)
|
|
|
|
return b == null;
|
|
|
|
else if (b == null)
|
|
|
|
return a == null;
|
|
|
|
else
|
|
|
|
return a.getSpecificType().equals(b.getSpecificType());
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the common type of each field.
|
|
|
|
* @return Common type of each field.
|
|
|
|
*/
|
|
|
|
public Class getFieldType() {
|
|
|
|
return fieldType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the type of the object we're modifying.
|
|
|
|
* @return Type of the object.
|
|
|
|
*/
|
|
|
|
public Class getTargetType() {
|
|
|
|
return targetType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the object we're currently modifying.
|
|
|
|
* @return Object we're modifying.
|
|
|
|
*/
|
|
|
|
public Object getTarget() {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the number of readable types.
|
|
|
|
* @return Readable types.
|
|
|
|
*/
|
|
|
|
public int size() {
|
|
|
|
return data.size();
|
|
|
|
}
|
|
|
|
|
2012-09-19 19:25:11 +02:00
|
|
|
/**
|
|
|
|
* Create a new structure modifier for the new field type.
|
|
|
|
* @param fieldType - common type of each field.
|
|
|
|
* @param filtered - list of fields after filtering the original modifier.
|
|
|
|
* @param defaults - list of default values after filtering the original.
|
|
|
|
* @param converter - the new converter.
|
|
|
|
* @return A new structure modifier.
|
|
|
|
*/
|
|
|
|
protected <T> StructureModifier<T> withFieldType(
|
|
|
|
Class fieldType, List<Field> filtered,
|
2012-09-27 03:24:34 +02:00
|
|
|
Map<Field, Integer> defaults, EquivalentConverter<T> converter) {
|
2012-09-19 19:25:11 +02:00
|
|
|
|
|
|
|
StructureModifier<T> result = new StructureModifier<T>();
|
|
|
|
result.initialize(targetType, fieldType, filtered, defaults,
|
2012-09-27 03:24:34 +02:00
|
|
|
converter, new ConcurrentHashMap<Class, StructureModifier>());
|
2012-09-19 19:25:11 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a structure modifier of the same type for a different object target.
|
|
|
|
* @param target - different target of the same type.
|
|
|
|
* @return Structure modifier with the new target.
|
|
|
|
*/
|
|
|
|
public StructureModifier<TField> withTarget(Object target) {
|
2012-09-19 19:25:11 +02:00
|
|
|
StructureModifier<TField> copy = new StructureModifier<TField>();
|
|
|
|
|
|
|
|
// Create a new instance
|
|
|
|
copy.initialize(targetType, fieldType, data, defaultFields, converter, subtypeCache);
|
|
|
|
copy.target = target;
|
|
|
|
return copy;
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a structure modifier with the same type and target, but using a new object converter.
|
2012-10-01 02:17:13 +02:00
|
|
|
* @param converter - the object converter to use.
|
2012-09-12 19:04:53 +02:00
|
|
|
* @return Structure modifier with the new converter.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private <T> StructureModifier<T> withConverter(EquivalentConverter<T> converter) {
|
2012-09-19 19:25:11 +02:00
|
|
|
StructureModifier copy = withTarget(target);
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-09-19 19:25:11 +02:00
|
|
|
copy.setConverter(converter);
|
2012-09-12 19:04:53 +02:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2012-09-19 19:25:11 +02:00
|
|
|
/**
|
|
|
|
* Set the current object converter. Should only be called during construction.
|
|
|
|
* @param converter - current object converter.
|
|
|
|
*/
|
|
|
|
protected void setConverter(EquivalentConverter<TField> converter) {
|
|
|
|
this.converter = converter;
|
|
|
|
}
|
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a list of the fields matching the constraints of this structure modifier.
|
|
|
|
* @return List of fields.
|
|
|
|
*/
|
|
|
|
public List<Field> getFields() {
|
|
|
|
return ImmutableList.copyOf(data);
|
|
|
|
}
|
2012-09-20 18:35:45 +02:00
|
|
|
|
2012-09-15 19:12:59 +02:00
|
|
|
/**
|
|
|
|
* Retrieve every value stored in the fields of the current type.
|
|
|
|
* @return Every field value.
|
|
|
|
* @throws FieldAccessException Unable to access one or all of the fields
|
|
|
|
*/
|
|
|
|
public List<TField> getValues() throws FieldAccessException {
|
|
|
|
List<TField> values = new ArrayList<TField>();
|
|
|
|
|
2012-09-20 18:35:45 +02:00
|
|
|
for (int i = 0; i < size(); i++) {
|
2012-09-15 19:12:59 +02:00
|
|
|
values.add(read(i));
|
2012-09-20 18:35:45 +02:00
|
|
|
}
|
2012-09-15 19:12:59 +02:00
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
// Used to generate plausible default values
|
2012-09-27 03:24:34 +02:00
|
|
|
private static Map<Field, Integer> generateDefaultFields(List<Field> fields) {
|
2012-09-13 11:01:08 +02:00
|
|
|
|
2012-09-27 03:24:34 +02:00
|
|
|
Map<Field, Integer> requireDefaults = new HashMap<Field, Integer>();
|
2012-09-15 19:12:59 +02:00
|
|
|
DefaultInstances generator = DefaultInstances.DEFAULT;
|
2012-09-27 03:24:34 +02:00
|
|
|
int index = 0;
|
2012-09-15 19:12:59 +02:00
|
|
|
|
2012-09-13 11:01:08 +02:00
|
|
|
for (Field field : fields) {
|
|
|
|
Class<?> type = field.getType();
|
|
|
|
|
|
|
|
// First, ignore primitive fields
|
2012-09-13 13:57:22 +02:00
|
|
|
if (!PrimitiveUtils.isPrimitive(type)) {
|
2012-09-13 11:01:08 +02:00
|
|
|
// Next, see if we actually can generate a default value
|
2012-09-15 19:12:59 +02:00
|
|
|
if (generator.getDefault(type) != null) {
|
2012-09-13 11:01:08 +02:00
|
|
|
// If so, require it
|
2012-09-27 03:24:34 +02:00
|
|
|
requireDefaults.put(field, index);
|
2012-09-13 11:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-27 03:24:34 +02:00
|
|
|
|
|
|
|
// Increment field index
|
|
|
|
index++;
|
2012-09-13 11:01:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return requireDefaults;
|
|
|
|
}
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
// Used to filter out irrelevant fields
|
2012-09-13 11:01:08 +02:00
|
|
|
private static List<Field> getFields(Class type, Class superclassExclude) {
|
2012-09-12 19:04:53 +02:00
|
|
|
List<Field> result = new ArrayList<Field>();
|
|
|
|
|
|
|
|
// Retrieve every private and public field
|
|
|
|
for (Field field : FuzzyReflection.fromClass(type, true).getFields()) {
|
|
|
|
int mod = field.getModifiers();
|
|
|
|
|
|
|
|
// Ignore static, final and "abstract packet" fields
|
2012-09-13 11:01:08 +02:00
|
|
|
if (!Modifier.isFinal(mod) && !Modifier.isStatic(mod) && (
|
2012-09-15 19:12:59 +02:00
|
|
|
superclassExclude == null || !field.getDeclaringClass().equals(superclassExclude)
|
2012-09-13 11:01:08 +02:00
|
|
|
)) {
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
result.add(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|