设为首页 加入收藏

TOP

利用Java注解将常量类生成js文件供前端调用(二)
2015-12-15 23:09:20 来源: 作者: 【 】 浏览:15
Tags:利用 Java 注解 常量 生成 文件 前端 调用
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
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python函数参数*args 和**kwargs.. 下一篇用Swift在应用中在指定时间添加本..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: