原文连接:https://gist.github.com/pfmiles/653c8b59e795698c867djava
如题,有的时候,咱们会采用自动生成java代码的方式来完成一些任务,好比根据业务数据自动生成调用api的sdk供用户下载、使用;
这样自动生成的代码,若是未经格式化处理,基本上是不可读的;git
正好,咱们经常使用的eclipse,快捷键"ctrl + shift + F"就能自动格式化java代码;
那么,下面这段代码,就是将eclipse的这个功能模块给“抠”出来单独调用,达到格式化java代码的目的:github
package test; import java.util.Map; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.text.edits.TextEdit; /** * 调用eclipse jdt core对生成的java源码进行格式化 * * @author pf-miles 2014-4-16 下午2:48:29 */ public class JavaCodeFormattingUtil { /** * 尝试对传入的JavaSourceFile格式化,此操做若成功则将改变传入对象的内容 */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void tryFormat(JavaSourceFile src) { Map m = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); m.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6); m.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6); m.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); m.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, "160"); m.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); String code = null; IDocument doc = null; try { code = src.getCharContent(true).toString(); CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(m); TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null); if (textEdit != null) { doc = new Document(code); textEdit.apply(doc); src.setCode(doc.get()); } } catch (Exception e) { throw new RuntimeException("Error occured while formatting code: " + src.toUri(), e); } } }
其中“code = src.getCharContent(true).toString();”这一行先没必要关心"src"是个什么对象,那是咱们自定义的东西,这里的重点只须要获得“code”,而后后面的操做彻底是针对"code"这个string形式的java代码而作;api
其中还能对格式化属性作一些配置, 如:
VERSION_1_6是指对应的java版本;
而DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT则指一行最多到多长就会自动换行;
DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR是说想要使用什么字符来作缩进,通常都选择空格或tab;
这些均可以根据需求随意调整;app
固然,上述这些代码的运行,是须要依赖eclipse的格式化模块功能库的,这个库,目前没有统一的依赖地址;能够根据类名上网随便搜一个可用的maven仓库依赖便可eclipse