From 704fe60bd18856e10ab4e51302103a03f35af35f Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Wed, 28 Jan 2015 21:28:28 -0500 Subject: [PATCH] Provide more descriptive error messages for nonexistent fields --- .../protocol/reflect/StructureModifier.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 58dead0b..238a6559 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -187,27 +187,33 @@ public class StructureModifier { */ @SuppressWarnings("unchecked") public TField read(int fieldIndex) throws FieldAccessException { + if (target == null) + throw new IllegalStateException("Cannot read from a null target!"); + if (fieldIndex < 0) throw new FieldAccessException(String.format("Field index (%s) cannot be negative.", fieldIndex)); + + if (fieldIndex == 0 && data.size() == 0) + throw new FieldAccessException(String.format("No field with type %s exists in class %s.", targetType.getName(), + target.getClass().getName())); + if (fieldIndex >= data.size()) throw new FieldAccessException(String.format("Field index out of bounds. (Index: %s, Size: %s)", fieldIndex, data.size())); - if (target == null) - throw new IllegalStateException("Cannot read from a null target"); try { Object result = FieldUtils.readField(data.get(fieldIndex), target, true); // Use the converter, if we have it - if (needConversion()) + if (needConversion()) { return converter.getSpecific(result); - else + } else { return (TField) result; - + } } catch (IllegalAccessException e) { throw new FieldAccessException("Cannot read field due to a security limitation.", e); } } - + /** * Reads the value of a field if and ONLY IF it exists. * @param fieldIndex - index of the field. @@ -282,22 +288,27 @@ public class StructureModifier { * @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints. */ public StructureModifier write(int fieldIndex, TField value) throws FieldAccessException { + if (target == null) + throw new IllegalStateException("Cannot read from a null target!"); + if (fieldIndex < 0) throw new FieldAccessException(String.format("Field index (%s) cannot be negative.", fieldIndex)); + + if (fieldIndex == 0 && data.size() == 0) + throw new FieldAccessException(String.format("No field with type %s exists in class %s.", targetType.getName(), target.getClass().getName())); + if (fieldIndex >= data.size()) throw new FieldAccessException(String.format("Field index out of bounds. (Index: %s, Size: %s)", fieldIndex, data.size())); - if (target == null) - throw new IllegalStateException("Cannot write to a null target"); - + // Use the converter, if it exists Object obj = needConversion() ? converter.getGeneric(getFieldType(fieldIndex), value) : value; - + try { FieldUtils.writeField(data.get(fieldIndex), target, obj, true); } catch (IllegalAccessException e) { throw new FieldAccessException("Cannot read field due to a security limitation.", e); } - + // Make this method chainable return this; }