From 1fa96a66c8ecb7dbaaba00ddd0ff22980e88b4b1 Mon Sep 17 00:00:00 2001 From: fuhouyin Date: Wed, 8 Feb 2023 17:24:00 +0800 Subject: [PATCH] :tada: new mod --- src/main/java/utils/HandlePage.java | 21 ++++++ src/main/java/utils/MethodGetAndSet.java | 94 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/utils/HandlePage.java create mode 100644 src/main/java/utils/MethodGetAndSet.java diff --git a/src/main/java/utils/HandlePage.java b/src/main/java/utils/HandlePage.java new file mode 100644 index 0000000..7d2a010 --- /dev/null +++ b/src/main/java/utils/HandlePage.java @@ -0,0 +1,21 @@ +package utils; + +import java.util.List; + +/** + * @author fuhouyin + * @time 2023/2/1 15:07 + */ +public class HandlePage { + + /**分页 start = (page - 1) * size, end = page * size*/ + private List handlePage(List source, int start, int end){ + int total = source.size(); +// end = end > total ? total : end; + end = Math.min(end, total); + if (end <= start) { + return null; + } + return source.subList(start,end); + } +} diff --git a/src/main/java/utils/MethodGetAndSet.java b/src/main/java/utils/MethodGetAndSet.java new file mode 100644 index 0000000..1dee900 --- /dev/null +++ b/src/main/java/utils/MethodGetAndSet.java @@ -0,0 +1,94 @@ +package utils; + +import java.lang.reflect.Method; +import java.math.BigDecimal; + +/** + * @author fuhouyin + * @time 2023/2/1 15:10 + */ +public class MethodGetAndSet { + + /**根据属性,获取get方法*/ + private Object getGetMethod(Object ob , String name)throws Exception{ + Method[] m = ob.getClass().getMethods(); + for(int i = 0;i < m.length;i++){ + if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){ + return m[i].invoke(ob); + } + } + return null; + } + + /**根据属性,拿到set方法,并把值set到对象中*/ + private void setValue(Object obj,Class clazz,String filedName,Class typeClass,Object value){ + filedName = removeLine(filedName); + String methodName = "set" + filedName.substring(0,1).toUpperCase()+filedName.substring(1); + try{ + Method method = clazz.getDeclaredMethod(methodName, new Class[]{typeClass}); + method.invoke(obj, new Object[]{getClassTypeValue(typeClass, value)}); + }catch(Exception ex){ + ex.printStackTrace(); + } + } + + /**通过class类型获取获取对应类型的值*/ + private Object getClassTypeValue(Class typeClass, Object value){ + if(typeClass == int.class || value instanceof Integer){ + if(null == value){ + return 0; + } + return value; + }else if(typeClass == short.class){ + if(null == value){ + return 0; + } + return value; + }else if(typeClass == byte.class){ + if(null == value){ + return 0; + } + return value; + }else if(typeClass == double.class){ + if(null == value){ + return 0; + } + return value; + }else if(typeClass == long.class){ + if(null == value){ + return 0; + } + return value; + }else if(typeClass == String.class){ + if(null == value){ + return ""; + } + return value; + }else if(typeClass == boolean.class){ + if(null == value){ + return true; + } + return value; + }else if(typeClass == BigDecimal.class){ + if(null == value){ + return new BigDecimal(0); + } + return new BigDecimal(value+""); + }else { + return typeClass.cast(value); + } + } + + /**处理字符串 如: abc_dex ---> abcDex*/ + private String removeLine(String str){ + if(null != str && str.contains("_")){ + int i = str.indexOf("_"); + char ch = str.charAt(i+1); + char newCh = (ch+"").substring(0, 1).toUpperCase().toCharArray()[0]; + String newStr = str.replace(str.charAt(i+1), newCh); + String newStr2 = newStr.replace("_", ""); + return newStr2; + } + return str; + } +}