parent
a394852b42
commit
a4b8fc39a9
@ -0,0 +1,33 @@
|
||||
package ${basePackage}.frame.excel;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class WCell implements Serializable {
|
||||
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* 默认无参构造方法.会将单元格的状态设为通过.
|
||||
*/
|
||||
public WCell() {
|
||||
this.value = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入默认单元格值的构造方法.
|
||||
*
|
||||
* @param value 单元格的值.
|
||||
*/
|
||||
public WCell(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ${basePackage}.frame.excel;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DataTable中的行对象.
|
||||
* Created on 2014/09/19.
|
||||
*
|
||||
* @author
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class WRow extends HashMap<String, WCell> implements Serializable {
|
||||
|
||||
private List<String> errorList = new ArrayList<>();
|
||||
|
||||
public final boolean hasError() {
|
||||
return errorList.size() > 0;
|
||||
}
|
||||
|
||||
public final void addError(String errorMsg) {
|
||||
errorList.add(errorMsg);
|
||||
}
|
||||
|
||||
public List<String> getErrorList() {
|
||||
return errorList;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Created on 2014/9/24.
|
||||
*
|
||||
* @author
|
||||
* @version v1.0.0.0
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ColumnDescription {
|
||||
String value() default "";
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 为字段、方法或类注解名称。<br/>
|
||||
* Created on 2014/9/24.
|
||||
*
|
||||
* @author
|
||||
* @version v1.0.0.0
|
||||
*/
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ColumnName {
|
||||
String value();
|
||||
|
||||
/**
|
||||
* 排序
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int width() default 8;
|
||||
|
||||
/**
|
||||
* 日期格式化
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String dateFormat() default "yyyy-MM-dd";
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 优先通过target来获取转换器
|
||||
*
|
||||
* @author 王兵
|
||||
* @version v0.0.1
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Converter {
|
||||
Class target();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 是否忽略表字段
|
||||
*
|
||||
* @author 金洋
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Ignore {
|
||||
boolean value() default true;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 是否关注父类的字段
|
||||
*
|
||||
* @author 金洋
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ParentFirst {
|
||||
boolean value() default true;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package ${basePackage}.frame.excel.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 为字段、方法或类注解名称。<br/>
|
||||
* Created on 2014/9/24.
|
||||
*
|
||||
* @author
|
||||
* @version v1.0.0.0
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface SheetName {
|
||||
String value();
|
||||
|
||||
boolean freezeFirst() default false;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class BooleanConverter implements Converter<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean convert(String var) {
|
||||
try {
|
||||
return Boolean.parseBoolean(var);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Boolean var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class ByteConverter implements Converter<Byte> {
|
||||
|
||||
@Override
|
||||
public Byte convert(String var) {
|
||||
try {
|
||||
return Byte.parseByte(var);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Byte var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class CharacterConverter implements Converter<Character> {
|
||||
|
||||
@Override
|
||||
public Character convert(String var) {
|
||||
if (var == null || var.equals("")) {
|
||||
return '0';
|
||||
} else {
|
||||
return var.charAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Character var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var).trim();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public interface Converter<T> {
|
||||
|
||||
T convert(String var);
|
||||
|
||||
String string(T var);
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateConverter implements Converter<Date> {
|
||||
|
||||
@Override
|
||||
public Date convert(String var) {
|
||||
if (var == null) {
|
||||
return null;
|
||||
}
|
||||
Date date = null;
|
||||
try {
|
||||
var = var.trim();
|
||||
if (var.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return sdf.parse(var);
|
||||
} else if (var.matches("[0-9]{4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return sdf.parse(var);
|
||||
} else if (var.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
date = sdf.parse(var);
|
||||
} else if (var.matches("[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日 [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
|
||||
date = sdf.parse(var);
|
||||
}
|
||||
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Date var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class DoubleConverter implements Converter<Double> {
|
||||
|
||||
@Override
|
||||
public Double convert(String var) {
|
||||
try {
|
||||
return Double.parseDouble(var);
|
||||
} catch (Exception e) {
|
||||
return 0d;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Double var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class FloatConverter implements Converter<Float> {
|
||||
|
||||
@Override
|
||||
public Float convert(String var) {
|
||||
try {
|
||||
//增加对1.0一类的的优化显示
|
||||
if (var.matches("(\\d+)\\.0")) {
|
||||
var = var.replaceAll("\\.0$", "");
|
||||
}
|
||||
|
||||
return Float.parseFloat(var);
|
||||
} catch (Exception e) {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Float var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class IntegerConverter implements Converter<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer convert(String var) {
|
||||
try {
|
||||
return Integer.parseInt(var);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Integer var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class LongConverter implements Converter<Long> {
|
||||
|
||||
@Override
|
||||
public Long convert(String var) {
|
||||
try {
|
||||
//增加对1.0一类的的兼容性
|
||||
if (var.matches("(\\d+)\\.(\\d*)")) {
|
||||
Pattern compile = Pattern.compile("(\\d+)\\.(\\d*)");
|
||||
Matcher matcher = compile.matcher(var);
|
||||
if (matcher.find()) {
|
||||
var = matcher.group(1);
|
||||
}
|
||||
}
|
||||
|
||||
return Long.parseLong(var);
|
||||
} catch (Exception e) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Long var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class ShortConverter implements Converter<Short> {
|
||||
|
||||
@Override
|
||||
public Short convert(String var) {
|
||||
try {
|
||||
return Short.parseShort(var);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(Short var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package ${basePackage}.frame.excel.converter;
|
||||
|
||||
public class StringConverter implements Converter<String> {
|
||||
|
||||
@Override
|
||||
public String convert(String var) {
|
||||
return var;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String string(String var) {
|
||||
if (var == null) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(var);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* Created on 2015/1/29.
|
||||
*
|
||||
* @author
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class BaseCellStyle {
|
||||
/**
|
||||
* 样式
|
||||
*/
|
||||
protected CellStyle style;
|
||||
|
||||
public BaseCellStyle(Workbook workbook) {
|
||||
style = workbook.createCellStyle();
|
||||
style.setFillPattern(CellStyle.NO_FILL);
|
||||
//下边框
|
||||
style.setBorderBottom(CellStyle.SOLID_FOREGROUND);
|
||||
//下边框颜色
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||
//左边框
|
||||
style.setBorderLeft(CellStyle.SOLID_FOREGROUND);
|
||||
//左边框颜色
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||
//右边框
|
||||
style.setBorderRight(CellStyle.SOLID_FOREGROUND);
|
||||
//右边框颜色
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||
//上边框
|
||||
style.setBorderTop(CellStyle.SOLID_FOREGROUND);
|
||||
//上边框颜色
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
||||
|
||||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中
|
||||
style.setBorderBottom(CellStyle.SOLID_FOREGROUND); //下边框
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色
|
||||
style.setBorderLeft(CellStyle.SOLID_FOREGROUND); //左边框
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); //左边框颜色
|
||||
style.setBorderRight(CellStyle.SOLID_FOREGROUND); //右边框
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); //右边框颜色
|
||||
style.setBorderTop(CellStyle.SOLID_FOREGROUND); //上边框
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色
|
||||
}
|
||||
|
||||
public CellStyle getStyle() {
|
||||
return style;
|
||||
}
|
||||
|
||||
public void setStyle(CellStyle style) {
|
||||
this.style = style;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* Created on 2015/1/29.
|
||||
*
|
||||
* @author
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class BaseFont {
|
||||
/**
|
||||
* 字体
|
||||
*/
|
||||
protected Font font;
|
||||
|
||||
public BaseFont(Workbook workbook) {
|
||||
font = workbook.createFont();
|
||||
}
|
||||
|
||||
public Font getFont() {
|
||||
return font;
|
||||
}
|
||||
|
||||
public void setFont(Font font) {
|
||||
this.font = font;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
public class DataCellStyle extends BaseCellStyle {
|
||||
|
||||
public DataCellStyle(Workbook workbook, short align, boolean error) {
|
||||
super(workbook);
|
||||
style.setAlignment(align);
|
||||
if (error) {
|
||||
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
|
||||
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
|
||||
}
|
||||
}
|
||||
|
||||
public DataCellStyle(Workbook workbook, short align) {
|
||||
this(workbook, align, false);
|
||||
}
|
||||
|
||||
public DataCellStyle(Workbook workbook, boolean error) {
|
||||
this(workbook, CellStyle.ALIGN_LEFT, error);
|
||||
}
|
||||
|
||||
public DataCellStyle(Workbook workbook) {
|
||||
this(workbook, CellStyle.ALIGN_LEFT, false);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
public class ErrorCellStyle extends BaseCellStyle {
|
||||
|
||||
public ErrorCellStyle(Workbook workbook) {
|
||||
super(workbook);
|
||||
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
|
||||
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
|
||||
}
|
||||
|
||||
public CellStyle getStyle() {
|
||||
return style;
|
||||
}
|
||||
|
||||
public void setStyle(CellStyle style) {
|
||||
this.style = style;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
public class HeadCellStyle extends BaseCellStyle {
|
||||
public HeadCellStyle(Workbook workbook) {
|
||||
super(workbook);
|
||||
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); //背景颜色-灰色
|
||||
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
|
||||
style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
|
||||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* 普通字体.颜色黑
|
||||
* Created on 2015/1/29.
|
||||
*
|
||||
* @author
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class NormalFont extends BaseFont {
|
||||
public NormalFont(Workbook workbook) {
|
||||
super(workbook);
|
||||
font.setColor(HSSFColor.BLACK.index); //字体颜色-黑色
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package ${basePackage}.frame.excel.style;
|
||||
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
/**
|
||||
* 普通字体.颜色黑
|
||||
* Created on 2015/1/29.
|
||||
*
|
||||
* @author
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class RedFont extends BaseFont {
|
||||
public RedFont(Workbook workbook) {
|
||||
super(workbook);
|
||||
font.setColor(HSSFColor.RED.index); //字体颜色-黑色
|
||||
}
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
package xyz.wbsite.frame.utils;
|
||||
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created on 2015/5/28.
|
||||
*
|
||||
* @author 金洋
|
||||
* @version 2.1.0
|
||||
*/
|
||||
public class ClassUtil {
|
||||
|
||||
/**
|
||||
* 获取模板类所有字段
|
||||
*
|
||||
* @param clazz 模板对象
|
||||
* @param parentFirst 是否关注父类的字段
|
||||
* @return
|
||||
*/
|
||||
public static Field[] getFields(Class clazz, boolean parentFirst) {
|
||||
List<Field> returnList = new ArrayList<>();
|
||||
Set<String> nameSet = new HashSet<>();
|
||||
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
|
||||
for (Field field : fields) {
|
||||
returnList.add(field);
|
||||
nameSet.add(field.getName());
|
||||
}
|
||||
List<Field> parentList = getParentFields(clazz.getSuperclass(), nameSet, parentFirst);
|
||||
if (parentFirst) {
|
||||
parentList.addAll(returnList);
|
||||
returnList = parentList;
|
||||
} else {
|
||||
returnList.addAll(parentList);
|
||||
}
|
||||
|
||||
fields = new Field[returnList.size()];
|
||||
|
||||
int index = 0;
|
||||
for (Field field : returnList) {
|
||||
fields[index++] = field;
|
||||
}
|
||||
return fields;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取class的 包括父类的
|
||||
*
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static Field[] getClassFields(Class<?> clazz) {
|
||||
List<Field> list = new ArrayList<Field>();
|
||||
Field[] fields;
|
||||
do {
|
||||
fields = clazz.getDeclaredFields();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
list.add(fields[i]);
|
||||
}
|
||||
clazz = clazz.getSuperclass();
|
||||
} while (clazz != Object.class && clazz != null);
|
||||
return list.toArray(fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是不是集合的实现类
|
||||
*
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static boolean isCollection(Class<?> clazz) {
|
||||
return Collection.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取GET方法
|
||||
*
|
||||
* @param name
|
||||
* @param pojoClass
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Method getMethod(String name, Class<?> pojoClass) throws Exception {
|
||||
StringBuffer getMethodName = new StringBuffer("get");
|
||||
getMethodName.append(name.substring(0, 1).toUpperCase());
|
||||
getMethodName.append(name.substring(1));
|
||||
Method method = null;
|
||||
try {
|
||||
method = pojoClass.getMethod(getMethodName.toString());
|
||||
} catch (Exception e) {
|
||||
method = pojoClass.getMethod(
|
||||
getMethodName.toString().replace("get", "is")
|
||||
);
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SET方法
|
||||
*
|
||||
* @param name
|
||||
* @param pojoClass
|
||||
* @param type
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Method getMethod(String name, Class<?> pojoClass, Class<?> type) throws Exception {
|
||||
StringBuffer getMethodName = new StringBuffer("set");
|
||||
getMethodName.append(name.substring(0, 1).toUpperCase());
|
||||
getMethodName.append(name.substring(1));
|
||||
return pojoClass.getMethod(getMethodName.toString(), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是不是java基础类
|
||||
*
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
public static boolean isJavaClass(Field field) {
|
||||
Class<?> fieldType = field.getType();
|
||||
boolean isBaseClass = false;
|
||||
if (fieldType.isArray()) {
|
||||
isBaseClass = false;
|
||||
} else if (fieldType.isPrimitive() || fieldType.getPackage() == null
|
||||
|| fieldType.getPackage().getName().equals("java.lang")
|
||||
|| fieldType.getPackage().getName().equals("java.math")
|
||||
|| fieldType.getPackage().getName().equals("java.sql")
|
||||
|| fieldType.getPackage().getName().equals("java.util")) {
|
||||
isBaseClass = true;
|
||||
}
|
||||
return isBaseClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过指定的类型获取所有的成员变量
|
||||
*
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static Field[] getFields(Class clazz) {
|
||||
return getFields(clazz, false);
|
||||
}
|
||||
|
||||
private static List<Field> getParentFields(Class parentClazz, Set<String> nameSet, boolean parentFirst) {
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
|
||||
if (parentClazz != null) {
|
||||
Field[] parentList = parentClazz.getDeclaredFields();
|
||||
int index = 0;
|
||||
for (Field field : parentList) {
|
||||
int beginSize = nameSet.size();
|
||||
nameSet.add(field.getName());
|
||||
int endSize = nameSet.size();
|
||||
|
||||
if (endSize > beginSize) {
|
||||
if (parentFirst) {
|
||||
fieldList.add(index++, field);
|
||||
} else {
|
||||
fieldList.add(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
fieldList.addAll(getParentFields(parentClazz.getSuperclass(), nameSet, parentFirst));
|
||||
}
|
||||
return fieldList;
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package xyz.wbsite.frame.utils;
|
||||
|
||||
public class StringUtil {
|
||||
|
||||
public static int getByteLength(String str) {
|
||||
int length = str.replaceAll("[^\\x00-\\xff]", "**").length();
|
||||
return length;
|
||||
}
|
||||
|
||||
public static String upperFirstWord(String str) {
|
||||
String temp = str.substring(0, 1);
|
||||
return temp.toUpperCase() + str.substring(1);
|
||||
}
|
||||
|
||||
public static boolean isEmpty(String value) {
|
||||
int strLen;
|
||||
if (value == null || (strLen = value.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if ((Character.isWhitespace(value.charAt(i)) == false)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNotEmpty(String value) {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查对象是否为数字型字符串,包含负数开头的。
|
||||
*/
|
||||
public static boolean isNumeric(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
char[] chars = obj.toString().toCharArray();
|
||||
int length = chars.length;
|
||||
if (length < 1)
|
||||
return false;
|
||||
|
||||
int i = 0;
|
||||
if (length > 1 && chars[0] == '-')
|
||||
i = 1;
|
||||
|
||||
for (; i < length; i++) {
|
||||
if (!Character.isDigit(chars[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把通用字符编码的字符串转化为汉字编码。
|
||||
*/
|
||||
public static String unicodeToChinese(String unicode) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
if (!isEmpty(unicode)) {
|
||||
for (int i = 0; i < unicode.length(); i++) {
|
||||
out.append(unicode.charAt(i));
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String toUnderlineStyle(String name) {
|
||||
StringBuilder newName = new StringBuilder();
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
char c = name.charAt(i);
|
||||
if (Character.isUpperCase(c)) {
|
||||
if (i > 0) {
|
||||
newName.append("_");
|
||||
}
|
||||
newName.append(Character.toLowerCase(c));
|
||||
} else {
|
||||
newName.append(c);
|
||||
}
|
||||
}
|
||||
return newName.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue