Examination_system/Examination_system-1/.svn/pristine/86/86f685fa142cdf56d506e0b78e0...

174 lines
4.8 KiB
Plaintext
Raw Normal View History

2023-10-30 13:10:40 +08:00
package com.bonus.core;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bonus.core.annotation.BackstageField;
import com.bonus.core.annotation.UploadField;
import com.bonus.core.annotation.UploadTable;
public class ReflectUtil {
/**
* @Author 无畏
* @Date 2019-07-16
* @function 获取导入类的数据库表字段
* @param o 需要传来全类名
* @return
*/
public static String[] getFieldColumn(String className){
List<String> titles = new ArrayList<String>();
String[] array = new String[titles.size()];
try {
Class<?> forName = Class.forName(className);
Field[] fields = forName.getDeclaredFields();
for (Field f : fields) {
// 判断字段注解是否存在
boolean isHas = f.isAnnotationPresent(UploadField.class);
if (isHas) {
UploadField name = f.getAnnotation(UploadField.class);
String column = name.column();
f.getType().getName();
if("".equals(column)) {
column = f.getName();
}
titles.add(column);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return titles.toArray(array);
}
/**
* @Author 无畏
* @Date 2019-07-16
* @function 获取导入类的数据库表字段
* @param o 需要传来全类名
* @return
*/
public static String[] getTypeColumn(String className){
List<String> list = new ArrayList<String>();
String[] type = new String[list.size()];
try {
Class<?> forName = Class.forName(className);
Field[] fields = forName.getDeclaredFields();
for (Field f : fields) {
// 判断字段注解是否存在
boolean isHas = f.isAnnotationPresent(UploadField.class);
if (isHas) {
String name = f.getType().getName();
name = name.substring(name.lastIndexOf(".")+1);
list.add(name);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list.toArray(type);
}
/**
* @Author 无畏
* @Date 2019-07-16
* @function 获取模板的title
* @param o 需要传来全类名
* @return
*/
public static String[] getTitleValue(String className){
List<String> titles = new ArrayList<String>();
String[] array = new String[titles.size()];
try {
Class<?> forName = Class.forName(className);
Field[] fields = forName.getDeclaredFields();
for (Field f : fields) {
// 判断字段注解是否存在
boolean isHas = f.isAnnotationPresent(UploadField.class);
if (isHas) {
UploadField name = f.getAnnotation(UploadField.class);
String value = name.value();
titles.add(value);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return titles.toArray(array);
}
/**
* @Author 无畏
* @Date 2019-07-16
* @function 获取模板的title
* @param o 需要传来全类名
* @return
*/
public static List<Map<String,String>> getBackstageValue(String className){
List<Map<String,String>> backstageValue = new ArrayList<Map<String,String>>();
Map<String,String> map;
try {
Class<?> forName = Class.forName(className);
Field[] fields = forName.getDeclaredFields();
for (Field f : fields) {
map = new HashMap<String,String>();
// 判断字段注解是否存在
boolean isHas = f.isAnnotationPresent(BackstageField.class);
if (isHas) {
BackstageField anno = f.getAnnotation(BackstageField.class);
String fieldName = f.getName();
String column = anno.column();
String jdbcType = anno.jdbcType();
String typeName = f.getType().getName();
typeName = typeName.substring(typeName.lastIndexOf(".")+1);
if("".equals(jdbcType)) {
map.put("jdbcType",typeName);
}else {
map.put("jdbcType",jdbcType);
}
if("".equals(column)) {
map.put("column",fieldName);
}else {
map.put("column",column);
}
String value = anno.value();
map.put("value",value);
map.put("field",fieldName);
backstageValue.add(map);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return backstageValue;
}
/**
* @Author 无畏
* @Date 2019-07-16
* @function 获取数据库表名
* @param o 需要传来全类名
* @return
*/
public static String[] getTypeAnnotation(String className){
String[] param = new String[2];
try {
Class<?> forName = Class.forName(className);
UploadTable annotation = forName.getAnnotation(UploadTable.class);
param[0] = annotation.value();
param[1] = annotation.fileName();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return param;
}
}