1 package jp.igapyon.jcfa.util;
5 import org.eclipse.jdt.core.JavaCore;
6 import org.eclipse.jdt.core.ToolFactory;
7 import org.eclipse.jdt.core.formatter.CodeFormatter;
8 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
9 import org.eclipse.jface.text.BadLocationException;
10 import org.eclipse.jface.text.Document;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.text.edits.MalformedTreeException;
13 import org.eclipse.text.edits.TextEdit;
15 public class JcfaEclipseUtil {
17 * Format source using eclipse.
21 * @return Formatted source.
23 public static String formatSource(final String source) {
25 System.out.println("TRACE: " + source);
27 final Map options = DefaultCodeFormatterConstants
28 .getEclipseDefaultSettings();
30 options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
31 options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
32 JavaCore.VERSION_1_7);
33 options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
36 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
37 DefaultCodeFormatterConstants.createAlignmentValue(true,
38 DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
39 DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
41 final CodeFormatter cf = ToolFactory.createCodeFormatter(options);
42 cf.createIndentationString(4);
43 final TextEdit te = cf.format(CodeFormatter.K_COMPILATION_UNIT
44 | CodeFormatter.F_INCLUDE_COMMENTS, source, 0, source.length(),
46 IDocument doc = new Document(source);
51 System.out.println("TRACE: " + doc.get());
54 } catch (MalformedTreeException e) {
56 throw new IllegalArgumentException(e);
57 } catch (BadLocationException e) {
59 throw new IllegalArgumentException(e);