From dc45b37985dc9b22aee7947b8b944d0d59ae4035 Mon Sep 17 00:00:00 2001 From: qi_liang Date: Sat, 26 Jan 2019 00:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E7=BC=96=E7=A8=8B=E8=A7=84?= =?UTF-8?q?=E8=8C=83=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/panda/beans/util/BeanUtil.java | 220 ++++++++++++++++++ .../java/org/panda/beans/util/ClassUtil.java | 143 ++++++++++++ .../java/org/panda/web/helper/BeanHelper.java | 54 +++++ 3 files changed, 417 insertions(+) create mode 100644 panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java create mode 100644 panda-beans/src/main/java/org/panda/beans/util/ClassUtil.java create mode 100644 panda-web/src/main/java/org/panda/web/helper/BeanHelper.java diff --git a/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java b/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java new file mode 100644 index 0000000..e400ea8 --- /dev/null +++ b/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java @@ -0,0 +1,220 @@ +package org.panda.beans.util; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +/** + * @author qi + * Created by qi_liang on 2018/1/29. + */ +public class BeanUtil { + + private final static Logger LOGGER = LoggerFactory.getLogger(BeanUtil.class); + /** + * 获取类属性,包括父类属性 + * @param clazz + */ + public static List getAllField(Class clazz){ + Field[] fields = getField(clazz); + List fieldList = new ArrayList<>(); + Field[] superFields = getSuperField(clazz); + if (superFields!=null&&superFields.length>0){ + for (Field field:superFields) { + fieldList.add(field); + } + } + if (fields!=null&&fields.length>0){ + for (Field field:fields){ + fieldList.add(field); + } + } + return fieldList; + } + + /** + * 获取类属性 + * @param clazz + * @return + */ + public static Field[] getField(Class clazz){ + + return clazz.getDeclaredFields(); + } + + /** + * 获取父类的属性 + * @param clazz + * @return + */ + public static Field[] getSuperField(Class clazz){ + + return clazz.getSuperclass().getDeclaredFields(); + } + + /** + * 复制属性,不为空的复制,为空忽略 + */ + public static void copyNotNull(Object targer,Object source){ + + Class sourceClass = source.getClass(); + List fieldList = getAllField(sourceClass); + Map fieldMap = new HashMap<>(fieldList.size()); + for (Field field: fieldList){ + field.setAccessible(true); + fieldMap.put(field.getName(),field); + } + Class targerClass = targer.getClass(); + List targerFieldList = getAllField(targerClass); + for (Field field:targerFieldList){ + field.setAccessible(true); + Field targerField = fieldMap.get(field.getName()); + //判断类型是否相等 + if (targerField!=null&&targerField.getGenericType().toString().equals(field.getGenericType().toString())){ + try { + PropertyDescriptor pd = new PropertyDescriptor(targerField.getName(), targerClass); + //获得写方法 + Method wM = pd.getWriteMethod(); + Object value = targerField.get(source); + if (value!=null) { + wM.invoke(targer, value); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IntrospectionException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + + } + + /** + * 通过反射获取属性的数据类型 + * key 属性名 + * value 类型 + * @param fields + * @return + */ + public static Map getFiledType(Field[] fields){ + Map map=new HashMap(fields.length); + for (Field field:fields){ + map.put(field.getName(),field.getGenericType().toString()); + } + return map; + } + + /** + * 获取对象属性详情 + * @param fields + * @return + */ + public static List> getFieldInfo(Field[] fields){ + List> list=new ArrayList<>(); + for (Field field:fields){ + Map map = new HashMap(2); + map.put("fieldName",field.getName()); + map.put("fieldType",field.getGenericType().toString()); + list.add(map); + } + return list; + } + + /** + * 返回属性名列表 + * @param fields + * @return + */ + public static List getFiledName(Field[] fields){ + List filedNameList=new ArrayList<>(); + for (Field field:fields){ + filedNameList.add(field.getName()); + } + return filedNameList; + } + + /** + * 返回属性类型列表 + * @param fields + * @return + */ + public static List getFileTypeList(Field[] fields){ + List fileTypeList=new ArrayList<>(); + for (Field field:fields){ + fileTypeList.add(field.getGenericType().toString()); + } + return fileTypeList; + } + + + + public static String getEntityName(Class entityClazz){ + String entityPath=entityClazz.getName(); + String [] entityNameArray=entityPath.split("\\."); + String entityName=entityNameArray[entityNameArray.length-1]; + return entityName; + } + + /** + * 判断对象是否该与类型一致 + * @param obj + * @param clazzType + * @return + */ + public static Boolean isType(Object obj,Class clazzType){ + if (obj!=null) { + Class clazz = obj.getClass(); + return clazz.getTypeName().equals(clazzType.getTypeName()) ? true : false; + } + else{ + return false; + } + } + + /** + * 获取属性的value + * @return + */ + public static Map getFieldValue(Object obj){ + Class clazz = obj.getClass(); + List fieldList = BeanUtil.getAllField(clazz); + Map fieldObjectMap = new HashMap<>(fieldList.size()); + fieldList.forEach((field)->{ + try { + //开启强制破解 + field.setAccessible(true); + Object value = field.get(obj); + if (value!=null) { + fieldObjectMap.put(field.getName(), obj); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + }); + return fieldObjectMap; + } + + + /** + * 判断对象是否为空 ,为空返回true + * @param object + * @return + */ + public boolean isNull(Object object){ + + return object==null; + } + + + +} diff --git a/panda-beans/src/main/java/org/panda/beans/util/ClassUtil.java b/panda-beans/src/main/java/org/panda/beans/util/ClassUtil.java new file mode 100644 index 0000000..4da11c4 --- /dev/null +++ b/panda-beans/src/main/java/org/panda/beans/util/ClassUtil.java @@ -0,0 +1,143 @@ +package org.panda.beans.util; + + +import org.panda.code.uitl.StringUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.net.JarURLConnection; +import java.net.URL; +import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +/** + * 类加载器 + */ +public class ClassUtil { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class); + + /** + * 类加载器 + * @return + */ + public static ClassLoader getClassLoader(){ + + return Thread.currentThread().getContextClassLoader(); + } + + /** + * 加载类 + * @param className + * @param isInitialized + * @return + */ + public static Class loadClass(String className,boolean isInitialized){ + Class clazz = null; + try { + clazz = Class.forName(className,isInitialized,getClassLoader()); + } catch (ClassNotFoundException e) { + LOGGER.error("load class failure",e); + throw new RuntimeException(e); + } + return clazz; + } + + /** + * 返回类实现接口集合 + */ + public static List getInterClass(Class clazz){ + Class[] classes = clazz.getInterfaces(); + List classList = Arrays.asList(classes); + return classList; + } + + /** + * 获取指定包名下的所有类 + * @param packageName + * @return + */ + public static Set getClassSet(String packageName){ + //class 集合 + Set classSet = new HashSet<>(); + + try { + Enumeration urls = getClassLoader().getResources(packageName.replace(".","/")); + while (urls.hasMoreElements()){ + URL url = urls.nextElement(); + if (url!=null){ + String protocol = url.getProtocol(); + if ("file".equals(protocol)){ + //替换空格 + String packagePath = url.getPath().replace("%20",""); + addClass(classSet,packagePath,packageName); + }else if ("".equals(protocol)){ + JarURLConnection jarURLConnection =(JarURLConnection) url.openConnection(); + if (jarURLConnection!=null){ + JarFile jarFile = jarURLConnection.getJarFile(); + if (jarFile!=null){ + Enumeration jarEntries = jarFile.entries(); + while (jarEntries.hasMoreElements()){ + JarEntry jarEntry = jarEntries.nextElement(); + String jarEntryName = jarEntry.getName(); + if (jarEntryName.endsWith(".class")){ + String className = jarEntryName.substring(0,jarEntryName.lastIndexOf(".")) + .replace("/","."); + doAddClass(classSet,className); + } + } + } + } + } + } + } + } catch (IOException e) { + LOGGER.error("获取实体类流异常",e); + throw new RuntimeException(e); + } + return classSet; + } + + /** + * 该处可以使用jdk8新特性优化 + * @param classSet + * @param packagePath + * @param packageName + */ + private static void addClass(Set classSet,String packagePath,String packageName){ + File[] files = new File(packagePath).listFiles((File file)->{ + return (file.isFile()&&file.getName().endsWith(".class"))||file.isDirectory(); + }); + for (File file:files){ + String fileName = file.getName(); + if (file.isFile()){ + String className = fileName.substring(0,fileName.lastIndexOf(".")); + if (StringUtil.isNotNull(packageName)){ + className = packageName+"."+className; + doAddClass(classSet,className); + } + }else { + String subPackagePath = fileName; + if (StringUtil.isNotNull(subPackagePath)){ + subPackagePath = packagePath+"/"+subPackagePath; + } + String subPackageName = fileName; + if (StringUtil.isNotNull(subPackageName)){ + subPackageName = packageName+"."+subPackageName; + } + addClass(classSet,subPackagePath,subPackageName); + } + } + + } + + public static void doAddClass(Set classSet,String className){ + Class clazz = loadClass(className,false); + classSet.add(clazz); + } + + +} diff --git a/panda-web/src/main/java/org/panda/web/helper/BeanHelper.java b/panda-web/src/main/java/org/panda/web/helper/BeanHelper.java new file mode 100644 index 0000000..f59a666 --- /dev/null +++ b/panda-web/src/main/java/org/panda/web/helper/BeanHelper.java @@ -0,0 +1,54 @@ +package org.panda.web.helper; + +import org.panda.code.uitl.ReflectionUtil; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * Bean助手类 + */ +public class BeanHelper { + + private static Map BEAN_MAP = new HashMap<>(); + + static { + Set beanClassSet = ClassHelper.getBeanClassSet(); + + beanClassSet.forEach(clazz -> { + Object obj = ReflectionUtil.newInstance(clazz); + BEAN_MAP.put(clazz, obj); + }); + } + + /** + * 获取 Bean 映射 + * + * @return + */ + public static Map getBeanMap() { + return BEAN_MAP; + } + + /** + * 获取 Bean 实例 + * + * @param clazz + * @param + * @return + */ + public static T getBean(Class clazz) { + + if (!BEAN_MAP.containsKey(clazz)) { + throw new RuntimeException("can not get bean by class" + clazz); + } + return (T) BEAN_MAP.get(clazz); + } + + /** + * 设置Bean实例 + */ + public static void setBean(Class clazz, Object obj) { + BEAN_MAP.put(clazz, obj); + } +}