Browse Source

add method and annotation

Looly 5 years ago
parent
commit
a5624e6f94

+ 1 - 0
CHANGELOG.md

@@ -19,6 +19,7 @@
 * 【extra 】     QRConfig中添加qrVersion属性(pr#1068@Github)
 * 【core  】     ArrayUtil增加equals方法
 * 【core  】     BeanDesc增加方法
+* 【core  】     增加@PropIgnore注解(issue#I1U846@Gitee)
 
 ### Bug修复
 * 【core  】     重新整理农历节假日,解决一个pr过来的玩笑导致的问题

+ 3 - 2
hutool-core/src/main/java/cn/hutool/core/annotation/Ignore.java

@@ -7,7 +7,8 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * 忽略注解,使用此注解的字段等会被忽略,主要用于Bean拷贝、Bean转Map等
+ * 属性忽略注解,使用此注解的字段等会被忽略,主要用于Bean拷贝、Bean转Map等<br>
+ * 此注解应用于字段时,忽略读取和设置属性值,应用于setXXX方法忽略设置值,应用于getXXX忽略读取值
  *
  * @author Looly
  * @since 5.4.2
@@ -15,6 +16,6 @@ import java.lang.annotation.Target;
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
-public @interface Ignore {
+public @interface PropIgnore {
 
 }

+ 11 - 11
hutool-core/src/main/java/cn/hutool/core/bean/BeanDesc.java

@@ -1,7 +1,7 @@
 package cn.hutool.core.bean;
 
 import cn.hutool.core.annotation.AnnotationUtil;
-import cn.hutool.core.annotation.Ignore;
+import cn.hutool.core.annotation.PropIgnore;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.map.CaseInsensitiveMap;
@@ -566,33 +566,33 @@ public class BeanDesc implements Serializable {
 		}
 
 		/**
-		 * 检查字段是否被忽略读,通过{@link Ignore} 注解完成,规则为:
+		 * 检查字段是否被忽略读,通过{@link PropIgnore} 注解完成,规则为:
 		 * <pre>
-		 *     1. 在字段上有{@link Ignore} 注解
-		 *     2. 在getXXX方法上有{@link Ignore} 注解
+		 *     1. 在字段上有{@link PropIgnore} 注解
+		 *     2. 在getXXX方法上有{@link PropIgnore} 注解
 		 * </pre>
 		 *
 		 * @return 是否忽略读
 		 * @since 5.4.2
 		 */
 		public boolean isIgnoreGet() {
-			return AnnotationUtil.hasAnnotation(this.field, Ignore.class)
-					|| AnnotationUtil.hasAnnotation(this.getter, Ignore.class);
+			return AnnotationUtil.hasAnnotation(this.field, PropIgnore.class)
+					|| AnnotationUtil.hasAnnotation(this.getter, PropIgnore.class);
 		}
 
 		/**
-		 * 检查字段是否被忽略写,通过{@link Ignore} 注解完成,规则为:
+		 * 检查字段是否被忽略写,通过{@link PropIgnore} 注解完成,规则为:
 		 * <pre>
-		 *     1. 在字段上有{@link Ignore} 注解
-		 *     2. 在setXXX方法上有{@link Ignore} 注解
+		 *     1. 在字段上有{@link PropIgnore} 注解
+		 *     2. 在setXXX方法上有{@link PropIgnore} 注解
 		 * </pre>
 		 *
 		 * @return 是否忽略写
 		 * @since 5.4.2
 		 */
 		public boolean isIgnoreSet() {
-			return AnnotationUtil.hasAnnotation(this.field, Ignore.class)
-					|| AnnotationUtil.hasAnnotation(this.setter, Ignore.class);
+			return AnnotationUtil.hasAnnotation(this.field, PropIgnore.class)
+					|| AnnotationUtil.hasAnnotation(this.setter, PropIgnore.class);
 		}
 
 		//------------------------------------------------------------------------------------ Private method start

+ 12 - 2
hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java

@@ -1,6 +1,7 @@
 package cn.hutool.json;
 
 import cn.hutool.core.annotation.Alias;
+import cn.hutool.core.annotation.PropIgnore;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DatePattern;
@@ -268,7 +269,9 @@ public class JSONObjectTest {
 	 */
 	@Test
 	public void toBeanTest7() {
-		String jsonStr = " {\"result\":{\"phone\":\"15926297342\",\"appKey\":\"e1ie12e1ewsdqw1\",\"secret\":\"dsadadqwdqs121d1e2\",\"message\":\"hello world\"},\"code\":100,\"message\":\"validate message\"}";
+		String jsonStr = " {\"result\":{\"phone\":\"15926297342\",\"appKey\":\"e1ie12e1ewsdqw1\"," +
+				"\"secret\":\"dsadadqwdqs121d1e2\",\"message\":\"hello world\"},\"code\":100,\"" +
+				"message\":\"validate message\"}";
 		ResultDto<?> dto = JSONUtil.toBean(jsonStr, ResultDto.class);
 		Assert.assertEquals("validate message", dto.getMessage());
 	}
@@ -466,6 +469,9 @@ public class JSONObjectTest {
 		final JSONObject parse = JSONUtil.parseObj(sameNameBean);
 		Assert.assertEquals("123", parse.getStr("username"));
 		Assert.assertEquals("abc", parse.getStr("userName"));
+
+		// 测试ignore注解是否有效
+		Assert.assertNull(parse.getStr("fieldToIgnore"));
 	}
 
 	/**
@@ -477,14 +483,18 @@ public class JSONObjectTest {
 	public static class SameNameBean {
 		private final String username = "123";
 		private final String userName = "abc";
-
 		public String getUsername() {
 			return username;
 		}
+		@PropIgnore
+		private final String fieldToIgnore = "sfdsdads";
 
 		public String getUserName() {
 			return userName;
 		}
 
+		public String getFieldToIgnore(){
+			return this.fieldToIgnore;
+		}
 	}
 }