1. Прошу объяснить максимально подробно, что происходит в этом методе и для чего он нужен (если я всё правильно перевел, этот метод позволяет узнать есть ли возможность создать экземпляр Т-класса, но не понимаю зачем это нужно.).
2. Как связан это метод с методом public List<T> execute()?
3. Синтаксис тоже не очень понятен. Я вижу это так: метод приводит к Т-классу что-то переданное в скобках.
Спасибо.
public abstract class AbstractDbSelectExecutor<T extends NamedItem> {
public abstract String getQuery();
/**
* This is a fake method
*
* @return a list of 5 fake items
*/
public List<T> execute() {
List<T> result = new ArrayList<>();
//check that the query is not null
String query = getQuery();
if (query == null) return result;
try {
//generate 5 fake items
for (int i = 1; i <= 5; i++) {
T newItem = getNewInstanceOfGenericType();
newItem.setId(i);
newItem.setName(newItem.getClass().getSimpleName() + "-" + i);
newItem.setDescription("Received from executing '" + query + "'");
result.add(newItem);
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return result;
}
//reflection
//you need to know that it is possible to create a new instance of the T (generic type) class using its default constructor
private T getNewInstanceOfGenericType() throws InstantiationException, IllegalAccessException {
return (T) ((Class) ((ParameterizedType) this.getClass().
getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
}
}