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