现在的位置: 首页 > 综合 > 正文

使用eclipse的JDT实现JAVA代码格式化功能

2013年06月29日 ⁄ 综合 ⁄ 共 3484字 ⁄ 字号 评论关闭

 使用eclipse的JDT实现JAVA代码格式化功能

 作者:hexin_2000 提交日期:2006-11-13 18:09:00  
    JAVA代码格式化工具目前有很多,如jalopy,AStyle,Polystyle,jacobe等,但都有一个问题,不支持非英文的变量名、类名。遇上一个项目,其中的类名、变量名有一部分是用日文的,如“先_MMC残高Ldt.java”.现在需要编写一个格式化程序,将JAVA代码格式化输出。经过查找,调用eclipse的核心组件可以实现该功能。为了支持JDK 1.5下的格式化,需要下载3.2.1版本的eclipse。“eclipse-SDK-3.2.1-win32.zip“
  在eclipse中新建立工程,将eclipse/plugins下的
  org.eclipse.core.commands_3.2.0.I20060605-1400.jar
  org.eclipse.core.contenttype_3.2.0.v20060603.jar
  org.eclipse.core.jobs_3.2.0.v20060603.jar
  org.eclipse.core.resources_3.2.1.R32x_v20060914.jar
  org.eclipse.core.runtime_3.2.0.v20060603.jar
  org.eclipse.equinox.common_3.2.0.v20060603.jar
  org.eclipse.equinox.preferences_3.2.1.R32x_v20060717.jar
  org.eclipse.jdt.core_3.2.1.v_677_R32x.jar
  org.eclipse.osgi_3.2.1.R32x_v20060919.jar
  org.eclipse.text_3.2.0.v20060605-1400.jar
  引入到工程中
    以下为CodeFromatter类的原码
     

  1.       package src;
  2.       import java.util.HashMap;
  3.       import java.util.Map;
  4.       import org.eclipse.jface.text.Document;
  5.       import org.eclipse.text.edits.TextEdit;
  6.       import org.eclipse.jdt.internal.formatter.*;
  7.       import org.eclipse.jdt.core.formatter.CodeFormatter;
  8.       import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
  9.       import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  10.   
  11.       public class CodeFromatter {
  12.       /**
  13.        format java source by default rule
  14.       * @param fileContent
  15.       * @exception Exception
  16.       * @return sourceCode
  17.       */
  18.       public static String format(String fileContent)throws Exception{
  19.          String sourceCode=fileContent;
  20.          //get default format for java
  21.          Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
  22.         DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);
  23.          Document doc = new Document(sourceCode);
  24.   
  25.          try {
  26.            Map compilerOptions = new HashMap();
  27.           //confirm java source base on java 1.5
  28.           compilerOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
  29.           compilerOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
  30.           compilerOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
  31.           DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences, compilerOptions);
  32.           //format
  33.           TextEdit edits= codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT ,sourceCode,0,sourceCode.length(),0,null);
  34.           edits.apply(doc);
  35.         } catch (Exception e) {
  36.           e.printStackTrace();
  37.           throw e;
  38.         }
  39.         sourceCode = doc.get();
  40.         return sourceCode;
  41.       }
  42.       public static void main(String[] arg){
  43.         String javaCodeBefore="public class 挨拶{public void 挨拶(){System.out.println(/"挨拶/");}}";
  44.         String javaCodeAfter="";
  45.         System.out.println("format before:"+"/n");
  46.         System.out.println(javaCodeBefore);
  47.         try{
  48.           javaCodeAfter= CodeFromatter.format(javaCodeBefore);
  49.           System.out.println("format after:"+"/n");
  50.           System.out.println(javaCodeAfter);
  51.         }catch(Exception e) {
  52.           e.printStackTrace();
  53.           System.out.println("format error");
  54.         }
  55.       }
  56.     }

  
  注意点:
  1. 到3.2.1版本的eclipse才支持jdk 1.5。若待格式化的代码不包括1.5的特性,比如泛型等可以使用较低版本的eclipse
  2. 被格式化的java代码必须保证无语法错误,否则不能正确格式化
  参考资料
  http://www.jsourcery.com/output/eclipse/3.1-rc-1/org/eclipse/jdt/internal/formatter/old/CodeFormatter.source.html#1519561095
  
  https://bugs.eclipse.org/bugs/attachment.cgi?id=45329

抱歉!评论已关闭.