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

📄 valueobjectutil.java

📁 一个基于Java的新闻发布系统
💻 JAVA
字号:
/*
 *
 * Title: The ERP System of kelamayi Downhole Company [PMIP]
 *
 * Copyright: Copyright (c) 2004
 *
 * Company: westerasoft Co., Ltd
 *
 * All right reserved.
 *
 * Created on 2004-4-8
 *
 * JDK version used		:1.4.1
 *
 * Modification history:
 *
 */
package com.hope.common.util;

import java.lang.reflect.Method;
import java.sql.Date;

import org.apache.log4j.Logger;

/**
 * @author biaoping.yin
 * 改类充分使用java.lang.reflection中提供的功能,提供以下工具:
 * 从对象中获取对应属性的值
 */
public class ValueObjectUtil {
	private static final Logger log = Logger.getLogger(TransferObjectFactory.class);

	/**
	 * Description:获取对象obj的property属性值
	 * @param obj
	 * @param property
	 * @return
	 * Object
	 */
	public static Object getValue(Object obj, String property) {
		return getValue(obj, property,null);
	}

	/**
	 * Description:获取对象obj的property属性值,params为参数数组
	 * @param obj
	 * @param property
	 * @param params 获取属性方法值的参数
	 * @return
	 * Object
	 */
	public static Object getValue(Object obj, String property,Object[] params) {
		if (obj == null || property == null || property.trim().length() == 0)
			return null;

		return getValueByMethodName(obj, getMethodName(property), params);
	}

	/**
	 * Description:根据方法名称获取,
	 * 在对象obj上调用改方法并且返回调用的返回值
	 * @param obj
	 * @param methodName 方法名称
	 * @param params 方法的参数
	 * @return
	 * Object
	 */
	public static Object getValueByMethodName(
		Object obj,
		String methodName,
		Object[] params) {
		if (obj == null
			|| methodName == null
			|| methodName.trim().length() == 0)
			return null;
		return getValueByMethodName(obj, methodName, params,null);
	}

	/**
	 * Description:根据方法名称获取,
	 * 在对象obj上调用改方法并且返回调用的返回值
	 * @param obj
	 * @param methodName 方法名称
	 * @param params 方法的参数
	 * @param paramsTtype 方法的参数类型
	 * @return
	 * Object
	 */
	public static Object getValueByMethodName(
		Object obj,
		String methodName,
		Object[] params,
		Class[] paramsTtype) {
		if (obj == null
			|| methodName == null
			|| methodName.trim().length() == 0)
			return null;
		try {
			Method method = obj.getClass().getMethod(methodName, paramsTtype);
			if (method != null)
				return method.invoke(obj, params);
		} catch (Exception e) {
			//e.printStackTrace();
			log.info(e.getMessage());
		}
		return null;

	}

	/**
	 * Description:实现在对象调用method并为该方法传入参数数组params
	 * @param obj 对象
	 * @param method 待调用的方法
	 * @param params 参数数组
	 * @return
	 * @throws Exception
	 * Object
	 */
	public static Object invoke(Object obj, Method method, Object[] params)
		throws Exception {
		return method.invoke(obj, params);
	}


	/**
	 * 获取fieldName的getter方法名称
	 * @param fieldName
	 * @return
	 */
	public static String getMethodName(String fieldName) {
		String ret = null;
		if (fieldName == null)
			return null;
		String letter = String.valueOf(fieldName.charAt(0));
		letter = letter.toUpperCase();
		ret = "get" + letter + fieldName.substring(1);
		//System.out.println("method name:" + ret);
		return ret;

	}

	/**
	 * 获取fieldName的setter方法
	 * @param fieldName
	 * @return
	 */
	public static String getSetterMethodName(String fieldName) {
		String ret = null;
		if (fieldName == null)
			return null;
		String letter = String.valueOf(fieldName.charAt(0));
		letter = letter.toUpperCase();
		ret = "set" + letter + fieldName.substring(1);
		//System.out.println("method name:" + ret);
		return ret;

	}

	/**
	 * 将obj对象从类型type转换到类型toType
	 * 支持字符串向其他基本类行转换:
	 * 支持的类型:
	 * int,char,short,double,float,long,boolean,byte
	 * java.sql.Date,java.util.Date,
	 * Integer
	 * Long
	 * Float
	 * Short
	 * Double
	 * Character
	 * Boolean
	 * Byte
	 * @param obj
	 * @param type
	 * @param toType
	 * @return
	 * @throws ClassCastException,NumberFormatException,IllegalArgumentException
	 */
	public final static Object typeCast(Object obj, Class type, Class toType)
		throws
			NoSupportTypeCastException,
			NumberFormatException,
			IllegalArgumentException {
		if(type.isArray() && !toType.isArray()
			|| !type.isArray() && toType.isArray())
//		if (type.getName().startsWith("[")
//			&& !toType.getName().startsWith("[")
//			|| !type.getName().startsWith("[")
//			&& toType.getName().startsWith("["))
			throw new IllegalArgumentException(
				new StringBuffer("类型无法转换,不支持[")
					.append(type.getName())
					.append("]向[")
					.append(toType.getName())
					.append("]转换")
					.toString());
		Object arrayObj;

		/**
		 * 基本类型转换
		 */
		if (!type.isArray()) {
			arrayObj = basicTypeCast(obj, type, toType);
		}

		/**
		 * 字符串数组向其他类型数组之间转换
		 */
		else {

			arrayObj = arrayTypeCast(obj, type, toType);
		}
		return arrayObj;
	}

	/**
	 * Description:基本的数据类型转圜
	 * @param obj
	 * @param type
	 * @param toType
	 * @return
	 * @throws NoSupportTypeCastException
	 * @throws NumberFormatException
	 * Object
	 */
	public final static Object basicTypeCast(
		Object obj,
		Class type,
		Class toType)
		throws NoSupportTypeCastException, NumberFormatException {
		if (type == toType )
			return obj;
		/**
		 * 如果obj的类型不是String型时直接抛异常,
		 * 不支持非字符串和字符串数组的类型转换
		 */
//		if (type != String.class)
//			throw new NoSupportTypeCastException(
//				new StringBuffer("不支持[")
//					.append(type)
//					.append("]向[")
//					.append(toType)
//					.append("]的转换")
//					.toString());
		/*******************************************
		 * 字符串向基本类型及其包装器转换		   *
		 * 如果obj不是相应得数据格式,将抛出		   *
		 * NumberFormatException				   *
		 ******************************************/
		if (toType == long.class || toType == Long.class)
			return new Long(obj.toString());
		if (toType == int.class || toType == Integer.class)
			return new Integer(obj.toString());
		if (toType == float.class || toType == Float.class)
			return new Float(obj.toString());
		if (toType == short.class || toType == Short.class)
			return new Short(obj.toString());
		if (toType == double.class || toType == Double.class)
			return new Double(obj.toString());
		if (toType == char.class || toType == Character.class)
			return new Character(obj.toString().charAt(0));

		if (toType == boolean.class || toType == Boolean.class)
			return new Boolean(obj.toString());
		if (toType == byte.class || toType == Byte.class)
			return new Byte(obj.toString());

		//如果是字符串则直接返回obj.toString()
		if (toType == String.class)
			return obj.toString();

		/**
		 * 字符串向java.util.Date和java.sql.Date 类型转换
		 */
		if (toType == java.util.Date.class)
		{
			if(type == java.sql.Date.class)
				return obj;
			return new java.util.Date(obj.toString());
		}

		if (toType == java.sql.Date.class)
		{

			if(type == java.util.Date.class)
				return new java.sql.Date(((java.util.Date)obj).getTime());
			java.sql.Date date = java.sql.Date.valueOf(obj.toString());//(new java.util.Date(obj.toString()).getTime());

			return date;
		}

		throw new NoSupportTypeCastException(
			new StringBuffer("不支持[")
				.append(type)
				.append("]向[")
				.append(toType)
				.append("]的转换")
				.toString());

	}
	/**
	 * 数组类型转换
	 * 支持字符串数组向一下类型数组得自动转换:
	 *	int[]
	 *	Integer[]
	 *	long[]
	 *	Long[]
	 *	short[]
	 *	Short[]
	 *	double[]
	 *	Double[]
	 *	boolean[]
	 *	Boolean[]
	 *	char[]
	 *	Character[]
	 *	float[]
	 *	Float[]
	 *	byte[]
	 *	Byte[]
	 *	java.sql.Date[]
	 *	java.util.Date[]
	 * @param obj
	 * @param type
	 * @param toType
	 * @return
	 * @throws NoSupportTypeCastException
	 * @throws NumberFormatException
	 */
	public final static Object arrayTypeCast(
		Object obj,
		Class type,
		Class toType)
		throws NoSupportTypeCastException, NumberFormatException {
		if (type == toType) {
			return obj;
		}
//		if (type != String[].class)
//			throw new NoSupportTypeCastException(
//				new StringBuffer("不支持[")
//					.append(type)
//					.append("]向[")
//					.append(toType)
//					.append("]的转换")
//					.toString());
		String[] values = (String[]) obj;
		if (toType == long[].class) {
			long[] ret = new long[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = Long.parseLong(values[i]);
			}
			return ret;
		}
		if (toType == Long[].class) {
			Long[] ret = new Long[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Long(values[i]);
			}
			return ret;
		}

		if (toType == int[].class) {
			int[] ret = new int[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = Integer.parseInt(values[i]);
			}
			return ret;
		}
		if (toType == Integer[].class) {
			Integer[] ret = new Integer[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Integer(values[i]);
			}
			return ret;
		}
		if (toType == float[].class) {
			float[] ret = new float[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = Float.parseFloat(values[i]);
			}
			return ret;
		}
		if (toType == Float[].class) {
			Float[] ret = new Float[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Float(values[i]);
			}
			return ret;
		}
		if (toType == short[].class) {
			short[] ret = new short[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = Short.parseShort(values[i]);
			}
			return ret;
		}
		if (toType == Short[].class) {
			Short[] ret = new Short[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Short(values[i]);
			}
			return ret;
		}
		if (toType == double[].class) {
			double[] ret = new double[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = Double.parseDouble(values[i]);
			}
			return ret;
		}
		if (toType == Double[].class) {
			Double[] ret = new Double[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Double(values[i]);
			}
			return ret;
		}

		if (toType == char[].class) {
			char[] ret = new char[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = values[i].charAt(0);
			}
			return ret;
		}
		if (toType == Character[].class) {
			Character[] ret = new Character[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Character(values[i].charAt(0));
			}
			return ret;
		}

		if (toType == boolean[].class) {
			boolean[] ret = new boolean[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Boolean(values[i]).booleanValue();
			}
			return ret;
		}
		if (toType == Boolean[].class) {
			Boolean[] ret = new Boolean[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Boolean(values[i]);
			}
			return ret;
		}
		if (toType == byte[].class) {
			byte[] ret = new byte[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Byte(values[i]).byteValue();
			}
			return ret;
		}
		if (toType == Byte[].class) {
			Byte[] ret = new Byte[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new Byte(values[i]);
			}
			return ret;
		}

		//如果是字符串则直接返回obj.toString()
		if (toType == String[].class) {
			return values;
			//			short[] ret = new short[values.length];
			//			for(int i = 0; i < values.length; i ++)
			//			{
			//				ret[i] = Short.parseShort(values[i]);
			//			}
			//			return ret;
		}
		/**
		 * 字符串向java.util.Date和java.sql.Date 类型转换
		 */
		if (toType == java.util.Date.class) {
			java.util.Date[] ret = new java.util.Date[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = new java.util.Date(values[i]);
			}
			return ret;
		}
		if (toType == java.sql.Date.class) {
			java.sql.Date[] ret = new java.sql.Date[values.length];
			for (int i = 0; i < values.length; i++) {
				ret[i] = java.sql.Date.valueOf(values[i]);//values[i]).getTime());
			}
			return ret;
		}

		throw new NoSupportTypeCastException(
			new StringBuffer("不支持[")
				.append(type)
				.append("]向[")
				.append(toType)
				.append("]的转换")
				.toString());

	}
}

⌨️ 快捷键说明

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