Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

DbUnitのITableからオブジェクトを生成する

private List createBeanList(Class clazz, ITable table) {

	Column[] columns;
	try {
		ITableMetaData metaData = table.getTableMetaData();
		columns = metaData.getColumns();
	}
	catch (DataSetException e) {
		throw new RuntimeException(e);
	}
	setTargetFields(columns);

	List list = new ArrayList();
	for (int i = 0; i < table.getRowCount(); i++) {

		Object o = Utility.createInstance(clazz);
		for (int j = 0; j < columns.length; j++) {
			Column column = columns[j];
			String columnName = column.getColumnName();
			try {
				Object value = table.getValue(i, columnName);
				BeanUtils.setProperty(o, columnName, value);
			}
			catch (Exception e) {
				throw new RuntimeException("index = " + i + ", columnName = " + columnName, e);
			}
		}
		list.add(o);
	}

	return list;
}