net.sf.cglib.beans.BeanMap Java Examples

The following examples show how to use net.sf.cglib.beans.BeanMap. 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: JdPricesFieldRender.java    From gecco with MIT License 6 votes vote down vote up
@Override
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean, Field field) {
	ProductList jd = (ProductList)bean;
	StringBuffer sb = new StringBuffer();
	/*for(String code : jd.getCodes()) {
		sb.append("J_").append(code).append(",");
	}*/
	String skuIds = sb.toString();
	try {
		skuIds = URLEncoder.encode(skuIds, "UTF-8");
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
	String url = "http://p.3.cn/prices/mgets?skuIds="+skuIds;
	HttpRequest subRequest = request.subRequest(url);
	try {
		HttpResponse subReponse = DownloaderContext.download(subRequest);
		String json = subReponse.getContent();
		List<JDPrice> prices = JSON.parseArray(json, JDPrice.class);
		beanMap.put(field.getName(), prices);
	} catch(Exception ex) {
		ex.printStackTrace();
	}
}
 
Example #2
Source File: JsonFieldRender.java    From gecco with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Map<String, Object> fieldMap = new HashMap<String, Object>();
	Set<Field> jsonPathFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(JSONPath.class));
	String jsonStr = response.getContent();
	jsonStr = jsonp2Json(jsonStr);
	if (jsonStr == null) {
		return;
	}
	try {
		Object json = JSON.parse(jsonStr);
		for (Field field : jsonPathFields) {
			Object value = injectJsonField(request, field, json);
			if(value != null) {
				fieldMap.put(field.getName(), value);
			}
		}
	} catch(JSONException ex) {
		//throw new RenderException(ex.getMessage(), bean.getClass());
		RenderException.log("json parse error : " + request.getUrl(), bean.getClass(), ex);
	}
	beanMap.putAll(fieldMap);
}
 
Example #3
Source File: ImageFieldRender.java    From gecco with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object injectImageField(HttpRequest request, BeanMap beanMap, SpiderBean bean, Field field) {
	Object value = beanMap.get(field.getName());
	if(value == null) {
		return null;
	}
	if(value instanceof Collection) {
		Collection<Object> collection = (Collection<Object>)value;
		for(Object item : collection) {
			String imgUrl = downloadImage(request, field, item.toString());
			item = imgUrl;
		}
		return collection;
	} else {
		return downloadImage(request, field, value.toString());
	}
}
 
Example #4
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 #5
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 #6
Source File: BackfillFilter.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
private void doFill(LinqContext linqContext, Object entity, Class<?> cls,
		String property, PropertyDescriptor pd) {
	Object value;
	if (Collection.class.isAssignableFrom(pd.getPropertyType())) {
		value = linqContext.getList(cls, BeanMap.create(entity).get(property));
	} else {
		value = linqContext.get(cls, BeanMap.create(entity).get(property));
	}
	if (value != null) {
		try {
			pd.getWriteMethod().invoke(entity, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: RequestFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked" })
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Set<Field> requestFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Request.class));
	for(Field field : requestFields) {
		beanMap.put(field.getName(), request);
	}
}
 
Example #12
Source File: HtmlRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
public void fieldRender(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	htmlFieldRender.render(request, response, beanMap, bean);
	ajaxFieldRender.render(request, response, beanMap, bean);
	jsVarFieldRender.render(request, response, beanMap, bean);
	imageFieldRender.render(request, response, beanMap, bean);
}
 
Example #13
Source File: HtmlFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Map<String, Object> fieldMap = new HashMap<String, Object>();
	Set<Field> htmlFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(HtmlField.class));
	for (Field htmlField : htmlFields) {
		Object value = injectHtmlField(request, response, htmlField, bean.getClass());
		if(value != null) {
			fieldMap.put(htmlField.getName(), value);
		}
	}
	beanMap.putAll(fieldMap);
}
 
Example #14
Source File: ImageFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Map<String, Object> fieldMap = new HashMap<String, Object>();
	Set<Field> imageFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Image.class));
	for (Field imageField : imageFields) {
		Object value = injectImageField(request, beanMap, bean, imageField);
		if(value != null) {
			fieldMap.put(imageField.getName(), value);
		}
	}
	beanMap.putAll(fieldMap);
}
 
Example #15
Source File: JSVarFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Context cx = Context.enter();
	ScriptableObject scope = cx.initSafeStandardObjects();
	String windowScript = "var window = {};var document = {};";
	cx.evaluateString(scope, windowScript, "window", 1, null);
	HtmlParser parser = new HtmlParser(request.getUrl(), response.getContent());
	for (Element ele : parser.$("script")) {
		String sc = ele.html();
		if (StringUtils.isNotEmpty(sc)) {
			try {
				cx.evaluateString(scope, sc, "", 1, null);
			} catch (Exception ex) {
				// ex.printStackTrace();
			}
		}
	}
	Map<String, Object> fieldMap = new HashMap<String, Object>();
	Set<Field> jsVarFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(JSVar.class));
	for (Field jsVarField : jsVarFields) {
		Object value = injectJsVarField(request, beanMap, jsVarField, cx, scope);
		if(value != null) {
			fieldMap.put(jsVarField.getName(), value);
		}
	}
	beanMap.putAll(fieldMap);
	Context.exit();
}
 
Example #16
Source File: AjaxFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
	Map<String, Object> fieldMap = new HashMap<String, Object>();
	Set<Field> ajaxFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(Ajax.class));
	for (Field ajaxField : ajaxFields) {
		Object value = injectAjaxField(request, beanMap, ajaxField);
		if(value != null) {
			fieldMap.put(ajaxField.getName(), value);
		}
	}
	beanMap.putAll(fieldMap);
}
 
Example #17
Source File: AjaxFieldRender.java    From gecco with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object injectAjaxField(HttpRequest request, BeanMap beanMap, Field field) {
	Class clazz = field.getType();
	// ajax的属性类型必须是spiderBean
	Ajax ajax = field.getAnnotation(Ajax.class);
	String url = ajax.url();
	url = UrlMatcher.replaceParams(url, request.getParameters());
	url = UrlMatcher.replaceFields(url, beanMap);
	HttpRequest subRequest = request.subRequest(url);
	HttpResponse subReponse = null;
	try {
		subReponse = DownloaderContext.download(subRequest);
		RenderType type = RenderType.HTML;
		if (ReflectUtils.haveSuperType(clazz, JsonBean.class)) {
			type = RenderType.JSON;
		}
		Render render = RenderContext.getRender(type);
		return render.inject(clazz, subRequest, subReponse);
	} catch (DownloadException ex) {
		//throw new FieldRenderException(field, ex.getMessage(), ex);
		FieldRenderException.log(field, ex.getMessage(), ex);
		return null;
	} finally {
		if(subReponse != null) {
			subReponse.close();
		}
	}
}
 
Example #18
Source File: CglibUtil.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
/**
 * 将Bean属性值封装至Map
 * @param bean
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map describe(Object bean){
	Map target = new HashMap();
	target.putAll(BeanMap.create(bean));
	return target;
}
 
Example #19
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 #20
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 #21
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 #22
Source File: ModelBuildEventListener.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder,
    AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0,
            new CellData(CellDataTypeEnum.EMPTY), null,
            "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1);
    Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        CellData cellData = cellDataMap.get(index);
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            continue;
        }
        ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),
            excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration(),
            context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            map.put(excelContentProperty.getField().getName(), value);
        }
    }
    BeanMap.create(resultModel).putAll(map);
    return resultModel;
}
 
Example #23
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 #24
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 #25
Source File: BackfillFilter.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean invoke(LinqContext linqContext) {
	Object entity = linqContext.getEntity();
	for (CollectInfo collectInfo : collectInfos) {
		Class<?> cls = collectInfo.getEntityClass();
		
		if (cls != null) {
			List<PropertyDescriptor> pds = getPropertyMap(entity).get(cls);
			String[] properties = collectInfo.getProperties();
			if (CollectionUtils.isEmpty(pds)) {
				Object value = linqContext.get(cls, BeanMap.create(entity).get(properties[0]));
				if (value != null) {
					EntityUtils.setValue(entity, Introspector.decapitalize(cls.getSimpleName()), value);
				}
			} else if (pds.size() == 1) {
				doFill(linqContext, entity, cls, properties[0], pds.get(0));
			} else {
				for (String property : properties) {
					for (PropertyDescriptor pd : pds) {
						if (StringUtils.contains(property, pd.getName()) || StringUtils.contains(pd.getName(), property)) {
							doFill(linqContext, entity, cls, property, pd);
						}
					}
				}
			}
			
			
		}
		
	}
	
	return filter == null ? true : filter.invoke(linqContext);
}
 
Example #26
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 #27
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 #28
Source File: XLSX2CSV.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    XLSX2CSV xlsx2csv = new XLSX2CSV("/users/subo/1.xlsx", "/users/subo/1.csv");
    xlsx2csv.process();
    Context cotext = new Context();
    BeanMap.create(cotext).put("currentCell.row", 1);
    System.out.println(cotext.getCurrentCell());
    
}
 
Example #29
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 #30
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);
}