根据编程规范,修改了部分代码

master
星期八 6 years ago
parent 1dc37eb4b0
commit dc45b37985
  1. 220
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java
  2. 143
      panda-beans/src/main/java/org/panda/beans/util/ClassUtil.java
  3. 54
      panda-web/src/main/java/org/panda/web/helper/BeanHelper.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<Field> getAllField(Class clazz){
Field[] fields = getField(clazz);
List<Field> 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<Field> fieldList = getAllField(sourceClass);
Map<String,Field> fieldMap = new HashMap<>(fieldList.size());
for (Field field: fieldList){
field.setAccessible(true);
fieldMap.put(field.getName(),field);
}
Class targerClass = targer.getClass();
List<Field> 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<String,String> getFiledType(Field[] fields){
Map<String,String> map=new HashMap(fields.length);
for (Field field:fields){
map.put(field.getName(),field.getGenericType().toString());
}
return map;
}
/**
* 获取对象属性详情
* @param fields
* @return
*/
public static List<Map<String,String>> getFieldInfo(Field[] fields){
List<Map<String,String>> list=new ArrayList<>();
for (Field field:fields){
Map<String,String> 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<String> getFiledName(Field[] fields){
List<String> filedNameList=new ArrayList<>();
for (Field field:fields){
filedNameList.add(field.getName());
}
return filedNameList;
}
/**
* 返回属性类型列表
* @param fields
* @return
*/
public static List<String> getFileTypeList(Field[] fields){
List<String> 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<String,Object> getFieldValue(Object obj){
Class clazz = obj.getClass();
List<Field> fieldList = BeanUtil.getAllField(clazz);
Map<String,Object> 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;
}
}

@ -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<Class> getInterClass(Class clazz){
Class[] classes = clazz.getInterfaces();
List<Class> classList = Arrays.asList(classes);
return classList;
}
/**
* 获取指定包名下的所有类
* @param packageName
* @return
*/
public static Set<Class> getClassSet(String packageName){
//class 集合
Set<Class> classSet = new HashSet<>();
try {
Enumeration<URL> 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<JarEntry> 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<Class> 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<Class> classSet,String className){
Class<?> clazz = loadClass(className,false);
classSet.add(clazz);
}
}

@ -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<Class, Object> BEAN_MAP = new HashMap<>();
static {
Set<Class> beanClassSet = ClassHelper.getBeanClassSet();
beanClassSet.forEach(clazz -> {
Object obj = ReflectionUtil.newInstance(clazz);
BEAN_MAP.put(clazz, obj);
});
}
/**
* 获取 Bean 映射
*
* @return
*/
public static Map<Class, Object> getBeanMap() {
return BEAN_MAP;
}
/**
* 获取 Bean 实例
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> 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);
}
}
Loading…
Cancel
Save