⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectutil.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.util;

import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.PropertyUtils;

public class ObjectUtil {
	
	private static final String START_TAG = "<";

	private static final String END_TAG1 = ">\n";

	private static final String END_TAG = ">";

	private static final String CLOSE_TAG = "</";

	public ObjectUtil() {
	}

	public static String toXMLString(Class aclass, Object object) {
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append("<").append(aclass.getName()).append(">\n");
			PropertyDescriptor descriptors[] = PropertyUtils.getPropertyDescriptors(object);
			for (int index = 0; index < descriptors.length; index++)
				if (descriptors[index].getPropertyType() != (java.lang.Class.class)) {
					buffer.append("<").append(descriptors[index].getName()).append(">");
					buffer.append(PropertyUtils.getProperty(object, descriptors[index].getName()));
					buffer.append("</").append(descriptors[index].getName()).append(">\n");
				}

			buffer.append("</").append(aclass.getName()).append(">\n");
			return buffer.toString();
		}catch(Exception e) {
			return "";
		}
	}
	
	public static boolean compareProperties(Object dest,Object ori) {
		System.out.println("*****************start to compare");
		boolean result = true;
		if (dest == null)
			return false;
		if (ori == null)
			return false;
		try {
			PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(ori);
			for (int i = 0; i < origDescriptors.length; i++) {
				if (origDescriptors[i].getPropertyType() != Class.class ) {
					String name = origDescriptors[i].getName();
					Object value = PropertyUtils.getProperty(ori, name);
					Object objValue = PropertyUtils.getProperty(dest, name);
					if(value != null && objValue == null) {
						result = false;
						break;
					}
					if(value == null && objValue != null) {
						result = false;
						break;
					}
					
					if(value != null && !value.equals(objValue)) {
						System.out.println("name=" + name + "[" + origDescriptors[i].getPropertyType() + "] value[" + value + "] compare objValue[" + objValue + "]");
						result = false;
						break;
					}
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
			System.out.println("*****************end to compare");
			return false;
		}
		System.out.println("*****************end to compare");
		return result;
	}
	
	public static Object createObject(String className) {
		Object result = null;
		try {
			result = Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
		} catch (ClassNotFoundException e) {
			result = null;
		} catch (InstantiationException e) {
			result = null;
		} catch (IllegalAccessException e) {
			result = null;
		}
		
		return result;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -