+ " found in " + targetClass.getSimpleName() + ".");
if (objectToInjectType instanceof Class) {
message.append(" If the target is a generic type, this can be caused by type erasure.");
}
message.append(" Specify the target field explicitly instead of injecting into by type.");
throw new JTesterException(message.toString());
} else if (fieldsWithExactType.size() == 1) {
fieldToInjectTo = fieldsWithExactType.iterator().next();
} else {
// Try to find a supertype field:
// If one field exist that has a type which is more specific than
// all other fields of the given type,
// this one is taken. Otherwise, an exception is thrown
Set<Field> fieldsOfType = FieldHelper.getFieldsAssignableFrom(targetClass, objectToInjectType);
if (fieldsOfType.size() == 0) {
throw new JTesterException("No field with (super)type " + objectToInjectType + " found in "
+ targetClass.getSimpleName());
}
for (Field field : fieldsOfType) {
boolean moreSpecific = true;
for (Field compareToField : fieldsOfType) {
if (field != compareToField) {
if (field.getClass().isAssignableFrom(compareToField.getClass())) {
moreSpecific = false;
break;
}
}
}
if (moreSpecific) {
fieldToInjectTo = field;
break;
}
}
if (fieldToInjectTo == null) {
throw new JTesterException("Multiple candidate target fields found in " + targetClass.getSimpleName()
+ ", with none of them more specific than all others.");
}
}
// Field to inject into found, inject the object and return old value