本文整理匯總了Java中org.apache.commons.beanutils.BeanUtils.setProperty方法的典型用法代碼示例。如果您正苦於以下問題:Java BeanUtils.setProperty方法的具體用法?Java BeanUtils.setProperty怎麽用?Java BeanUtils.setProperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.beanutils.BeanUtils
的用法示例。
在下文中一共展示了BeanUtils.setProperty方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: transformMapListToBeanList
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/**
* 將所有記錄包裝成T類對象,並將所有T類對象放入一個數組中並返回,傳入的T類的定義必須符合JavaBean規範,
* 因為本函數是調用T類對象的setter器來給對象賦值的
*
* @param clazz T類的類對象
* @param listProperties 所有記錄
* @return 返回所有被實例化的對象的數組,若沒有對象,返回一個空數組
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static <T> List<T> transformMapListToBeanList(Class<T> clazz, List<Map<String, Object>> listProperties)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
// 存放被實例化的T類對象
T bean = null;
// 存放所有被實例化的對象
List<T> list = new ArrayList<T>();
// 將實例化的對象放入List數組中
if (listProperties.size() != 0) {
Iterator<Map<String, Object>> iter = listProperties.iterator();
while (iter.hasNext()) {
// 實例化T類對象
bean = clazz.newInstance();
// 遍曆每條記錄的字段,並給T類實例化對象屬性賦值
for (Map.Entry<String, Object> entry : iter.next().entrySet()) {
BeanUtils.setProperty(bean, entry.getKey(), entry.getValue());
}
// 將賦過值的T類對象放入數組中
list.add(bean);
}
}
return list;
}
示例2: setProperty
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/**
*
* @param retval
* @param key
* @param value
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private void setProperty(T retval, String key, Object value) throws IllegalAccessException, InvocationTargetException {
String property = key2property(key);
for (final Field f : this.type.getDeclaredFields()) {
final SerializedName annotation = f.getAnnotation(SerializedName.class);
if (annotation != null && property.equalsIgnoreCase(annotation.value())) {
property = f.getName();
break;
}
}
if (propertyExists(retval, property)) {
BeanUtils.setProperty(retval, property, value);
} else {
LOG.warn(retval.getClass() + " does not support public setter for existing property [" + property + "]");
}
}
示例3: setProperties
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public static void setProperties(Object obj, Map map){
Field[] fields = obj.getClass().getDeclaredFields();
for(Field field : fields) {
Column column = field.getAnnotation(Column.class);
String dbColumnname = null;
if ("".equals(column.name()))
dbColumnname = field.getName();
else dbColumnname = column.name();
try {
Object value = map.get(dbColumnname);
if (value != null) {
BeanUtils.setProperty(obj, field.getName(), value);
}
} catch (Exception e) {
log.catching(e);
}
}
}
示例4: convertWithMethod
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
private static <T> T convertWithMethod(Row row, Class<T> clazz) throws IllegalAccessException, InstantiationException, ParseException, InvocationTargetException {
Field[] fields = new Field[]{};
Class temp = clazz;
while(temp != null){
fields = ArrayUtils.addAll(temp.getDeclaredFields(),fields);
temp = temp.getSuperclass();
}
Object object = clazz.newInstance();
for (Field field : fields) {
ExcelCellField filedExcelAnnotation = getAnnotationCellFiled(field.getAnnotations());
if (filedExcelAnnotation == null) {
continue;
}
Class<?> fieldType = field.getType();
Integer index = filedExcelAnnotation.index();
String propertyName = field.getName();
Object value = getValue(fieldType, row.getCell(index), filedExcelAnnotation);
if (value != null) {
BeanUtils.setProperty(object, propertyName, value);
}
}
return (T) object;
}
示例5: injectValue
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
protected void injectValue(Object bean, PropertyDescriptor pd) {
Method writeMethod = pd.getWriteMethod();
Value valueAnnotate = writeMethod.getAnnotation(Value.class);
if (valueAnnotate == null) {
return;
}
String value = valueAnnotate.value();
if (value.startsWith("${") && value.endsWith("}")) {
value = value.substring(2, value.length() - 1);
value = getProperty(value);
}
try {
BeanUtils.setProperty(bean, pd.getName(), value);
} catch (Exception e) {
throw new InjectBeanException("Could not inject value: " + writeMethod + "; nested exception is " + e, e);
}
}
示例6: createBeanFromRequest
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public static <T> T createBeanFromRequest(NaviHttpRequest request, Class<? extends T> clazz) throws NaviSystemException {
try {
T bean = clazz.newInstance();
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
if (request.getParameter(field.getName()) != null) {
String val = request.getParameter(field.getName());
if (val == null) {
continue;
}
Object obj = val;
if (field.getType().equals(JSONArray.class) || field.getType().equals(JSONObject.class)) {
obj = JSON.parse(val);
}
BeanUtils.setProperty(bean, field.getName(), obj);
}
}
return bean;
} catch (Exception e) {
throw new NaviSystemException("system, error, " + e.getMessage(), NaviError.SYSERROR);
}
}
示例7: convert
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public static BatteryInfoReq convert(String str) {
if (StringUtils.isBlank(str)) {
return null;
}
BatteryInfoReq req = new BatteryInfoReq();
String[] arr = StringUtils.split(str, "&");
for (int i = 0; i < arr.length; i++) {
String[] pair = StringUtils.split(arr[i], "=");
try {
BeanUtils.setProperty(req, pair[0], pair[1]);
} catch (Exception e) {
logger.error(ExceptionUtils.getStackTrace(e));
}
}
return req;
}
示例8: toBean
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/***
* 將JSON文本反序列化為主從關係的實體
* @param 泛型T 代表主實體類型
* @param 泛型D 代表從實體類型
* @param jsonString JSON文本
* @param mainClass 主實體類型
* @param detailName 從實體類在主實體類中的屬性名稱
* @param detailClass 從實體類型
* @return
*/
public static <T, D> T toBean(String jsonString, Class<T> mainClass,
String detailName, Class<D> detailClass)
{
JSONObject jsonObject = JSONObject.fromObject(jsonString);
JSONArray jsonArray = (JSONArray) jsonObject.get(detailName);
T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
List<D> detailList = JSONUtils.toList(jsonArray, detailClass);
try
{
BeanUtils.setProperty(mainEntity, detailName, detailList);
}
catch (Exception ex)
{
throw new RuntimeException("主從關係JSON反序列化實體失敗!");
}
return mainEntity;
}
示例9: delete
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
@Override
@Transactional
public void delete(T entity) {
try {
BeanUtils.setProperty(entity, "deleted", true);
BeanUtils.setProperty(entity, "dateUpdated", LocalDateTime.now());
Account account = SecurityContextUtil.getUserAccount();
if(account!=null)
BeanUtils.setProperty(entity, "userId", account.getAccountId());
entityManager.merge(entity);
} catch (Exception e) {
entityManager.detach(entity);
entityManager.flush();
}
}
示例10: setField
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public void setField(NamedNodeMap namedNodeMap, String fieldName) {
Attr attr = (Attr) namedNodeMap.getNamedItem(fieldName);
if (attr != null) {
boolean isDefaultValue = !attr.getSpecified();
boolean success = setDefaultFlag(fieldName, isDefaultValue);
if (success) {
// Json.print(baseInfo, "baseInfo");
Object value = this.toBaseInfoValue(fieldName, attr.getNodeValue());
try {
BeanUtils.setProperty(baseInfo, fieldName, value);
}
catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}
示例11: toBean
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/***
* 將JSON文本反序列化為主從關係的實體
*
* @param <T>泛型T 代表主實體類型
* @param <D1>泛型D1 代表從實體類型
* @param <D2>泛型D2 代表從實體類型
* @param jsonString
* JSON文本
* @param mainClass
* 主實體類型
* @param detailName1
* 從實體類在主實體類中的屬性
* @param detailClass1
* 從實體類型
* @param detailName2
* 從實體類在主實體類中的屬性
* @param detailClass2
* 從實體類型
* @return
*/
public static <T, D1, D2> T toBean(String jsonString, Class<T> mainClass,
String detailName1, Class<D1> detailClass1, String detailName2,
Class<D2> detailClass2) {
JSONObject jsonObject = JSONObject.fromObject(jsonString);
JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);
T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);
List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);
try {
BeanUtils.setProperty(mainEntity, detailName1, detailList1);
BeanUtils.setProperty(mainEntity, detailName2, detailList2);
} catch (Exception ex) {
throw new RuntimeException("主從關係JSON反序列化實體失敗!");
}
return mainEntity;
}
示例12: HeaderToSimpleBean
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/**
* 轉換請求中的header中的參數為簡單對象(隻有對象中的簡單類型會被賦值),參數值為數組的隻取第一個
*
* @param request 請求對象
* @param claz 目標對象類型類
* @param <T> 目標對象類型
* @return 目標對象
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static <T> T HeaderToSimpleBean(HttpServletRequest request, Class<T> claz) throws IllegalAccessException, InstantiationException {
Enumeration it = request.getHeaderNames();
T object = claz.newInstance();
while (it.hasMoreElements()) {
String hKey = (String) it.nextElement();
String hValue = request.getHeader(hKey);
if (StringUtils.isNotBlank(hKey) && StringUtils.isNotBlank(hValue)) {
try {
BeanUtils.setProperty(object, hKey, hValue);
} catch (Exception e) {
}
}
}
return object;
}
示例13: setProperty
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
private void setProperty(Object object, String propertyPath, Object value) {
try {
if (log.isTraceEnabled()) {
log.trace("Applying property [{}] value [{}] on object of type [{}]",
new Object[]{propertyPath, value, object.getClass().getName()});
}
BeanUtils.setProperty(object, propertyPath, value);
} catch (Exception e) {
String msg = "Unable to set property '" + propertyPath + "' with value [" + value + "] on object " +
"of type " + (object != null ? object.getClass().getName() : null) + ". If " +
"'" + value + "' is a reference to another (previously defined) object, prefix it with " +
"'" + OBJECT_REFERENCE_BEGIN_TOKEN + "' to indicate that the referenced " +
"object should be used as the actual value. " +
"For example, " + OBJECT_REFERENCE_BEGIN_TOKEN + value;
throw new ConfigurationException(msg, e);
}
}
示例14: setFieldValue
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public static boolean setFieldValue(Object obj, Field field, Object value){
if(null == obj || null == field){
return false;
}
try{
if(field.isAccessible()){
//可訪問屬性
field.set(obj, value);
BeanUtils.setProperty(obj, field.getName(), value);
}else{
//不可訪問屬性
field.setAccessible(true);
field.set(obj, value);
BeanUtils.setProperty(obj, field.getName(), value);
field.setAccessible(false);
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
示例15: convertToType
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
/**
* Converts the input object into a <code>Map</code> object.
*
* @param <T> Target type of the conversion.
* @param type Data type to which this value should be converted.
* @param value TThe input value to be converted.
* @return The converted <code>Map</code>
* @throws Throwable if an error occurs converting to the specified type
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
if(mapClass == null) {
throw new IllegalAccessException("mapClass is not initialized by setListClass().");
}
final Class<T> sourceType = (Class<T>) value.getClass();
Map<String, Object> returnMap = new HashMap<>();
if(value instanceof LinkedTreeMap){
for (String key : ((LinkedTreeMap<String, Object>) value).keySet()) {
Object instanz = this.mapClass.newInstance();
Object val = ((LinkedTreeMap) value).get(key);
BeanUtils.setProperty(instanz,"name",key);
for (String key2 : ((LinkedTreeMap<String, Object>) val).keySet()) {
Object val2 = ((LinkedTreeMap) val).get(key2);
BeanUtils.setProperty(instanz,key2,val2);
}
returnMap = toClassMap(sourceType,instanz,returnMap);
}
return (T) returnMap;
}
if(value instanceof String){
return (T) toStringMap(sourceType,value.toString(),returnMap);
}
final String stringValue = value.toString().trim();
if (stringValue.length() == 0) {
return handleMissing(type);
}
return (T) toStringMap(sourceType,stringValue,returnMap);
}