Java Code Examples for org.apache.ibatis.reflection.property.PropertyTokenizer

The following examples show how to use org.apache.ibatis.reflection.property.PropertyTokenizer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mybaties   Source File: MetaClass.java    License: Apache License 2.0 6 votes vote down vote up
private Class<?> getGetterType(PropertyTokenizer prop) {
  Class<?> type = reflector.getGetterType(prop.getName());
  if (prop.getIndex() != null && Collection.class.isAssignableFrom(type)) {
    Type returnType = getGenericGetterType(prop.getName());
    if (returnType instanceof ParameterizedType) {
      Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
      if (actualTypeArguments != null && actualTypeArguments.length == 1) {
        returnType = actualTypeArguments[0];
        if (returnType instanceof Class) {
          type = (Class<?>) returnType;
        } else if (returnType instanceof ParameterizedType) {
          type = (Class<?>) ((ParameterizedType) returnType).getRawType();
        }
      }
    }
  }
  return type;
}
 
Example 2
Source Project: mybaties   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example 3
Source Project: mybaties   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example 4
Source Project: mybaties   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (map.containsKey(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return true;
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return map.containsKey(prop.getName());
  }
}
 
Example 5
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasSetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasSetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasSetter(name);
      } else {
        return metaValue.hasSetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasSetter(name);
  }
}
 
Example 6
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasGetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasGetter(name);
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasGetter(name);
  }
}
 
Example 7
Source Project: mybaties   Source File: MetaObject.java    License: Apache License 2.0 6 votes vote down vote up
public void setValue(String name, Object value) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      if (value == null && prop.getChildren() != null) {
        // don't instantiate child path if value is null
        //如果上层就是null了,还得看有没有儿子,没有那就结束
        return;
      } else {
          //否则还得new一个,委派给ObjectWrapper.instantiatePropertyValue
        metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
      }
    }
    //递归调用setValue
    metaValue.setValue(prop.getChildren(), value);
  } else {
      //到了最后一层了,所以委派给ObjectWrapper.set
    objectWrapper.set(prop, value);
  }
}
 
Example 8
Source Project: mybatis   Source File: MetaClass.java    License: Apache License 2.0 6 votes vote down vote up
private Class<?> getGetterType(PropertyTokenizer prop) {
  Class<?> type = reflector.getGetterType(prop.getName());
  if (prop.getIndex() != null && Collection.class.isAssignableFrom(type)) {
    Type returnType = getGenericGetterType(prop.getName());
    if (returnType instanceof ParameterizedType) {
      Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
      if (actualTypeArguments != null && actualTypeArguments.length == 1) {
        returnType = actualTypeArguments[0];
        if (returnType instanceof Class) {
          type = (Class<?>) returnType;
        } else if (returnType instanceof ParameterizedType) {
          type = (Class<?>) ((ParameterizedType) returnType).getRawType();
        }
      }
    }
  }
  return type;
}
 
Example 9
Source Project: mybatis   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example 10
Source Project: mybatis   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example 11
Source Project: mybatis   Source File: MapWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (map.containsKey(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return true;
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return map.containsKey(prop.getName());
  }
}
 
Example 12
Source Project: mybatis   Source File: BeanWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasSetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasSetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasSetter(name);
      } else {
        return metaValue.hasSetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasSetter(name);
  }
}
 
Example 13
Source Project: mybatis   Source File: BeanWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasGetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasGetter(name);
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasGetter(name);
  }
}
 
Example 14
Source Project: mybatis   Source File: MetaObject.java    License: Apache License 2.0 6 votes vote down vote up
public void setValue(String name, Object value) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      if (value == null && prop.getChildren() != null) {
        // don't instantiate child path if value is null
        //如果上层就是null了,还得看有没有儿子,没有那就结束
        return;
      } else {
          //否则还得new一个,委派给ObjectWrapper.instantiatePropertyValue
        metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
      }
    }
    //递归调用setValue
    metaValue.setValue(prop.getChildren(), value);
  } else {
      //到了最后一层了,所以委派给ObjectWrapper.set
    objectWrapper.set(prop, value);
  }
}
 
Example 15
Source Project: Shop-for-JavaWeb   Source File: SQLHelper.java    License: MIT License 5 votes vote down vote up
/**
 * 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
 *
 * @param ps              表示预编译的 SQL 语句的对象。
 * @param mappedStatement MappedStatement
 * @param boundSql        SQL
 * @param parameterObject 参数对象
 * @throws java.sql.SQLException 数据库异常
 */
@SuppressWarnings("unchecked")
public static void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException {
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        Configuration configuration = mappedStatement.getConfiguration();
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        MetaObject metaObject = parameterObject == null ? null :
                configuration.newMetaObject(parameterObject);
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                PropertyTokenizer prop = new PropertyTokenizer(propertyName);
                if (parameterObject == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                    value = parameterObject;
                } else if (boundSql.hasAdditionalParameter(propertyName)) {
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) {
                    value = boundSql.getAdditionalParameter(prop.getName());
                    if (value != null) {
                        value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
                    }
                } else {
                    value = metaObject == null ? null : metaObject.getValue(propertyName);
                }
                @SuppressWarnings("rawtypes")
	TypeHandler typeHandler = parameterMapping.getTypeHandler();
                if (typeHandler == null) {
                    throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
                }
                typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
            }
        }
    }
}
 
Example 16
Source Project: mybaties   Source File: MetaClass.java    License: Apache License 2.0 5 votes vote down vote up
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaClass metaProp = metaClassForProperty(prop.getName());
    return metaProp.getSetterType(prop.getChildren());
  } else {
    return reflector.getSetterType(prop.getName());
  }
}
 
Example 17
Source Project: mybaties   Source File: MetaClass.java    License: Apache License 2.0 5 votes vote down vote up
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaClass metaProp = metaClassForProperty(prop);
    return metaProp.getGetterType(prop.getChildren());
  }
  // issue #506. Resolve the type inside a Collection Object
  return getGetterType(prop);
}
 
Example 18
Source Project: mybaties   Source File: MetaClass.java    License: Apache License 2.0 5 votes vote down vote up
public boolean hasSetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (reflector.hasSetter(prop.getName())) {
      MetaClass metaProp = metaClassForProperty(prop.getName());
      return metaProp.hasSetter(prop.getChildren());
    } else {
      return false;
    }
  } else {
    return reflector.hasSetter(prop.getName());
  }
}
 
Example 19
Source Project: mybaties   Source File: MetaClass.java    License: Apache License 2.0 5 votes vote down vote up
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (reflector.hasGetter(prop.getName())) {
      MetaClass metaProp = metaClassForProperty(prop);
      return metaProp.hasGetter(prop.getChildren());
    } else {
      return false;
    }
  } else {
    return reflector.hasGetter(prop.getName());
  }
}
 
Example 20
Source Project: mybaties   Source File: MapWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object get(PropertyTokenizer prop) {
    //如果有index,说明是集合,那就要分解集合,调用的是BaseWrapper.resolveCollection 和 getCollectionValue
  if (prop.getIndex() != null) {
    Object collection = resolveCollection(prop, map);
    return getCollectionValue(prop, collection);
  } else {
    return map.get(prop.getName());
  }
}
 
Example 21
Source Project: mybaties   Source File: MapWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PropertyTokenizer prop, Object value) {
  if (prop.getIndex() != null) {
    Object collection = resolveCollection(prop, map);
    setCollectionValue(prop, collection, value);
  } else {
    map.put(prop.getName(), value);
  }
}
 
Example 22
Source Project: mybaties   Source File: BaseWrapper.java    License: Apache License 2.0 5 votes vote down vote up
protected Object resolveCollection(PropertyTokenizer prop, Object object) {
  if ("".equals(prop.getName())) {
    return object;
  } else {
    return metaObject.getValue(prop.getName());
  }
}
 
Example 23
Source Project: mybaties   Source File: BaseWrapper.java    License: Apache License 2.0 5 votes vote down vote up
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
  if (collection instanceof Map) {
      //map['name']
    return ((Map) collection).get(prop.getIndex());
  } else {
    int i = Integer.parseInt(prop.getIndex());
    if (collection instanceof List) {
        //list[0]
      return ((List) collection).get(i);
    } else if (collection instanceof Object[]) {
      return ((Object[]) collection)[i];
    } else if (collection instanceof char[]) {
      return ((char[]) collection)[i];
    } else if (collection instanceof boolean[]) {
      return ((boolean[]) collection)[i];
    } else if (collection instanceof byte[]) {
      return ((byte[]) collection)[i];
    } else if (collection instanceof double[]) {
      return ((double[]) collection)[i];
    } else if (collection instanceof float[]) {
      return ((float[]) collection)[i];
    } else if (collection instanceof int[]) {
      return ((int[]) collection)[i];
    } else if (collection instanceof long[]) {
      return ((long[]) collection)[i];
    } else if (collection instanceof short[]) {
      return ((short[]) collection)[i];
    } else {
      throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
    }
  }
}
 
Example 24
Source Project: mybaties   Source File: BaseWrapper.java    License: Apache License 2.0 5 votes vote down vote up
protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
  if (collection instanceof Map) {
    ((Map) collection).put(prop.getIndex(), value);
  } else {
    int i = Integer.parseInt(prop.getIndex());
    if (collection instanceof List) {
      ((List) collection).set(i, value);
    } else if (collection instanceof Object[]) {
      ((Object[]) collection)[i] = value;
    } else if (collection instanceof char[]) {
      ((char[]) collection)[i] = (Character) value;
    } else if (collection instanceof boolean[]) {
      ((boolean[]) collection)[i] = (Boolean) value;
    } else if (collection instanceof byte[]) {
      ((byte[]) collection)[i] = (Byte) value;
    } else if (collection instanceof double[]) {
      ((double[]) collection)[i] = (Double) value;
    } else if (collection instanceof float[]) {
      ((float[]) collection)[i] = (Float) value;
    } else if (collection instanceof int[]) {
      ((int[]) collection)[i] = (Integer) value;
    } else if (collection instanceof long[]) {
      ((long[]) collection)[i] = (Long) value;
    } else if (collection instanceof short[]) {
      ((short[]) collection)[i] = (Short) value;
    } else {
      throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
    }
  }
}
 
Example 25
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object get(PropertyTokenizer prop) {
    //如果有index(有中括号),说明是集合,那就要解析集合,调用的是BaseWrapper.resolveCollection 和 getCollectionValue
  if (prop.getIndex() != null) {
    Object collection = resolveCollection(prop, object);
    return getCollectionValue(prop, collection);
  } else {
      //否则,getBeanProperty
    return getBeanProperty(prop, object);
  }
}
 
Example 26
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PropertyTokenizer prop, Object value) {
    //如果有index,说明是集合,那就要解析集合,调用的是BaseWrapper.resolveCollection 和 setCollectionValue
  if (prop.getIndex() != null) {
    Object collection = resolveCollection(prop, object);
    setCollectionValue(prop, collection, value);
  } else {
      //否则,setBeanProperty
    setBeanProperty(prop, object, value);
  }
}
 
Example 27
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getSetterType(name);
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    return metaClass.getSetterType(name);
  }
}
 
Example 28
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getGetterType(name);
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    return metaClass.getGetterType(name);
  }
}
 
Example 29
Source Project: mybaties   Source File: BeanWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
  MetaObject metaValue;
  Class<?> type = getSetterType(prop.getName());
  try {
    Object newObject = objectFactory.create(type);
    metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory());
    set(prop, newObject);
  } catch (Exception e) {
    throw new ReflectionException("Cannot set value of property '" + name + "' because '" + name + "' is null and cannot be instantiated on instance of " + type.getName() + ". Cause:" + e.toString(), e);
  }
  return metaValue;
}
 
Example 30
Source Project: mybaties   Source File: MetaObject.java    License: Apache License 2.0 5 votes vote down vote up
public Object getValue(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        //如果上层就是null了,那就结束,返回null
      return null;
    } else {
        //否则继续看下一层,递归调用getValue
     return metaValue.getValue(prop.getChildren());
    }
  } else {
    return objectWrapper.get(prop);
  }
}