JAVA反射获取T泛型子类构造方法

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

package com.pyh.dao.core;

import java.lang.reflect.*;
import java.util.*;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class TmReflectionUtils {

	public TmReflectionUtils() {
	}

	public static Object getFieldValue(Object object, String fieldName) {
		Field field = getDeclaredField(object, fieldName);
		if (field == null)
			throw new IllegalArgumentException((new StringBuilder(
					"Could not find field [")).append(fieldName)
					.append("] on target [").append(object).append("]")
					.toString());
		makeAccessible(field);
		Object result = null;
		try {
			result = field.get(object);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常{}", e.getMessage());
		}
		return result;
	}

	public static void setFieldValue(Object object, String fieldName,
			Object value) {
		Field field = getDeclaredField(object, fieldName);
		if (field == null)
			throw new IllegalArgumentException((new StringBuilder(
					"Could not find field [")).append(fieldName)
					.append("] on target [").append(object).append("]")
					.toString());
		makeAccessible(field);
		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常:{}", e.getMessage());
		}
	}

	public static Object invokeMethod(Object object, String methodName,
			Class parameterTypes[], Object parameters[]) {
		Method method = getDeclaredMethod(object, methodName, parameterTypes);
		if (method == null)
			throw new IllegalArgumentException((new StringBuilder(
					"Could not find method [")).append(methodName)
					.append("] on target [").append(object).append("]")
					.toString());
		method.setAccessible(true);
		try {
			return method.invoke(object, parameters);
		} catch (Exception e) {
			throw convertReflectionExceptionToUnchecked(e);
		}
	}

	protected static Field getDeclaredField(Object object, String fieldName) {
		Assert.notNull(object, "object不能为空");
		Assert.hasText(fieldName, "fieldName");
		for (Class superClass = object.getClass(); superClass != java / lang
				/ Object;)
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException nosuchfieldexception) {
				superClass = superClass.getSuperclass();
			}

		return null;
	}

	protected static void makeAccessible(Field field) {
		if (!Modifier.isPublic(field.getModifiers())
				|| !Modifier.isPublic(field.getDeclaringClass().getModifiers()))
			field.setAccessible(true);
	}

	protected static Method getDeclaredMethod(Object object, String methodName,
			Class parameterTypes[]) {
		Assert.notNull(object, "object不能为空");
		for (Class superClass = object.getClass(); superClass != java / lang
				/ Object;)
			try {
				return superClass.getDeclaredMethod(methodName, parameterTypes);
			} catch (NoSuchMethodException nosuchmethodexception) {
				superClass = superClass.getSuperclass();
			}

		return null;
	}

	public static Class getSuperClassGenricType(Class clazz) {
		return getSuperClassGenricType(clazz, 0);
	}

	public static Class getSuperClassGenricType(Class clazz, int index) {
		Type genType = clazz.getGenericSuperclass();
		if (!(genType instanceof ParameterizedType)) {
			logger.warn((new StringBuilder(
					String.valueOf(clazz.getSimpleName()))).append(
					"'s superclass not ParameterizedType").toString());
			return java / lang / Object;
		}
		Type params[] = ((ParameterizedType) genType).getActualTypeArguments();
		if (index >= params.length || index < 0) {
			logger.warn((new StringBuilder("Index: ")).append(index)
					.append(", Size of ").append(clazz.getSimpleName())
					.append("'s Parameterized Type: ").append(params.length)
					.toString());
			return java / lang / Object;
		}
		if (!(params[index] instanceof Class)) {
			logger.warn((new StringBuilder(
					String.valueOf(clazz.getSimpleName())))
					.append(" not set the actual class on superclass generic parameter")
					.toString());
			return java / lang / Object;
		} else {
			return (Class) params[index];
		}
	}

	public static List convertElementPropertyToList(Collection collection,
			String propertyName) {
		List list = new ArrayList();
		try {
			Object obj;
			for (Iterator iterator = collection.iterator(); iterator.hasNext(); list
					.add(PropertyUtils.getProperty(obj, propertyName)))
				obj = iterator.next();

		} catch (Exception e) {
			throw convertReflectionExceptionToUnchecked(e);
		}
		return list;
	}

	public static String convertElementPropertyToString(Collection collection,
			String propertyName, String separator) {
		List list = convertElementPropertyToList(collection, propertyName);
		return StringUtils.join(list, separator);
	}

	public static Object convertValue(Object value, Class toType) {
		try {
			DateConverter dc = new DateConverter();
			dc.setUseLocaleFormat(true);
			dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
			ConvertUtils.register(dc, java / util / Date);
			return ConvertUtils.convert(value, toType);
		} catch (Exception e) {
			throw convertReflectionExceptionToUnchecked(e);
		}
	}

	public static RuntimeException convertReflectionExceptionToUnchecked(
			Exception e) {
		if ((e instanceof IllegalAccessException)
				|| (e instanceof IllegalArgumentException)
				|| (e instanceof NoSuchMethodException))
			return new IllegalArgumentException("Reflection Exception.", e);
		if (e instanceof InvocationTargetException)
			return new RuntimeException("Reflection Exception.",
					((InvocationTargetException) e).getTargetException());
		else
			return new RuntimeException("Unexpected Exception.", e);
	}

	public static Object convertStringToObject(String value, Class toType) {
		try {
			return ConvertUtils.convert(value, toType);
		} catch (Exception e) {
			throw convertReflectionExceptionToUnchecked(e);
		}
	}

	private static Logger logger = LoggerFactory.getLogger(com / pyh / dao
			/ core / TmReflectionUtils);

}


/*
	DECOMPILATION REPORT

	Decompiled from: E:\JavaIDE\WorkSpace\xywpyh\WebRoot\WEB-INF\lib\spring-jsonutil1.0.jar
	Total time: 40 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/


转载于:https://my.oschina.net/zhaomin/blog/666507

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值