ExpressionEvaluator.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.scripting.xmltags;

  17. import java.lang.reflect.Array;
  18. import java.math.BigDecimal;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.apache.ibatis.builder.BuilderException;

  23. /**
  24.  * @author Clinton Begin
  25.  */
  26. public class ExpressionEvaluator {

  27.   public static final ExpressionEvaluator INSTANCE = new ExpressionEvaluator();

  28.   public boolean evaluateBoolean(String expression, Object parameterObject) {
  29.     Object value = OgnlCache.getValue(expression, parameterObject);
  30.     if (value instanceof Boolean) {
  31.       return (Boolean) value;
  32.     }
  33.     if (value instanceof Number) {
  34.       return new BigDecimal(String.valueOf(value)).compareTo(BigDecimal.ZERO) != 0;
  35.     }
  36.     return value != null;
  37.   }

  38.   /**
  39.    * @deprecated Since 3.5.9, use the {@link #evaluateIterable(String, Object, boolean)}.
  40.    */
  41.   @Deprecated
  42.   public Iterable<?> evaluateIterable(String expression, Object parameterObject) {
  43.     return evaluateIterable(expression, parameterObject, false);
  44.   }

  45.   /**
  46.    * @since 3.5.9
  47.    */
  48.   public Iterable<?> evaluateIterable(String expression, Object parameterObject, boolean nullable) {
  49.     Object value = OgnlCache.getValue(expression, parameterObject);
  50.     if (value == null) {
  51.       if (nullable) {
  52.         return null;
  53.       }
  54.       throw new BuilderException("The expression '" + expression + "' evaluated to a null value.");
  55.     }
  56.     if (value instanceof Iterable) {
  57.       return (Iterable<?>) value;
  58.     }
  59.     if (value.getClass().isArray()) {
  60.       // the array may be primitive, so Arrays.asList() may throw
  61.       // a ClassCastException (issue 209). Do the work manually
  62.       // Curse primitives! :) (JGB)
  63.       int size = Array.getLength(value);
  64.       List<Object> answer = new ArrayList<>();
  65.       for (int i = 0; i < size; i++) {
  66.         Object o = Array.get(value, i);
  67.         answer.add(o);
  68.       }
  69.       return answer;
  70.     }
  71.     if (value instanceof Map) {
  72.       return ((Map) value).entrySet();
  73.     }
  74.     throw new BuilderException(
  75.         "Error evaluating expression '" + expression + "'.  Return value (" + value + ") was not iterable.");
  76.   }

  77. }