Archiviert
13
0

Provide more descriptive error messages for nonexistent fields

Dieser Commit ist enthalten in:
Dan Mulloy 2015-01-28 21:28:28 -05:00
Ursprung 1d711315e4
Commit 704fe60bd1

Datei anzeigen

@ -187,22 +187,28 @@ public class StructureModifier<TField> {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TField read(int fieldIndex) throws FieldAccessException { public TField read(int fieldIndex) throws FieldAccessException {
if (target == null)
throw new IllegalStateException("Cannot read from a null target!");
if (fieldIndex < 0) if (fieldIndex < 0)
throw new FieldAccessException(String.format("Field index (%s) cannot be negative.", fieldIndex)); 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()) if (fieldIndex >= data.size())
throw new FieldAccessException(String.format("Field index out of bounds. (Index: %s, Size: %s)", 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 { try {
Object result = FieldUtils.readField(data.get(fieldIndex), target, true); Object result = FieldUtils.readField(data.get(fieldIndex), target, true);
// Use the converter, if we have it // Use the converter, if we have it
if (needConversion()) if (needConversion()) {
return converter.getSpecific(result); return converter.getSpecific(result);
else } else {
return (TField) result; return (TField) result;
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new FieldAccessException("Cannot read field due to a security limitation.", e); throw new FieldAccessException("Cannot read field due to a security limitation.", e);
} }
@ -282,12 +288,17 @@ public class StructureModifier<TField> {
* @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints. * @throws FieldAccessException The field doesn't exist, or it cannot be accessed under the current security contraints.
*/ */
public StructureModifier<TField> write(int fieldIndex, TField value) throws FieldAccessException { public StructureModifier<TField> write(int fieldIndex, TField value) throws FieldAccessException {
if (target == null)
throw new IllegalStateException("Cannot read from a null target!");
if (fieldIndex < 0) if (fieldIndex < 0)
throw new FieldAccessException(String.format("Field index (%s) cannot be negative.", fieldIndex)); 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()) if (fieldIndex >= data.size())
throw new FieldAccessException(String.format("Field index out of bounds. (Index: %s, Size: %s)", 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 // Use the converter, if it exists
Object obj = needConversion() ? converter.getGeneric(getFieldType(fieldIndex), value) : value; Object obj = needConversion() ? converter.getGeneric(getFieldType(fieldIndex), value) : value;