add method and annotation

This commit is contained in:
Looly 2020-09-08 23:54:43 +08:00
parent 46764f02b8
commit a5624e6f94
4 changed files with 27 additions and 15 deletions

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package cn.hutool.json; package cn.hutool.json;
import cn.hutool.core.annotation.Alias; import cn.hutool.core.annotation.Alias;
import cn.hutool.core.annotation.PropIgnore;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
@ -268,7 +269,9 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void toBeanTest7() { 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); ResultDto<?> dto = JSONUtil.toBean(jsonStr, ResultDto.class);
Assert.assertEquals("validate message", dto.getMessage()); Assert.assertEquals("validate message", dto.getMessage());
} }
@ -466,6 +469,9 @@ public class JSONObjectTest {
final JSONObject parse = JSONUtil.parseObj(sameNameBean); final JSONObject parse = JSONUtil.parseObj(sameNameBean);
Assert.assertEquals("123", parse.getStr("username")); Assert.assertEquals("123", parse.getStr("username"));
Assert.assertEquals("abc", 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 { public static class SameNameBean {
private final String username = "123"; private final String username = "123";
private final String userName = "abc"; private final String userName = "abc";
public String getUsername() { public String getUsername() {
return username; return username;
} }
@PropIgnore
private final String fieldToIgnore = "sfdsdads";
public String getUserName() { public String getUserName() {
return userName; return userName;
} }
public String getFieldToIgnore(){
return this.fieldToIgnore;
}
} }
} }