使用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
作者: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
相关文章推荐
- 使用JAVA反射技术实现代码零耦合与功能无限扩展!
- 【Android】菜单功能的实现:使用Java代码
- java使用淘宝API读写json实现手机归属地查询功能代码
- 用代码实现在eclipse中自动生成package_这个功能很有用_java版
- Eclipse如何让Java代码和xml代码实现自动提示功能
- 使用idea Live Template实现eclipse syso自动提示代码功能
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- Java中使用fileupload组件实现文件上传功能的实例代码
- Atitit.uml2 api 的编程代码实现设计uml开发 使用eclipse jar java 版本
- 使用idea Live Template实现eclipse syso自动提示代码功能
- Java使用String类格式化当前日期实现代码
- 使用Eclipse-jee3.6版本部署web项目已实现修改java代码不用重启
- 使用Java代码在Android中实现图片裁剪功能
- Eclipse 保存文件时自动格式化代码去除未使用的引用
- eclipse使用图型svn代码对比 上传功能
- Java代码实现SMS短信发送功能
- 提高Java代码质量的Eclipse插件之Checkstyle的使用详解
- java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)
- java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并
- 使用Eclipse的代码追踪功能