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

The following examples show how to use org.apache.ibatis.reflection.property.PropertyTokenizer. 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 File: BeanWrapper.java    From mybatis with 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 #2
Source File: BeanWrapper.java    From mybaties with 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 #3
Source File: MapWrapper.java    From mybatis with 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 #4
Source File: MapWrapper.java    From mybatis with 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 #5
Source File: MetaClass.java    From mybaties with 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 #6
Source File: MapWrapper.java    From mybatis with 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 #7
Source File: BeanWrapper.java    From mybaties with 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 #8
Source File: BeanWrapper.java    From mybatis with 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 #9
Source File: MetaClass.java    From mybatis with 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 #10
Source File: MapWrapper.java    From mybaties with 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 #11
Source File: MapWrapper.java    From mybaties with 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 #12
Source File: MapWrapper.java    From mybaties with 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 #13
Source File: MetaObject.java    From mybatis with 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 #14
Source File: MetaObject.java    From mybaties with 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 File: MetaObject.java    From mybaties with 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);
  }
}
 
Example #16
Source File: BeanWrapper.java    From mybatis with 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 #17
Source File: MetaClass.java    From mybatis with 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 #18
Source File: BeanWrapper.java    From mybaties with 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 #19
Source File: MetaObject.java    From mybatis with 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);
  }
}
 
Example #20
Source File: MapWrapper.java    From mybaties with 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 File: BeanWrapper.java    From mybatis with 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 #22
Source File: MetaClass.java    From mybatis with 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 #23
Source File: MetaClass.java    From mybatis with 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 #24
Source File: BeanWrapper.java    From mybaties with 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 #25
Source File: MetaClass.java    From mybatis with 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 #26
Source File: MapWrapper.java    From mybatis with 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 #27
Source File: MapWrapper.java    From mybatis with 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 #28
Source File: BaseWrapper.java    From mybatis with 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 #29
Source File: BaseWrapper.java    From mybatis with 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 #30
Source File: BaseWrapper.java    From mybatis with 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());
  }
}