/**
* by hxy on 2019/2/26.
* 自定义方法、参数描述的注解
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface DescAnnotation {
String desc();
}
/**
* by hxy on 2019/2/26.
* 管理员方法参数类
*/
public class AdminTaskParam {
private String code; //变量名
// private String name;
private String type; //类型
private String desc; //描述
}
/**
* 获取管理员功能列表
*/
private List<AdminTask> getAllList(Class clazz) {
RequestMapping annotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
String classUrl = annotation.value()[0];
Method[] methods = clazz.getDeclaredMethods();
List<AdminTask> taskList = new ArrayList<>();
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
AdminTask methodTask = null;
for (Method m : methods) {
//有@RequestMapping和@ResponseBody的方法
if (m.isAnnotationPresent(RequestMapping.class) && m.isAnnotationPresent(ResponseBody.class)) {
methodTask = new AdminTask();
//获取方法描述
if (m.isAnnotationPresent(DescAnnotation.class)) {
String desc = m.getAnnotation(DescAnnotation.class).desc();
methodTask.setDesc(StringUtil.isBlank(desc) ? m.getName() : desc);
} else {
methodTask.setDesc(m.getName());
}
String methodUrl = m.getAnnotation(RequestMapping.class).value()[0];
methodTask.setUrl((classUrl.startsWith("/") ? "" : "/") + classUrl + (methodUrl.startsWith("/") ? "" : "/") + methodUrl);
methodTask.setCode(m.getName());
int pCount = m.getParameterCount();
if (pCount > 0) {
List<AdminTaskParam> paramList = new ArrayList<>(pCount);
Class<?>[] paramTypes = m.getParameterTypes();
String[] params = u.getParameterNames(m);
//获取参数描述
List<String> pDescs = new ArrayList<>(pCount);
Annotation[][] paramAnnos = m.getParameterAnnotations();
for (Annotation[] annotations : paramAnnos) {
//如果方法有添加@DescAnnotation
if (CollectionUtils.arrayToList(annotations).contains(DescAnnotation.class)) {
for (Annotation anno : annotations) {
if (anno instanceof DescAnnotation) {
DescAnnotation da = (DescAnnotation) anno;
pDescs.add(da.desc());
}
}
} else {
pDescs.add("");
}
}
//参数列表
for (int i = 0; i < pDescs.size(); i++) {
AdminTaskParam param = new AdminTaskParam();
param.setCode(params[i]);
param.setType(paramTypes[i].getName());
param.setDesc(StringUtil.isBlank(pDescs.get(i)) ? params[i] : pDescs.get(i));
paramList.add(param);
}
methodTask.setParams(paramList);
}
taskList.add(methodTask);
}
}
return taskList;
}
/**
* @param cls 要添加到管理任务的Controller的@RequestMapping映射
* @param method 方法名
* @return
*/
@RequestMapping("/adminList")
public ModelAndView getAdminTaskList(@RequestParam(required = false) String cls, @RequestParam(required = false) String method) {
List<AdminTask> methodList = getAllList(getClassByCode(cls));
ModelAndView mav = new ModelAndView();
mav.setViewName("admin/adminList");
mav.addObject("taskList", methodList);
mav.addObject("cls", cls);
mav.addObject("adminConsoleUrl", this.getConsoleUrl());
if (StringUtil.isNotBlank(method)) {
for (AdminTask task : methodList) {
if (method.equals(task.getCode())) {
mav.addObject("methodInfo", task);
break;
}
}
}
return mav;
}