UtilObject.java 11.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
package com.taover.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class UtilObject {
	/**
	 * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同;
	 * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer
	 * @param source
	 * @param dest
	 * @param allowNull
	 * @param exceptFieldNameArray
	 * @return 如果source==dest或者source==null或者dest==null,则返回false
	 */
	public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], boolean isNewInstance){
		//引用检查
		if(source==null || dest==null || source==dest){
			return null;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		Field[] destField = dest.getClass().getDeclaredFields();
		
		//获取目标对象
		String[] destFieldNameArray = new String[destField.length];
		String[] destFieldTypeArray = new String[destField.length];
		for(int i=0; i<destField.length; ++i){
			destFieldNameArray[i] = destField[i].getName();
			destFieldTypeArray[i] = destField[i].getType().getName();
		}
	
		Object newDest = null;
		//注入属性
		if(isNewInstance){
			try {
				newDest = dest.getClass().newInstance();
				for(int i=0; i<destField.length; ++i){
					destField[i].setAccessible(true);
					Object tempValue = destField[i].get(dest);
					destField[i].set(newDest, tempValue);
				}
			} catch (Exception e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
		
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			String sourceFieldType = sourceField[i].getType().getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//检查源field字段名是否在目标field组中存在
			int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray);
			if(nameIndex == -1){
				continue;
			}else{
				//如果是final关键词修饰,则返回
				if(Modifier.isFinal(destField[nameIndex].getModifiers())){
					continue;
				}
				
				//如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象
				if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){
					destField[nameIndex].setAccessible(true);
					try {
						destField[nameIndex].set(dest, sourceFieldValue);
						if(isNewInstance){
							destField[nameIndex].set(newDest, sourceFieldValue);	
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
		return newDest;
	}
	
	/**
	 * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同;
	 * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer
	 * @param source
	 * @param destClass
	 * @param allowNull
	 * @param exceptFieldNameArray
	 * @return 如果source==null或者destClass==null,则返回false
	 */
	public static <E> E fieldCopy(Object source, Class<E> destClass, boolean allowNull, String exceptFieldNameArray[]) throws Exception{
		//引用检查
		if(source==null || destClass==null){
			return null;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		Field[] destField = destClass.getDeclaredFields();
		
		//获取目标对象
		String[] destFieldNameArray = new String[destField.length];
		String[] destFieldTypeArray = new String[destField.length];
		for(int i=0; i<destField.length; ++i){
			destFieldNameArray[i] = destField[i].getName();
			destFieldTypeArray[i] = destField[i].getType().getName();
		}
	
		E newDest = destClass.newInstance();
				
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			String sourceFieldType = sourceField[i].getType().getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//检查源field字段名是否在目标field组中存在
			int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray);
			if(nameIndex == -1){
				continue;
			}else{
				//如果是final关键词修饰,则返回
				if(Modifier.isFinal(destField[nameIndex].getModifiers())){
					continue;
				}
				
				//如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象
				if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){
					destField[nameIndex].setAccessible(true);
					try {
						destField[nameIndex].set(newDest, sourceFieldValue);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		return newDest;
	}
	
	/**
	 * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性
	 * 在字符串驼峰命名转化成下划线形式
	 * 如 helloWord -> hello_word
	 * @param source
	 * @param isCopyNull
	 * @param exceptFieldNameArray
	 * @return
	 */
	public static List<Object[]> objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){
		List<Object[]> result = new ArrayList<Object[]>();
		//参数检验
		if(source == null){
			return result;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!isCopyNull && sourceFieldValue == null){
				continue;
			}
			sourceFieldName = UtilString.underscoreName(sourceFieldName).toLowerCase();
			//添加属性
			result.add(new Object[]{sourceFieldName, sourceFieldValue});
		}
		
		return result;
	}
	
	/**
	 * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性
	 * @param source
	 * @param allowNull
	 * @param exceptFieldNameArray
	 * @return
	 */
	public static List<Object[]> fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){
		List<Object[]> result = new ArrayList<Object[]>();
		//参数检验
		if(source == null){
			return result;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//添加属性
			result.add(new Object[]{sourceFieldName, sourceFieldValue});
		}
		
		return result;
	}	
	
	public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{
		if(beanClass == null){
			return null;
		}
		E result = beanClass.newInstance();
		Iterator<String> keyIter = dataMap.keySet().iterator();
		while(keyIter.hasNext()){
			String keyName = keyIter.next();
			String camelName = UtilString.camelName(keyName);
			Object keyValue = dataMap.get(keyName);
			
			if(keyValue == null){
				continue;
			}
			
			Field keyField = beanClass.getDeclaredField(camelName);
			String keyTypeName = keyField.getType().getSimpleName();
			
			//如果是final关键词修饰,则返回
			if(Modifier.isFinal(keyField.getModifiers())){
				continue;
			}
			
			if(keyTypeName.equals("String")){
				keyField.set(result, keyValue.toString());
			}else if(keyTypeName.equals("Integer")){
				keyField.set(result, Integer.valueOf(keyValue.toString()));
			}
		}
		return result;
	}
	
	public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass) throws Exception{
		if(beanClass == null){
			return null;
		}
		E result = beanClass.newInstance();
		Iterator<String> keyIter = dataMap.keySet().iterator();
		while(keyIter.hasNext()){
			String keyName = keyIter.next();
			String camelName = UtilString.camelName(keyName);
			Object keyValue = dataMap.get(keyName);
			
			if(keyValue == null){
				continue;
			}
			
			try {
				Field keyField = beanClass.getDeclaredField(camelName);
				
				//如果是final关键词修饰,则返回
				if(Modifier.isFinal(keyField.getModifiers())){
					continue;
				}
				
				keyField.setAccessible(true);
				keyField.set(result, keyValue);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public static void main(String args[]){
		
		class Temp2{
			final Integer a;
			Integer c;
			public Temp2(Integer a, Integer c){
				this.a = a;
				this.c = c;
			}
			@Override
			public String toString(){
				return "a="+a+":c="+c;
			}
		}

		class Temp{
			Integer a;
			Integer b;
			public Temp(Integer a, Integer b){
				this.a = a;
				this.b = b;
			}
			@Override
			public String toString(){
				return "a="+a+":b="+b;
			}
		}
		
		Temp temp1 = new Temp(null,2);
		Temp temp2 = new Temp(3,4);
		Temp temp3 = new Temp(5,6);
		Temp2 temp4 = new Temp2(7,8);
		
		try {
			Field a= temp4.getClass().getDeclaredField("a");
			System.out.print(Modifier.isFinal(a.getModifiers()));
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*fieldCopy(temp1, temp2, false, null,false);
		System.out.println(temp1.toString());
		System.out.println(temp2.toString());
		
		fieldCopy(temp3, temp4, false, null,false);
		System.out.println(temp3.toString());
		System.out.println(temp4.toString());
		
		List<Object[]> temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"});
		for(int i=0; i<temp.size(); ++i){
			System.out.println(temp.get(i)[0]+":"+temp.get(i)[1]);	
		}	*/	
	}
}