Skip to content

Commit

Permalink
add Test at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Nov 29, 2018
1 parent 72a0c8a commit 82e0513
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
85 changes: 42 additions & 43 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ private Sheet getSheetToProcess(Workbook workbook, PoijiOptions options) {

private void loadColumnTitles(Sheet sheet, int maxPhysicalNumberOfRows) {
if (maxPhysicalNumberOfRows > 0) {
int row = options.getRowStart();
int row = options.getRowStart();
Row firstRow = sheet.getRow(row);
for (Cell cell : firstRow) {
titles.put(cell.getStringCellValue() + cell.getColumnIndex() , cell.getColumnIndex());
titles.put(cell.getStringCellValue() + cell.getColumnIndex(), cell.getColumnIndex());
}
}
}
Expand All @@ -100,57 +100,56 @@ private <T> T deserialize0(Row currentRow, Class<T> type) {
return setFieldValue(currentRow, type, instance);
}

private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instance) {
int column = 0;
private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instance) {
int column = 0;
for (Field field : type.getDeclaredFields()) {
ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o;
o = casting.castValue(field.getType(), valueOf(currentRow.getRowNum()), options);
setFieldData(instance, field, o);
}
ExcelCellRange excelCellRange = field.getAnnotation(ExcelCellRange.class);
if (excelCellRange != null) {
if (column < excelCellRange.begin() || column > excelCellRange.end()) {
continue;
}
Class<?> o = field.getType();
Object ins;
ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
if (excelRow != null) {
Object o;
o = casting.castValue(field.getType(), valueOf(currentRow.getRowNum()), options);
setFieldData(instance, field, o);
}
ExcelCellRange excelCellRange = field.getAnnotation(ExcelCellRange.class);
if (excelCellRange != null) {
if (column < excelCellRange.begin() || column > excelCellRange.end()) {
continue;
}
Class<?> o = field.getType();
Object ins;
try {
ins = o.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
throw new PoijiInstantiationException("Cannot create a new instance of " + o.getName());
}
for(Field f: o.getDeclaredFields()) {
tailSetFieldValue(currentRow, ins, f, column++);
}
try {
field.setAccessible(true);
field.set(instance, ins);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new PoijiInstantiationException("Cannot access field " + field.getName());
}
} else {
tailSetFieldValue(currentRow, instance, field, column++);
}
for (Field f : o.getDeclaredFields()) {
tailSetFieldValue(currentRow, ins, f, column++);
}
try {
field.setAccessible(true);
field.set(instance, ins);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new PoijiInstantiationException("Cannot access field " + field.getName());
}
} else {
tailSetFieldValue(currentRow, instance, field, column++);
}
}
return instance;
}

private <T> void tailSetFieldValue(Row currentRow, T instance,
Field field, int column) {
ExcelCell index = field.getAnnotation(ExcelCell.class);
if (index != null) {
constructTypeValue(currentRow, instance, field, index.value());
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
Integer titleColumn = titles.get(excelCellName.value() + column);
if (titleColumn != null) {
constructTypeValue(currentRow, instance, field, titleColumn);
}
}
}
private <T> void tailSetFieldValue(Row currentRow, T instance, Field field, int column) {
ExcelCell index = field.getAnnotation(ExcelCell.class);
if (index != null) {
constructTypeValue(currentRow, instance, field, index.value());
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
Integer titleColumn = titles.get(excelCellName.value() + column);
if (titleColumn != null) {
constructTypeValue(currentRow, instance, field, titleColumn);
}
}
}
}

private <T> void constructTypeValue(Row currentRow, T instance, Field field, int column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import static org.junit.Assert.fail;

@RunWith(Parameterized.class)
public class DeserializeByCellRange {
public class DeserializeByCellRangeTest {

private String path;

public DeserializeByCellRange(String path) {
public DeserializeByCellRangeTest(String path) {
this.path = path;
}

Expand Down

0 comments on commit 82e0513

Please sign in to comment.