您的位置:首页 > 编程语言 > Java开发

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

2008-09-19 15:14 1056 查看
使用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类的原码
  

   package src;

   import java.util.HashMap;

   import java.util.Map;

   import org.eclipse.jface.text.Document;

   import org.eclipse.text.edits.TextEdit;

   import org.eclipse.jdt.internal.formatter.*;

   import org.eclipse.jdt.core.formatter.CodeFormatter;

   import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;

   import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

  

   public class CodeFromatter {

   /**

   format java source by default rule

   * @param fileContent

   * @exception Exception

   * @return sourceCode

   */

   public static String format(String fileContent)throws Exception{

   String sourceCode=fileContent;

   //get default format for java

   Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();

   DefaultCodeFormatterOptions preferences = new DefaultCodeFormatterOptions(options);

   Document doc = new Document(sourceCode);

  

   try {

   Map compilerOptions = new HashMap();

   //confirm java source base on java 1.5

   compilerOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);

   compilerOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);

   compilerOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);

   DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences, compilerOptions);

   //format

   TextEdit edits= codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT ,sourceCode,0,sourceCode.length(),0,null);

   edits.apply(doc);

   } catch (Exception e) {

   e.printStackTrace();

   throw e;

   }

   sourceCode = doc.get();

   return sourceCode;

   }

   public static void main(String[] arg){

   String javaCodeBefore="public class 挨拶{public void 挨拶(){System.out.println(/"挨拶/");}}";

   String javaCodeAfter="";

   System.out.println("format before:"+"/n");

   System.out.println(javaCodeBefore);

   try{

   javaCodeAfter= CodeFromatter.format(javaCodeBefore);

   System.out.println("format after:"+"/n");

   System.out.println(javaCodeAfter);

   }catch(Exception e) {

   e.printStackTrace();

   System.out.println("format error");

   }

   }

   }

  
  注意点:
  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐