JavaFormatter

本文介绍了一个用于Java代码格式化的工具类,该工具基于Eclipse JDT Core API实现,能够读取并格式化指定路径下的Java文件,并将其保存。文章提供了完整的JavaFormatter类代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在修改Hibernate Tools的源码的时候,发现的一个java格式化工具类.
package org.hibernate.tool.ide.formatting;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
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.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
import org.hibernate.tool.hbm2x.ExporterException;


public class JavaFormatter {

private CodeFormatter codeFormatter;

/**
* @param args
*/
public static void main(String[] args) {
//DefaultCodeFormatter.USE_NEW_FORMATTER = true;
HashMap hashMap = new HashMap();
hashMap.put(JavaCore.COMPILER_SOURCE, "1.5");
hashMap.put(JavaCore.COMPILER_COMPLIANCE, "1.5");
hashMap.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(hashMap);

JavaFormatter format = new JavaFormatter(null);
format.formatFile(new File("D:\\workspace\\CodeGenerator\\src\\com\\Table3.java"));

}

public JavaFormatter(Map settings) {
if(settings==null) {
// if no settings run with jdk 5 as default
settings = new HashMap();
settings.put( JavaCore.COMPILER_SOURCE, "1.5");
settings.put( JavaCore.COMPILER_COMPLIANCE, "1.5");
settings.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
}

this.codeFormatter = ToolFactory.createCodeFormatter(settings);
}

/**
* Throws exception if not possible to read or write the file.
* Returns true if formatting went ok; returns false if the formatting could not finish because of errors in the input.
*
* @param file
* @param codeFormatter
* @return
*/
public boolean formatFile(File file) throws ExporterException {
IDocument doc = new Document();
try {
String contents = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(file, null));
doc.set(contents);
TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0, contents.length(), 0, null);
if (edit != null) {
edit.apply(doc);
} else {
return false; // most likely syntax errror
}

// write the file
final BufferedWriter out = new BufferedWriter(new FileWriter(file));
try {
out.write(doc.get());
out.flush();
} finally {
try {
out.close();
} catch (IOException e) {
/* ignore */
}
}
return true;
} catch (IOException e) {
throw new ExporterException("Could not format " + file, e);
} catch (BadLocationException e) {
throw new ExporterException("Could not format " + file, e);
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值