io.github.wujun728.sql.util.OgnlUtil Maven / Gradle / Ivy
The newest version!
package io.github.wujun728.sql.util;
import ognl.Ognl;
import ognl.OgnlException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OgnlUtil {
public static Object getValue(String expression, Map root) {
try {
Map context = Ognl.createDefaultContext(root);
Object value = Ognl.getValue(Ognl.parseExpression(expression), context, root);
return value;
} catch (OgnlException e) {
throw new RuntimeException(e.getMessage());
}
}
public static Boolean getBooleanValue(String expression, Map root) {
Object value = getValue(expression, root);
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof Number)
return !new BigDecimal(String.valueOf(value)).equals(BigDecimal.ZERO);
else
throw new RuntimeException("expression value is not boolean or number type: " + expression);
}
public static Iterable> getIterable(String expression, Map root) {
Object value = getValue(expression, root);
if (value == null)
throw new RuntimeException("The expression '" + expression + "' evaluated to a null value.");
if (value instanceof Iterable)
return (Iterable>) value;
if (value.getClass().isArray()) {
// the array may be primitive, so Arrays.asList() may throw
// a ClassCastException (issue 209). Do the work manually
// Curse primitives! :) (JGB)
int size = Array.getLength(value);
List