3

I noticed the eclipse indenter has support for the latest version of java, and it would be nice if I could use that class to indent generated java source code. Is there a way of integrating it ?

EDIT: I need to be able to include the code formatter in my code. No external calls.

EDIT2: I've managed to get it working. You can read the story here. Thanks VonC !

Geo
  • 93,257
  • 117
  • 344
  • 520
  • Awesome! I have updated my answer with the main principle of your post for future SO readers, but thank you again for your feedback on this issue. – VonC Jun 10 '09 at 18:35

3 Answers3

5

You can try running the formatter as a standalone application (also detailed here).

eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>

Try first to define formatting settings with eclipse IDE in order to achieve the right result, then export those settings, and use that configuration file in the eclipse.exe parameters.
Or see also "Generating a Config File for the Formatter Application"

eclipse [...] -config <myExportedSettings> 

In a java program, you can try to directly format by:

  • Creating an instance of CodeFormatter
  • Using the method void format(aString) on this instance to format aString. It will return the formatted string.

Thanks to Geo himself and his report in his blog entry, I now know you need to use DefaultCodeFormatter

    String code = "public class geo{public static void main(String[] args){System.out.println(\"geo\");}}";
    CodeFormatter cf = new DefaultCodeFormatter();

    TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null);
    IDocument dc = new Document(code);
    try {
        te.apply(dc);
        System.out.println(dc.get());
    } catch (MalformedTreeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Again, full details in the blog entry. Thank you Geo for that feedback!


Thorbjørn Ravn Andersen mentions in the comments:

Maven2 Java Formatter Plugin v0.4 describes a maven plugin that allows Maven to invoke the Eclipse formatter.
As of 0.4 it invokes Eclipse 3.5 which does not support Java 8.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • "as a standalone class", but in a java program! Right, I understand now ;) Looking into it right now.. – VonC Jun 10 '09 at 14:59
  • english is not my primary language. I find that some ideas are complicated for me to express :). Sorry. – Geo Jun 10 '09 at 15:03
  • Well... count me in too (in the "english is not my primary language") ;) I did find the right class, but I am still unsure how to use that CodeFormatter for *java* format. Let me know if it helps. – VonC Jun 10 '09 at 15:12
  • I'll give it a try and post the results. Thanks! – Geo Jun 10 '09 at 15:13
  • I've managed to get it to work. I'm writing a blog post about it. I'll post the link here when I'm done. – Geo Jun 10 '09 at 17:31
  • Excellent! I look forward reading about it. Thank you for the feedback :) – VonC Jun 10 '09 at 17:46
  • I've added the link to the question's body. Thanks! – Geo Jun 10 '09 at 18:15
  • Note: http://maven-java-formatter-plugin.googlecode.com/svn/site/0.4/usage.html describes a maven plugin that allows Maven to invoke the Eclipse formatter. As of 0.4 it invokes Eclipse 3.5 which does not support Java 8. – Thorbjørn Ravn Andersen Jun 25 '14 at 12:37
  • @ThorbjørnRavnAndersen Ok, I have included your comment in the answer for more visibility. – VonC Jun 25 '14 at 12:44
1

Actually, there is one problem with VonC's answer: DefaultCodeFormatter is in an 'internal' package, and therefore should not be be used by clients!

I recently asked the same question here on stackoverflow, and came up with the answer a little while later.

In short, you need to use ToolFactory, as in

ToolFactory.createCodeFormatter(null);
Community
  • 1
  • 1
Nels Beckman
  • 20,508
  • 3
  • 24
  • 28
1

I was using the CodeFormatter in an Eclipse-independent project. The default options used by calling ToolFactory.createCodeFormatter(null); could not handle the source code - the result of the format() call being null.

A minimal working options setup is the following:

Hashtable<String, String> options = new Hashtable<>();
options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
options.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
Cwt
  • 8,206
  • 3
  • 32
  • 27