Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package cn.tom.kit.clazz;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import cn.tom.mvc.core.CocookException;
/**
* 反射工具类
*
* @author Service Platform Architecture Team ([email protected])
*/
public abstract class ReflectUtil {
/* 首字母大写 */
public static String toTitleCase(String name) {
return Character.toTitleCase(name.charAt(0)) + name.substring(1);
}
/* 获取setXxx的方法名称 */
public static String toSetMethod(String name) {
return "set" + toTitleCase(name);
}
/* 获取getXxx的方法名称 */
public static String toGetMethod(String name) {
return "get" + toTitleCase(name);
}
/**
* method.setAccessible(true); 执行方法 method.invoke(obj, params...) 返回方法的执行结果,
* void 返回null, obj为实例[静态方法可为NULL], params 为参数,无参可为0[NULL]
*/
public static Object invokeMethodByConvert(final Object object, final Method method, Object... params) {
convert(method, params);
try{
return method.invoke(object, params);
}catch(Exception e){
throw new CocookException(e);
}
}
public static Object invokeMethod(final Object object, final Method method, Object... params){
try{
return method.invoke(object, params);
}catch(Exception e){
throw new CocookException(e);
}
}
/**
* 转换方法的所有参数类型
*
* @param method
* @param params
* @return
*/
public static Object[] convert(final Method method, Object... params) {
Class>[] _class = method.getParameterTypes(); // 查找 方法形参类型
if (_class.length != params.length) {
return params;
}
for (int i = 0; i < params.length; i++) {
params[i] = covert(_class[i], params[i]);
}
return params;
}
/**
* 根据method形参转换参数,支持常用参数类型
*
* @param _class
* @param param
* @return
*/
public static Object covert(Class> _class, Object param) {
if(param == null) return null;
Class> clazz = param.getClass();
if(Converter.convertType2Class(_class) == clazz || _class.isAssignableFrom(clazz)){ // 是本类 或 父类判断[isAssignableFrom]
return param;
}
if (Converter.canConvertValue(_class)) {
return Converter.coverterClass2Value(_class, null, param.toString());
}
return param.toString();
}
/**
* 持续向上查找,直到找到某个方法
*/
public static Method getDeclaredMethod(Object object, String methodName, Class>[] parameterTypes) {
for (Class> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {// NOSONAR
// Method不在当前类定义,继续向上转型
}
}
return null;
}
/**
* 根据方法名获取方法
*/
public static Method getDeclaredMethod(Class> _class, String methodName) {
Method m[] = _class.getDeclaredMethods();
Method me = null;
for (int i = 0; i < m.length; i++) {
/* 如果是公共方法,而且名称相同则返回,如果名称相同参数不同,按先后顺序只执行第一个方法 */// 是否可以判断参数个数相同的执行
if (m[i].getModifiers() == Modifier.PUBLIC && m[i].getName().equals(methodName)) {
me = m[i];
break;
}
}
return me;
}
/**
* 查找泛型类的泛型
*/
public static Class> getSuperClassGenricType(final Class> clazz, final int index) {
Class> _class = Object.class;
Type genType = clazz.getGenericSuperclass(); // 首先查找类的类型
if (!(genType instanceof ParameterizedType)) { // 如果不是泛型类,直接返回
return _class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); // 强制转换成泛型类的类型数组
if (params.length == 0) {
return _class;
}
try {
/* 例如 class User params(0)返回E,params(1)返回U */
_class = (Class>) params[index];
} catch (Exception e) {
}
return _class;
}
/**
* map 转 bean
*
* @param
* @param map
* @param _class
* @return
*/
public static T mapToBean(Map map, Class _class) {
T t;
try {
t = _class.newInstance();
} catch (Exception e) {
handleReflectionException(e);
return null;
}
for (String key : map.keySet()) {
Object value = map.get(key);
String methodName = toSetMethod(key);
Method method = getDeclaredMethod(_class, methodName);
if(method == null) continue;
invokeMethodByConvert(t, method, value);
}
return t;
}
/**
* bean 转 map
*
* @param bean
* @return
*/
public static Map beanToMap(Object bean) {
BeanInfo info = null;
Map map = new HashMap();
try {
info = Introspector.getBeanInfo(bean.getClass());
} catch (IntrospectionException e) {
handleReflectionException(e);
return null;
}
PropertyDescriptor[] pros = info.getPropertyDescriptors();
for (PropertyDescriptor pro : pros) {
String name = pro.getName();
if (!"class".equals(name)) {
Method method = pro.getReadMethod();
try {
Object result = method.invoke(bean);
if (result != null)
map.put(name, result);
} catch (Exception e) {
handleReflectionException(e);
}
}
}
return map;
}
/**
* Check whether the given class is visible in the given ClassLoader.
* @param clazz the class to check (typically an interface)
* @param classLoader the ClassLoader to check against (may be null,
* in which case this method will always return true)
*/
public static boolean isVisible(Class> clazz, ClassLoader classLoader) {
if (classLoader == null) {
return true;
}
try {
Class> actualClass = classLoader.loadClass(clazz.getName());
return (clazz == actualClass);
// Else: different interface class found...
}
catch (ClassNotFoundException ex) {
// No interface class found...
return false;
}
}
/**
* Return all interfaces that the given class implements as array,
* including ones implemented by superclasses.
*
If the class itself is an interface, it gets returned as sole interface.
* @param clazz the class to analyze for interfaces
* @return all interfaces that the given object implements as array
*/
public static Class>[] getAllInterfacesForClass(Class> clazz) {
return getAllInterfacesForClass(clazz, null);
}
/**
* Return all interfaces that the given class implements as array,
* including ones implemented by superclasses.
*
If the class itself is an interface, it gets returned as sole interface.
* @param clazz the class to analyze for interfaces
* @param classLoader the ClassLoader that the interfaces need to be visible in
* (may be null when accepting all declared interfaces)
* @return all interfaces that the given object implements as array
*/
public static Class>[] getAllInterfacesForClass(Class> clazz, ClassLoader classLoader) {
@SuppressWarnings("rawtypes")
Set ifcs = getAllInterfacesForClassAsSet(clazz, classLoader);
return ifcs.toArray(new Class[ifcs.size()]);
}
/**
* Return all interfaces that the given class implements as Set,
* including ones implemented by superclasses.
*
If the class itself is an interface, it gets returned as sole interface.
* @param clazz the class to analyze for interfaces
* @param classLoader the ClassLoader that the interfaces need to be visible in
* (may be null when accepting all declared interfaces)
* @return all interfaces that the given object implements as Set
*/
@SuppressWarnings("rawtypes")
public static Set getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader) {
if (clazz.isInterface() && isVisible(clazz, classLoader)) {
return java.util.Collections.singleton(clazz);
}
Set interfaces = new LinkedHashSet();
while (clazz != null) {
Class>[] ifcs = clazz.getInterfaces();
for (Class> ifc : ifcs) {
interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader));
}
clazz = clazz.getSuperclass();
}
return interfaces;
}
/**
* 将prop 注入到 obj(CoC name --> setName 注入)
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static void populate(Object obj, Properties prop) {
Iterator