Provide more descriptive error messages for nonexistent fields
Dieser Commit ist enthalten in:
Ursprung
1d711315e4
Commit
704fe60bd1
@ -187,27 +187,33 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the value of a field if and ONLY IF it exists.
|
* Reads the value of a field if and ONLY IF it exists.
|
||||||
* @param fieldIndex - index of the field.
|
* @param fieldIndex - index of the field.
|
||||||
@ -282,22 +288,27 @@ 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;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FieldUtils.writeField(data.get(fieldIndex), target, obj, true);
|
FieldUtils.writeField(data.get(fieldIndex), target, obj, true);
|
||||||
} 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make this method chainable
|
// Make this method chainable
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren