Looly 5 years ago
parent
commit
637e1942f9

+ 1 - 0
hutool-core/src/main/java/cn/hutool/core/lang/Assert.java

@@ -486,6 +486,7 @@ public class Assert {
 	 * </pre>
 	 *
 	 * @param <T>           数组元素类型
+	 * @param <X>           异常类型
 	 * @param array         被检查的数组
 	 * @param errorSupplier 错误抛出异常附带的消息生产接口
 	 * @return 被检查的数组

+ 1 - 3
hutool-json/src/main/java/cn/hutool/json/JSONConverter.java

@@ -77,9 +77,7 @@ public class JSONConverter implements Converter<JSON> {
 			}
 		}
 
-		final T targetValue = ignoreError ?
-				Convert.convertQuietly(targetType, value):
-				Convert.convert(targetType, value);
+		final T targetValue = Convert.convertWithCheck(targetType, value, null, ignoreError);
 
 		if (null == targetValue && false == ignoreError) {
 			if (StrUtil.isBlankIfStr(value)) {

+ 27 - 0
hutool-json/src/test/java/cn/hutool/json/Issue1200Test.java

@@ -0,0 +1,27 @@
+package cn.hutool.json;
+
+import cn.hutool.core.io.resource.ResourceUtil;
+import cn.hutool.core.lang.Console;
+import cn.hutool.json.test.bean.ResultBean;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * 测试在bean转换时使用BeanConverter,默认忽略转换失败的字段。
+ * 现阶段Converter的问题在于,无法更细粒度的控制转换失败的范围,例如Bean的一个字段为List,
+ * list任意一个item转换失败都会导致这个list为null。
+ *
+ * TODO 需要在Converter中添加ConvertOption,用于更细粒度的控制转换规则
+ */
+public class Issue1200Test {
+
+	@Test
+	@Ignore
+	public void toBeanTest(){
+		final JSONObject jsonObject = JSONUtil.parseObj(ResourceUtil.readUtf8Str("issue1200.json"));
+		Console.log(jsonObject);
+
+		final ResultBean resultBean = jsonObject.toBean(ResultBean.class);
+		Console.log(resultBean);
+	}
+}

+ 10 - 0
hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java

@@ -1,9 +1,11 @@
 package cn.hutool.json;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.convert.ConvertException;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.lang.Console;
 import cn.hutool.core.lang.Dict;
+import cn.hutool.core.lang.TypeReference;
 import cn.hutool.core.util.CharsetUtil;
 import cn.hutool.json.test.bean.Exam;
 import cn.hutool.json.test.bean.JsonNode;
@@ -157,6 +159,14 @@ public class JSONArrayTest {
 		Assert.assertEquals("bvalue", list.get(1).getBkey());
 	}
 
+	@Test(expected = ConvertException.class)
+	public void toListWithErrorTest(){
+		String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
+		JSONArray ja = JSONUtil.parseArray(json);
+
+		List<List<KeyBean>> list = ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
+	}
+
 	@Test
 	public void toBeanListTest() {
 		List<Map<String, String>> mapList = new ArrayList<>();

+ 20 - 0
hutool-json/src/test/java/cn/hutool/json/test/bean/ResultBean.java

@@ -0,0 +1,20 @@
+package cn.hutool.json.test.bean;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class ResultBean {
+	public List<List<List<ItemsBean>>> items;
+
+	@Data
+	public static class ItemsBean {
+		public DetailBean detail;
+
+		@Data
+		public static class DetailBean {
+			public String visitorStatus;
+		}
+	}
+}

+ 15 - 0
hutool-json/src/test/resources/issue1200.json

@@ -0,0 +1,15 @@
+{
+  "items": [
+    [
+      [
+        {
+          "fromAccount": "test禾信-cd09dss",
+          "url": "https://m.baidu.com/s?wd=aaa",
+          "fromType": "搜索推广"
+        },
+        "去杭州旅游旅游攻略",
+        "杭州旅游攻略"
+      ]
+    ]
+  ]
+}

+ 33 - 0
hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java

@@ -6,13 +6,19 @@ import cn.hutool.core.util.StrUtil;
 import cn.hutool.poi.excel.ExcelUtil;
 import cn.hutool.poi.excel.StyleSet;
 import cn.hutool.poi.excel.editors.TrimEditor;
+
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Drawing;
 import org.apache.poi.ss.usermodel.FormulaError;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.NumberToTextConverter;
 import org.apache.poi.ss.util.RegionUtil;
@@ -379,6 +385,33 @@ public class CellUtil {
 				SheetUtil.getCell(sheet, y, x));
 	}
 
+	/**
+	 * 为特定单元格添加批注
+	 *
+	 * @param cell 单元格
+	 * @param commentText 批注内容
+	 * @param commentAuthor 作者
+	 * @param anchor 批注的位置、大小等信息,null表示使用默认
+	 * @since 5.4.8
+	 */
+	public static void setComment(Cell cell, String commentText, String commentAuthor, ClientAnchor anchor) {
+		final Sheet sheet = cell.getSheet();
+		final Workbook wb = sheet.getWorkbook();
+		final Drawing<?> drawing = sheet.createDrawingPatriarch();
+		final CreationHelper factory = wb.getCreationHelper();
+		if (anchor == null) {
+			anchor = factory.createClientAnchor();
+			anchor.setCol1(cell.getColumnIndex() + 1);
+			anchor.setCol2(cell.getColumnIndex() + 3);
+			anchor.setRow1(cell.getRowIndex());
+			anchor.setRow2(cell.getRowIndex() + 2);
+		}
+		Comment comment = drawing.createCellComment(anchor);
+		comment.setString(factory.createRichTextString(commentText));
+		comment.setAuthor(StrUtil.nullToEmpty(commentText));
+		cell.setCellComment(comment);
+	}
+
 	// -------------------------------------------------------------------------------------------------------------- Private method start
 
 	/**