BaseWrapper.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.reflection.wrapper;

  17. import java.util.List;
  18. import java.util.Map;

  19. import org.apache.ibatis.reflection.MetaObject;
  20. import org.apache.ibatis.reflection.ReflectionException;
  21. import org.apache.ibatis.reflection.SystemMetaObject;
  22. import org.apache.ibatis.reflection.property.PropertyTokenizer;

  23. /**
  24.  * @author Clinton Begin
  25.  */
  26. public abstract class BaseWrapper implements ObjectWrapper {

  27.   protected static final Object[] NO_ARGUMENTS = {};
  28.   protected final MetaObject metaObject;

  29.   protected BaseWrapper(MetaObject metaObject) {
  30.     this.metaObject = metaObject;
  31.   }

  32.   protected Object resolveCollection(PropertyTokenizer prop, Object object) {
  33.     if ("".equals(prop.getName())) {
  34.       return object;
  35.     }
  36.     return metaObject.getValue(prop.getName());
  37.   }

  38.   protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
  39.     if (collection == null) {
  40.       throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
  41.           + prop.getName() + "' is null.");
  42.     }
  43.     if (collection instanceof Map) {
  44.       return ((Map) collection).get(prop.getIndex());
  45.     }
  46.     int i = Integer.parseInt(prop.getIndex());
  47.     if (collection instanceof List) {
  48.       return ((List) collection).get(i);
  49.     } else if (collection instanceof Object[]) {
  50.       return ((Object[]) collection)[i];
  51.     } else if (collection instanceof char[]) {
  52.       return ((char[]) collection)[i];
  53.     } else if (collection instanceof boolean[]) {
  54.       return ((boolean[]) collection)[i];
  55.     } else if (collection instanceof byte[]) {
  56.       return ((byte[]) collection)[i];
  57.     } else if (collection instanceof double[]) {
  58.       return ((double[]) collection)[i];
  59.     } else if (collection instanceof float[]) {
  60.       return ((float[]) collection)[i];
  61.     } else if (collection instanceof int[]) {
  62.       return ((int[]) collection)[i];
  63.     } else if (collection instanceof long[]) {
  64.       return ((long[]) collection)[i];
  65.     } else if (collection instanceof short[]) {
  66.       return ((short[]) collection)[i];
  67.     } else {
  68.       throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
  69.           + prop.getName() + "' is not Map, List or Array.");
  70.     }
  71.   }

  72.   protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
  73.     if (collection == null) {
  74.       throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
  75.           + prop.getName() + "' is null.");
  76.     }
  77.     if (collection instanceof Map) {
  78.       ((Map) collection).put(prop.getIndex(), value);
  79.     } else {
  80.       int i = Integer.parseInt(prop.getIndex());
  81.       if (collection instanceof List) {
  82.         ((List) collection).set(i, value);
  83.       } else if (collection instanceof Object[]) {
  84.         ((Object[]) collection)[i] = value;
  85.       } else if (collection instanceof char[]) {
  86.         ((char[]) collection)[i] = (Character) value;
  87.       } else if (collection instanceof boolean[]) {
  88.         ((boolean[]) collection)[i] = (Boolean) value;
  89.       } else if (collection instanceof byte[]) {
  90.         ((byte[]) collection)[i] = (Byte) value;
  91.       } else if (collection instanceof double[]) {
  92.         ((double[]) collection)[i] = (Double) value;
  93.       } else if (collection instanceof float[]) {
  94.         ((float[]) collection)[i] = (Float) value;
  95.       } else if (collection instanceof int[]) {
  96.         ((int[]) collection)[i] = (Integer) value;
  97.       } else if (collection instanceof long[]) {
  98.         ((long[]) collection)[i] = (Long) value;
  99.       } else if (collection instanceof short[]) {
  100.         ((short[]) collection)[i] = (Short) value;
  101.       } else {
  102.         throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
  103.             + prop.getName() + "' is not Map, List or Array.");
  104.       }
  105.     }
  106.   }

  107.   protected Object getChildValue(PropertyTokenizer prop) {
  108.     MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
  109.     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
  110.       return null;
  111.     }
  112.     return metaValue.getValue(prop.getChildren());
  113.   }

  114.   protected void setChildValue(PropertyTokenizer prop, Object value) {
  115.     MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
  116.     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
  117.       if (value == null) {
  118.         // don't instantiate child path if value is null
  119.         return;
  120.       }
  121.       metaValue = instantiatePropertyValue(null, new PropertyTokenizer(prop.getName()), metaObject.getObjectFactory());
  122.     }
  123.     metaValue.setValue(prop.getChildren(), value);
  124.   }
  125. }