Root; ? ? ? ? String tempPath = fPath; ? ? ? ? PathMatchingResourcePatternResolver util = new PathMatchingResourcePatternResolver(); ? ? ? ? String fileName = ""; ? ? ? ? Map fileNameMap = new HashMap(); ? ? ? ? try { ? ? ? ? ? ? //获取所有*Constants.class文件 ? ? ? ? ? ? Resource[] file = util.getResources("classpath*:com/fx/oa/**/api/define/*Constants.class"); ? ? ? ? ? ? StringBuffer jsContent = null; ? ? ? ? ? ? for (Resource resource : file) { ? ? ? ? ? ? ? ? logger.info("扫描到文件:>>>>>" + resource.getURI()); ? ? ? ? ? ? ? ? jsContent = null; ? ? ? ? ? ? ? ? String clazz = resource.getURI().toString(); ? ? ? ? ? ? ? ? jsContent = new StringBuffer(); ? ? ? ? ? ? ? ? //获取反射需要的常量类全路径,2:!/,6:.class ? ? ? ? ? ? ? ? String className = clazz.substring(clazz.lastIndexOf("!") + 2, clazz.length() - 6).replace("/", "."); ? ? ? ? ? ? ? ? fileName = className.substring(0, className.indexOf(".api")); ? ? ? ? ? ? ? ? //1:. ? ? ? ? ? ? ? ? fileName = fileName.substring(fileName.lastIndexOf(".") + 1); ? ? ? ? ? ? ? ? if ((fileName.length() > 0) && fileNameMap.containsKey(fileName)) { ? ? ? ? ? ? ? ? ? ? jsContent = fileNameMap.get(fileName); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? logger.info("扫描到常量类:" + className); ? ? ? ? ? ? ? ? //获取顶级var变量名 ? ? ? ? ? ? ? ? String varName = "oa." ? ? ? ? ? ? ? ? ? ? ? ? + className.substring(className.lastIndexOf(".") + 1).replace("Constants", "").toLowerCase(); ? ? ? ? ? ? ? ? jsContent.append(genContent(className, fileName, varName)); ? ? ? ? ? ? ? ? if (jsContent.length() == 0) { ? ? ? ? ? ? ? ? ? ? logger.info("常量类" + className + "未定义任何内容,跳过!"); ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? fileNameMap.put(fileName, jsContent); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? for (String s : fileNameMap.keySet()) { ? ? ? ? ? ? ? ? fPath += s; ? ? ? ? ? ? ? ? File outDir = new File(fPath); ? ? ? ? ? ? ? ? //输出JS文件 ? ? ? ? ? ? ? ? if (!outDir.exists()) { ? ? ? ? ? ? ? ? ? ? if (!outDir.mkdir()) { ? ? ? ? ? ? ? ? ? ? ? ? throw new RuntimeException("创建目录:" + outDir + "失败!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? StringBuffer f = fileNameMap.get(s); ? ? ? ? ? ? ? ? File jsFile = new File(fPath + "/" + s + ".js"); ? ? ? ? ? ? ? ? logger.info("生成Js文件路径(文件系统):" + jsFile.getAbsolutePath()); ? ? ? ? ? ? ? ? logger.info("生成Js文件路径(项目相对路径):" + jsFile.getPath()); ? ? ? ? ? ? ? ? BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(jsFile)); ? ? ? ? ? ? ? ? bos.write(f.toString().getBytes()); ? ? ? ? ? ? ? ? bos.close(); ? ? ? ? ? ? ? ? fPath = tempPath; ? ? ? ? ? ? } ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? logger.error(e.getMessage()); ? ? ? ? ? ? throw new RuntimeException(e); ? ? ? ? } ? ? } ? ? ? ? ? ? @SuppressWarnings({ "rawtypes", "unchecked" }) ? ? public static StringBuffer genContent(String className, String packageName, String varName) throws Exception { ? ? ? ? Class> clazz = Class.forName(className); ? ? ? ? StringBuffer jsContent = new StringBuffer(); ? ? ? ? Field[] fields = clazz.getDeclaredFields(); ? ? ? ? Map map = new HashMap(); ? ? ? ? Map mapText = new HashMap(); ? ? ? ? List list = null; ? ? ? ? List listText = null; ? ? ? ? String fieldName = null, pre = null, end = null; ? ? ? ? if (fields.length == 0) { ? ? ? ? ? ? logger.info(className + "尚未定义任何常量!"); ? ? ? ? ? ? return new StringBuffer(); ? ? ? ? } ? ? ? ? for (Field field : fields) { ? ? ? ? ? ? fieldName = field.getName(); ? ? ? ? ? ? int fieldIndex = fieldName.indexOf("_"); ? ? ? ? ? ? if (fieldIndex == -1) { ? ? ? ? ? ? ? ? logger.error(className + "字段:" + fieldName + "命名不符合规范!"); ? ? ? ? ? ? ? ? throw new Exception(className + "字段:" + fieldName + "命名不符合规范!"); ? ? ? ? ? ? } ? ? ? ? ? ? pre = fieldName.substring(0, fieldIndex); ? ? ? ? ? ? end = firstCharToUpper(fieldName.substring(fieldName.indexOf("_") + 1).toLowerCas |