mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
eaa80f55a6
commit
9835d1bd80
@ -67,7 +67,7 @@ public class BeanPath implements Serializable{
|
||||
* @param expression 表达式
|
||||
* @return BeanPath
|
||||
*/
|
||||
public static BeanPath create(final String expression) {
|
||||
public static BeanPath of(final String expression) {
|
||||
return new BeanPath(expression);
|
||||
}
|
||||
|
||||
@ -80,6 +80,15 @@ public class BeanPath implements Serializable{
|
||||
init(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表达式解析后的分段列表
|
||||
*
|
||||
* @return 表达式分段列表
|
||||
*/
|
||||
public List<String> getPatternParts(){
|
||||
return this.patternParts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Bean中对应表达式的值
|
||||
*
|
||||
@ -107,6 +116,12 @@ public class BeanPath implements Serializable{
|
||||
set(bean, this.patternParts, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.patternParts.toString();
|
||||
}
|
||||
|
||||
//region Private Methods
|
||||
/**
|
||||
* 设置表达式指定位置(或filed对应)的值<br>
|
||||
* 若表达式指向一个List则设置其坐标对应位置的值,若指向Map则put对应key的值,Bean则设置字段的值<br>
|
||||
@ -131,7 +146,6 @@ public class BeanPath implements Serializable{
|
||||
BeanUtil.setFieldValue(subBean, patternParts.get(patternParts.size() - 1), value);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 获取Bean中对应表达式的值
|
||||
*
|
||||
@ -224,6 +238,7 @@ public class BeanPath implements Serializable{
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
char c;
|
||||
boolean isNumStart = false;// 下标标识符开始
|
||||
boolean isInWrap = false; //标识是否在引号内
|
||||
for (int i = 0; i < length; i++) {
|
||||
c = expression.charAt(i);
|
||||
if (0 == i && '$' == c) {
|
||||
@ -232,7 +247,13 @@ public class BeanPath implements Serializable{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ArrayUtil.contains(EXP_CHARS, c)) {
|
||||
if('\'' == c){
|
||||
// 结束
|
||||
isInWrap = (false == isInWrap);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false == isInWrap && ArrayUtil.contains(EXP_CHARS, c)) {
|
||||
// 处理边界符号
|
||||
if (CharUtil.BRACKET_END == c) {
|
||||
// 中括号(数字下标)结束
|
||||
@ -252,7 +273,7 @@ public class BeanPath implements Serializable{
|
||||
// 每一个边界符之前的表达式是一个完整的KEY,开始处理KEY
|
||||
}
|
||||
if (builder.length() > 0) {
|
||||
localPatternParts.add(unWrapIfPossible(builder));
|
||||
localPatternParts.add(builder.toString());
|
||||
}
|
||||
builder.setLength(0);
|
||||
} else {
|
||||
@ -266,25 +287,12 @@ public class BeanPath implements Serializable{
|
||||
throw new IllegalArgumentException(StrUtil.format("Bad expression '{}':{}, we find '[' but no ']' !", expression, length - 1));
|
||||
} else {
|
||||
if (builder.length() > 0) {
|
||||
localPatternParts.add(unWrapIfPossible(builder));
|
||||
localPatternParts.add(builder.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// 不可变List
|
||||
this.patternParts = ListUtil.view(localPatternParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对于非表达式去除单引号
|
||||
*
|
||||
* @param expression 表达式
|
||||
* @return 表达式
|
||||
*/
|
||||
private static String unWrapIfPossible(final CharSequence expression) {
|
||||
if (StrUtil.containsAny(expression, " = ", " > ", " < ", " like ", ",")) {
|
||||
return expression.toString();
|
||||
}
|
||||
return StrUtil.unWrap(expression, '\'');
|
||||
}
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------- Private method end
|
||||
//endregion
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ public class BeanUtil {
|
||||
if (null == bean || StrUtil.isBlank(expression)) {
|
||||
return null;
|
||||
}
|
||||
return (T) BeanPath.create(expression).get(bean);
|
||||
return (T) BeanPath.of(expression).get(bean);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -354,7 +354,7 @@ public class BeanUtil {
|
||||
* @since 4.0.6
|
||||
*/
|
||||
public static void setProperty(final Object bean, final String expression, final Object value) {
|
||||
BeanPath.create(expression).set(bean, value);
|
||||
BeanPath.of(expression).set(bean, value);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------- mapToBean
|
||||
|
@ -534,7 +534,7 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getByPath(final String expression) {
|
||||
return (T) BeanPath.create(expression).get(this);
|
||||
return (T) BeanPath.of(expression).get(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,14 +86,14 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
|
||||
final BeanPath pattern = BeanPath.of("userInfo.examInfoDict[0].id");
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assert.assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTest() {
|
||||
final BeanPath pattern = BeanPath.create("userInfo.examInfoDict[0].id");
|
||||
final BeanPath pattern = BeanPath.of("userInfo.examInfoDict[0].id");
|
||||
pattern.set(tempMap, 2);
|
||||
final Object result = pattern.get(tempMap);
|
||||
Assert.assertEquals(2, result);
|
||||
@ -101,7 +101,7 @@ public class BeanPathTest {
|
||||
|
||||
@Test
|
||||
public void getMapTest () {
|
||||
final BeanPath pattern = BeanPath.create("userInfo[id, photoPath]");
|
||||
final BeanPath pattern = BeanPath.of("userInfo[id, photoPath]");
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
|
||||
Assert.assertEquals(1, result.get("id"));
|
||||
@ -114,7 +114,15 @@ public class BeanPathTest {
|
||||
dataMap.put("aa", "value0");
|
||||
dataMap.put("aa.bb.cc", "value111111");// key 是类名 格式 带 ' . '
|
||||
|
||||
final BeanPath pattern = BeanPath.create("'aa.bb.cc'");
|
||||
final BeanPath pattern = BeanPath.of("'aa.bb.cc'");
|
||||
Assert.assertEquals("value111111", pattern.get(dataMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compileTest(){
|
||||
final BeanPath of = BeanPath.of("'abc.dd'.ee.ff'.'");
|
||||
Assert.assertEquals("abc.dd", of.getPatternParts().get(0));
|
||||
Assert.assertEquals("ee", of.getPatternParts().get(1));
|
||||
Assert.assertEquals("ff.", of.getPatternParts().get(2));
|
||||
}
|
||||
}
|
||||
|
@ -704,7 +704,7 @@ public class BeanUtilTest {
|
||||
|
||||
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3});
|
||||
|
||||
final BeanPath beanPath = BeanPath.create("testPojo2List.age");
|
||||
final BeanPath beanPath = BeanPath.of("testPojo2List.age");
|
||||
final Object o = beanPath.get(testPojo);
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0));
|
||||
|
@ -192,7 +192,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public Object getByPath(final String expression) {
|
||||
return BeanPath.create(expression).get(this);
|
||||
return BeanPath.of(expression).get(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -202,7 +202,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public void putByPath(final String expression, final Object value) {
|
||||
BeanPath.create(expression).set(this, value);
|
||||
BeanPath.of(expression).set(this, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,7 +184,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
|
||||
@Override
|
||||
public Object getByPath(final String expression) {
|
||||
return BeanPath.create(expression).get(this);
|
||||
return BeanPath.of(expression).get(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -194,7 +194,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
|
||||
@Override
|
||||
public void putByPath(final String expression, final Object value) {
|
||||
BeanPath.create(expression).set(this, value);
|
||||
BeanPath.of(expression).set(this, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user