Java Code Examples for net.sf.cglib.beans.BeanMap#create()

The following examples show how to use net.sf.cglib.beans.BeanMap#create() . 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: ELCellPostParser.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(Context context) {
	MappingRule mappingRule = context.getCurrentMappingRule();
	Object value = context.getValue();
	String cellPostParserParam = mappingRule.getCellPostParserParam();
	JexlContext jexlContext = expressionHandler.getJexlContext();
	jexlContext.set("context", context);
	jexlContext.set("value", value);
	Expression expression = expressionHandler.compile(cellPostParserParam);
	if (expression == null) {
		value = cellPostParserParam;
	} else {
		value = expression.evaluate();
	}
	context.setValue(value);
	BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
	if (value != Constants.IGNORE_ERROR_FORMAT_DATA) {
		beanMap.put(mappingRule.getPropertyName(), value);
	}
}
 
Example 2
Source File: DefaultCellProcessor.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
protected void cellParse(Context context) {
	MappingRule mappingRule = context.getCurrentMappingRule();
	BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
	String propertyName = mappingRule.getPropertyName();
	Class<?> type = beanMap.getPropertyType(propertyName);
	Object value = context.getValue();
	value = value == null ? null : value.toString();
	for (TypeConverter typeConverter : typeConverters) {
		if (typeConverter.support(type)) {
			try {
				value = typeConverter.fromText(type, mappingRule.getMappingValueIfNeed((String) value));
			} catch (Exception e) {
				if (mappingRule.isIgnoreErrorFormatData()) {
					log.debug(e.getMessage());
					value = Constants.IGNORE_ERROR_FORMAT_DATA;
				} else {
					throw new DataFormatException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol(), (String) value);
				}
			}
			break;
		}
	}
	context.setValue(value);
}
 
Example 3
Source File: AbstractCellPostParser.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(Context context) {
	if (context.getValue() != null) {
		MappingRule mappingRule =context.getCurrentMappingRule();
		BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
		if (context.getValue() != Constants.IGNORE_ERROR_FORMAT_DATA) {
			Field field = ReflectionUtils.findField(context.getEntityClass(), mappingRule.getPropertyName());
			Column column = field.getAnnotation(Column.class);
			if (column.nullable()) {
				if (context.getValue() == null) {
					throw new DataNullableException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol());
				}
			}
			
			if (field.getType() == String.class && !field.isAnnotationPresent(Lob.class)) {
				String value = (String) context.getValue();
				if (value.getBytes().length > column.length()) {
					throw new DataLengthException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol(), value, column.length());
				}
			}
			beanMap.put(mappingRule.getPropertyName(), context.getValue());
		}
	}
}
 
Example 4
Source File: DefaultCellPostParser.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(Context context) {
	if (context.getValue() != null) {
		MappingRule mappingRule =context.getCurrentMappingRule();
		BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
		if (context.getValue() != Constants.IGNORE_ERROR_FORMAT_DATA) {
			beanMap.put(mappingRule.getPropertyName(), context.getValue());
		}
	}
}
 
Example 5
Source File: MapToBeanCopier.java    From Copiers with Apache License 2.0 5 votes vote down vote up
@Override
public void copy(Map<String, Object> source, T bean) {
    checkNotNull(source, "source map cannot be null!");
    checkNotNull(bean, "target bean cannot be null!");
    try {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(source);
    } catch (Exception e) {
        throw new CopierException("create object fail, class: " + bean.getClass().getName(), e);
    }
}
 
Example 6
Source File: MapToBeanCopier.java    From Copiers with Apache License 2.0 5 votes vote down vote up
@Override
public T copy(Map<String, Object> source) {
    checkNotNull(source, "source map cannot be null!");
    try {
        T bean = ReflectionUtil.newInstance(targetClass);
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(source);
        return bean;
    } catch (Exception e) {
        throw new CopierException("create object fail, class: " + targetClass.getName(), e);
    }
}
 
Example 7
Source File: BeanToMapCopier.java    From Copiers with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void copy(F bean, Map<String, Object> target) {
    checkNotNull(bean, "source bean cannot be null!");
    checkNotNull(target, "target map cannot be null!");
    try {
        BeanMap beanMap = BeanMap.create(bean);
        target.putAll(beanMap);
    } catch (Exception e) {
        throw new CopierException("create object fail, class: " + bean.getClass().getName(), e);
    }
}
 
Example 8
Source File: BeanToMapCopier.java    From Copiers with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Map<String, Object> copy(F bean, Supplier<Map<String, Object>> mapFactory) {
    checkNotNull(bean, "source bean cannot be null!");
    checkNotNull(mapFactory, "map factory cannot be null!");
    try {
        BeanMap beanMap = BeanMap.create(bean);
        Map<String, Object> map = mapFactory.get();
        map.putAll(beanMap);
        return map;
    } catch (Exception e) {
        throw new CopierException("create object fail, class: " + bean.getClass().getName(), e);
    }
}
 
Example 9
Source File: ParseRecordPolicyImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Context context) throws ClassNotFoundException {
	List<Record> records = context.getRecords();
	
	for (int i = context.getStartRow(); i < records.size(); i++) {
		Record record = records.get(i);
		Object entity = BeanUtils.newInstance(context.getEntityClass());
		context.setCurrentEntity(entity);
		context.setCurrentRecord(record);
		String idProperty = JpaUtil.getIdName(context.getEntityClass());
		BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
		if (beanMap.getPropertyType(idProperty) == String.class) {
			beanMap.put(idProperty, UUID.randomUUID().toString());
		}
		for (MappingRule mappingRule : context.getMappingRules()) {
			Cell cell = record.getCell(mappingRule.getExcelColumn());

			context.setCurrentMappingRule(mappingRule);
			context.setCurrentCell(cell);
			cellPreprocess(context);
			cellProcess(context);
			cellPostprocess(context);
			
		}
		JpaUtil.persist(entity);
		if (i % 100 == 0) {
			JpaUtil.persistAndFlush(entity);
			JpaUtil.getEntityManager(entity).clear();
		} else {
			JpaUtil.persist(entity);
		}
	}

}
 
Example 10
Source File: Context.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T getCurrentEntityId() {
	String idProperty = JpaUtil.getIdName(entityClass);
	BeanMap beanMap = BeanMap.create(currentEntity);
	return (T) beanMap.get(idProperty);
	
}
 
Example 11
Source File: LinqImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
private void initCollectInfos(Collection<?> list) {
	for (Object entity : list) {
		BeanMap beanMap = BeanMap.create(entity);
		for (CollectInfo collectInfo : collectInfos) {
			for (String property : collectInfo.getProperties()) {
				Object value = beanMap.get(property);
				if (value != null) {
					collectInfo.add(value);
				}
			}
		}
	}
}
 
Example 12
Source File: Wirte.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleWrite1() {
    LargeData ss = new LargeData();
    ss.setStr23("ttt");
    Map map = BeanMap.create(ss);
    System.out.println(map.containsKey("str23"));
    System.out.println(map.containsKey("str22"));
    System.out.println(map.get("str23"));
    System.out.println(map.get("str22"));
}
 
Example 13
Source File: MapUtils.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * 将对象装换为map
 *
 * @param bean
 * @return
 */
public static <T> Map<String, Object> beanToMap(T bean) {
    Map<String, Object> map = new LinkedHashMap<>();
    if (bean != null) {
        BeanMap beanMap = BeanMap.create(bean);
        for (Object key : beanMap.keySet()) {
            map.put(key + "", beanMap.get(key));
        }
    }
    return map;
}
 
Example 14
Source File: MapUtils.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * 将对象装换为linkedmap
 * map顺序按照 key值的acsii码进行排序
 * @param bean
 * @return
 */
public static <T> Map<String, Object> beanToLinkedMap(T bean) {
    Map<String, Object> map = new LinkedHashMap<>();
    if (bean != null) {
        BeanMap beanMap = BeanMap.create(bean);
        Object[] keys = beanMap.keySet().toArray();
        Arrays.sort(keys);
        for (Object key : keys) {
            map.put(key + "", beanMap.get(key));
        }
    }
    return map;
}
 
Example 15
Source File: DictAnnotation.java    From submarine with Apache License 2.0 4 votes vote down vote up
public DictAnnotation(Map propertyMap) {
  this.object = generateBean(propertyMap);
  this.beanMap = BeanMap.create(this.object);
}
 
Example 16
Source File: NoticeServiceImpl.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional
public void send(Notice notice) {
	Message message = new Message(notice.getType(), BeanMap.create(notice));
	noticeService.receive(message);
}
 
Example 17
Source File: MapUtils.java    From nuls-v2 with MIT License 2 votes vote down vote up
/**
 * 将map装换为javabean对象
 *
 * @param map
 * @param bean
 * @return
 */
public static <T> T mapToBean(Map<String, Object> map, T bean) {
    BeanMap beanMap = BeanMap.create(bean);
    beanMap.putAll(map);
    return bean;
}