/**
* 类型,状态,各个枚举类型的javascript对象
*/
@RequestMapping(value = "/common/enums")
public void enumJS() throws JsonProcessingException {
HttpServletResponse response = getResponse();
Map<String, Map<String, Object>> result = new HashMap<>();
for (Class<? extends NameValueEnum> anEnum : ClassUtil.getAllClassByInterface(NameValueEnum.class)) {
NameValueEnum[] enumConstants = anEnum.getEnumConstants();
List list = new ArrayList<>();
for (NameValueEnum enumConstant : enumConstants) {
ImmutableMap map = ImmutableMap.builder().put("value", enumConstant.getValue()).put("name", enumConstant.getName()).build();
list.add(map);
result.put(enumConstant.getName(), map);
}
}
try {
response.setContentType("application/javascript");
response.setCharacterEncoding("UTF-8");
response.getWriter().println("ENUMS = " + JsonUtils.objectToJson(result));
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
上面用到的工具类(ClassUtil)的代码是从网上找的:
public class ClassUtil {
/**
* @param c 接口
* @return List<Class> 实现接口的所有类
* @Description: 根据一个接口返回该接口的所有类
* @author LiYaoHua
* @date 2012-4-5 上午11:22:24
*/
@SuppressWarnings("unchecked")
public static List<Class> getAllClassByInterface(Class c) {
List returnClassList = new ArrayList<Class>();
//判断是不是接口,不是接口不作处理
if (c.isInterface()) {
String packageName = c.getPackage().getName(); //获得当前包名
try {
List<Class> allClass = getClasses(packageName);//获得当前包以及子包下的所有类
//判断是否是一个接口
for (int i = 0; i < allClass.size(); i++) {
if (c.isAssignableFrom(allClass.get(i))) {
if (!c.equals(allClass.get(i))) {
returnClassList.add(allClass.get(i));
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
return returnClassList;
}
/**
* @param packageName 包名
* @return List<Class> 包下所有类
* @Description: 根据包名获得该包以及子包下的所有类不查找jar包中的
* @author LiYaoHua
* @date 2012-4-5 上午11:26:48
*/
private static List<Class> getClasses(String packageName) throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = packageName.replace(".", "/");
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class> classes = new ArrayList<Class>();
for (File directory : dirs) {
classes.addAll(findClass(directory, packageName));
}
return classes;
}
private static List<Class> findClass(File directory, String packageName)
throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClass(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + "." + file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
}
了解 工作生活心情记忆 的更多信息
Subscribe to get the latest posts sent to your email.