11package com .github .developframework .expression ;
22
33import com .github .developframework .expression .exception .ExpressionException ;
4+ import develop .toolkit .utils .JavaBeanUtils ;
45
56import java .lang .reflect .Field ;
7+ import java .lang .reflect .Method ;
68import java .util .*;
79
810/**
@@ -18,7 +20,8 @@ private ExpressionUtils() {
1820
1921 /**
2022 * 获取值
21- * @param instance 实例
23+ *
24+ * @param instance 实例
2225 * @param expressionValue 表达式字符串
2326 * @return
2427 */
@@ -28,9 +31,10 @@ public static final Object getValue(Object instance, String expressionValue) {
2831
2932 /**
3033 * 获取值
31- * @param instance 实例
34+ *
35+ * @param instance 实例
3236 * @param expressionValue 表达式字符串
33- * @param targetClass 目标类型
37+ * @param targetClass 目标类型
3438 * @param <T>
3539 * @return
3640 */
@@ -40,8 +44,9 @@ public static final <T> T getValue(Object instance, String expressionValue, Clas
4044
4145 /**
4246 * 获取值
43- * @param instance 实例
44- * @param expression 表达式
47+ *
48+ * @param instance 实例
49+ * @param expression 表达式
4550 * @param targetClass 目标类型
4651 * @param <T>
4752 * @return
@@ -52,25 +57,26 @@ public static final <T> T getValue(Object instance, Expression expression, Class
5257
5358 /**
5459 * 获取值
55- * @param instance 实例
60+ *
61+ * @param instance 实例
5662 * @param expression 表达式
5763 * @return 值
5864 */
5965 public static final Object getValue (Object instance , Expression expression ) {
6066 Objects .requireNonNull (instance );
61- if (expression == null ) {
67+ if (expression == null ) {
6268 throw new ExpressionException ("expression is null." );
6369 }
64- if (expression == Expression .EMPTY_EXPRESSION ) {
70+ if (expression == Expression .EMPTY_EXPRESSION ) {
6571 return instance ;
6672 }
6773 Expression [] expressionTree = expression .expressionTree ();
6874 Object tempObject = instance ;
6975 for (Expression singleExpression : expressionTree ) {
70- if (tempObject == null ) {
76+ if (tempObject == null ) {
7177 return null ;
7278 }
73- if (singleExpression instanceof ObjectExpression ){
79+ if (singleExpression instanceof ObjectExpression ) {
7480 tempObject = getValueFromObjectOrMap (tempObject , ((ObjectExpression ) singleExpression ).getPropertyName ());
7581 } else if (singleExpression instanceof ArrayExpression ) {
7682 tempObject = getValueFromArray (tempObject , (ArrayExpression ) singleExpression );
@@ -84,37 +90,32 @@ public static final Object getValue(Object instance, Expression expression) {
8490 /**
8591 * 从对象或Map中获取值
8692 *
87- * @param instance 实例
93+ * @param instance 实例
8894 * @param propertyName 属性名称
8995 * @return 值
9096 */
9197 private static final Object getValueFromObjectOrMap (Object instance , String propertyName ) {
9298 Class <?> clazz = instance .getClass ();
93- if (Map .class .isAssignableFrom (clazz )) {
99+ if (Map .class .isAssignableFrom (clazz )) {
94100 return ((Map ) instance ).get (propertyName );
95101 }
96- try {
97- Field field = getDeclaredField (clazz , propertyName );
98- field .setAccessible (true );
99- return field .get (instance );
100- } catch (IllegalAccessException e ) {
101- throw new ExpressionException ("Illegal access field \" %s\" in class \" %s\" ." , propertyName , clazz .getName ());
102- }
102+ Field field = getDeclaredField (clazz , propertyName );
103+ return getFieldValue (field , instance );
103104 }
104105
105106 /**
106107 * 从数组中获取值
107108 *
108- * @param instance 实例
109+ * @param instance 实例
109110 * @param arrayExpression 表达式
110111 * @return 值
111112 */
112113 private static final Object getValueFromArray (Object instance , ArrayExpression arrayExpression ) {
113114 Object arrayObject = instance ;
114- if (arrayExpression .hasPropertyName ()) {
115+ if (arrayExpression .hasPropertyName ()) {
115116 arrayObject = getValueFromObjectOrMap (instance , arrayExpression .getPropertyName ());
116117 }
117- if (arrayObject == null ) {
118+ if (arrayObject == null ) {
118119 return null ;
119120 }
120121 Class <?> clazz = arrayObject .getClass ();
@@ -133,7 +134,7 @@ private static final Object getValueFromArray(Object instance, ArrayExpression a
133134
134135 private static Field getDeclaredField (final Class <?> clazz , String propertyName ) {
135136 Class <?> temp = clazz ;
136- while (temp != Object .class ) {
137+ while (temp != Object .class ) {
137138 try {
138139 Field field = temp .getDeclaredField (propertyName );
139140 field .setAccessible (true );
@@ -144,4 +145,21 @@ private static Field getDeclaredField(final Class<?> clazz, String propertyName)
144145 }
145146 throw new ExpressionException ("No such field \" %s\" in class \" %s\" ." , propertyName , clazz .getName ());
146147 }
148+
149+ private static Object getFieldValue (Field field , Object instance ) {
150+ String getterMethodName = JavaBeanUtils .getGetterMethodName (field .getName (), field .getType ());
151+ Class <?> instanceClass = instance .getClass ();
152+ try {
153+ Method getterMethod = instanceClass .getMethod (getterMethodName , new Class <?>[0 ]);
154+ return getterMethod .invoke (instance , new Class <?>[0 ]);
155+ } catch (NoSuchMethodException e ) {
156+ try {
157+ return field .get (instance );
158+ } catch (IllegalAccessException e1 ) {
159+ throw new ExpressionException ("Illegal access field \" %s\" in class \" %s\" ." , field .getName (), instanceClass .getName ());
160+ }
161+ } catch (Exception e ) {
162+ throw new ExpressionException ("%s invoke failed." , getterMethodName );
163+ }
164+ }
147165}
0 commit comments