--- /dev/null
+package jp.igapyon.jcfa.util;
+
+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.MalformedTreeException;
+import org.eclipse.text.edits.TextEdit;
+
+public class JcfaEclipseUtil {
+ /**
+ * Format source using eclipse.
+ *
+ * @param source
+ * Input source.
+ * @return Formatted source.
+ */
+ public static String formatSource(final String source) {
+ final CodeFormatter cf = ToolFactory.createCodeFormatter(null);
+ final TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, source, 0,
+ source.length(), 0, null);
+ IDocument doc = new Document(source);
+ try {
+ te.apply(doc);
+ return doc.get();
+ } catch (MalformedTreeException e) {
+ e.printStackTrace();
+ throw new IllegalArgumentException(e);
+ } catch (BadLocationException e) {
+ e.printStackTrace();
+ throw new IllegalArgumentException(e);
+ }
+ }
+}