Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How can I programmatically format Java code?
How can I programmatically format Java code? [message #258992] Sat, 07 March 2009 06:02 Go to next message
Eclipse UserFriend
Hi,

I have a program that generates Java source files from FreeMarker template
files. The generated files do not have optimal or consistent code
formatting in order to make the FreeMarker template files easier to read.
I would like the program to
format (ie pretty print) these generated files to improve their layout.
This
would need to be automated. At the moment the program has no dependencies
on
Eclipse libraries but I can add whatever dependencies I need.

I notice there is a class called CodeFormatter in the Eclipse API. How do
I
create or obtain an instance of this class in order to format the
generated
java code? Also what dependencies would I need in order to use it? I don't
really want to pull in SWT dependencies etc as my program does not have a
GUI.

Many thanks in advance for any help that you can offer.


Nicholas Allen
Re: How can I programmatically format Java code? [message #258996 is a reply to message #258992] Sat, 07 March 2009 06:26 Go to previous messageGo to next message
Eclipse UserFriend
Hi Nick,

Here is what I use in a QuickFixProcessor of mine that creates methods.
The catch is that it formats the code according to Eclipse's default
settings, not the user's current settings. Any improvement is welcome!

private static String format( String code, int initialIndent ) {
// take default Eclipse formatting options
Map<String, String> options =
DefaultCodeFormatterConstants.getEclipseDefaultSettings();

// initialize the compiler settings to be able to format 1.5 code
options.put( JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5 );
options.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
JavaCore.VERSION_1_5 );
options.put( JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5 );

// instantiate the default code formatter with the given options
final CodeFormatter codeFormatter =
ToolFactory.createCodeFormatter( options );

// retrieve the source to format
final TextEdit edit = codeFormatter.format( CodeFormatter.K_UNKNOWN,
code, 0, code.length(), initialIndent,
System.getProperty( "line.separator" ) ); //$NON-NLS-1$
if( edit == null ) {
new IllegalArgumentException( "cannot format this: "
+ code ).printStackTrace(); //$NON-NLS-1$
return code;
} else {
// apply the format edit
IDocument document = new Document( code );
try {
edit.apply( document );
} catch( MalformedTreeException e ) {
e.printStackTrace();
} catch( BadLocationException e ) {
e.printStackTrace();
}
return document.get();
}
}

HTH,
J.-P.

Nicholas Allen wrote:
> Hi,
>
> I have a program that generates Java source files from FreeMarker
> template files. The generated files do not have optimal or consistent
> code formatting in order to make the FreeMarker template files easier to
> read. I would like the program to format (ie pretty print) these
> generated files to improve their layout. This would need to be
> automated. At the moment the program has no dependencies on Eclipse
> libraries but I can add whatever dependencies I need.
>
> I notice there is a class called CodeFormatter in the Eclipse API. How
> do I create or obtain an instance of this class in order to format the
> generated java code? Also what dependencies would I need in order to use
> it? I don't really want to pull in SWT dependencies etc as my program
> does not have a GUI.
>
> Many thanks in advance for any help that you can offer.
>
>
> Nicholas Allen
>
Re: How can I programmatically format Java code? [message #259000 is a reply to message #258996] Sat, 07 March 2009 06:46 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for that! That looks like exactly what I need. If I find any way
to improve it I'll let you know...

Cheers,

Nick

J.-P. Pellet wrote:
> Hi Nick,
>
> Here is what I use in a QuickFixProcessor of mine that creates methods.
> The catch is that it formats the code according to Eclipse's default
> settings, not the user's current settings. Any improvement is welcome!
>
> private static String format( String code, int initialIndent ) {
> // take default Eclipse formatting options
> Map<String, String> options =
> DefaultCodeFormatterConstants.getEclipseDefaultSettings();
>
> // initialize the compiler settings to be able to format 1.5 code
> options.put( JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5 );
> options.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
> JavaCore.VERSION_1_5 );
> options.put( JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5 );
>
> // instantiate the default code formatter with the given options
> final CodeFormatter codeFormatter =
> ToolFactory.createCodeFormatter( options );
>
> // retrieve the source to format
> final TextEdit edit = codeFormatter.format( CodeFormatter.K_UNKNOWN,
> code, 0, code.length(), initialIndent,
> System.getProperty( "line.separator" ) ); //$NON-NLS-1$
> if( edit == null ) {
> new IllegalArgumentException( "cannot format this: "
> + code ).printStackTrace(); //$NON-NLS-1$
> return code;
> } else {
> // apply the format edit
> IDocument document = new Document( code );
> try {
> edit.apply( document );
> } catch( MalformedTreeException e ) {
> e.printStackTrace();
> } catch( BadLocationException e ) {
> e.printStackTrace();
> }
> return document.get();
> }
> }
>
> HTH,
> J.-P.
>
> Nicholas Allen wrote:
>> Hi,
>>
>> I have a program that generates Java source files from FreeMarker
>> template files. The generated files do not have optimal or consistent
>> code formatting in order to make the FreeMarker template files easier
>> to read. I would like the program to format (ie pretty print) these
>> generated files to improve their layout. This would need to be
>> automated. At the moment the program has no dependencies on Eclipse
>> libraries but I can add whatever dependencies I need.
>>
>> I notice there is a class called CodeFormatter in the Eclipse API. How
>> do I create or obtain an instance of this class in order to format the
>> generated java code? Also what dependencies would I need in order to
>> use it? I don't really want to pull in SWT dependencies etc as my
>> program does not have a GUI.
>>
>> Many thanks in advance for any help that you can offer.
>>
>>
>> Nicholas Allen
>>
Re: How can I programmatically format Java code? [message #259020 is a reply to message #259000] Mon, 09 March 2009 13:19 Go to previous message
Eclipse UserFriend
> private static String format( String code, int initialIndent ) {
> // take default Eclipse formatting options
> Map<String, String> options =
> DefaultCodeFormatterConstants.getEclipseDefaultSettings();
>
> // initialize the compiler settings to be able to format 1.5 code
> options.put( JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5 );
> options.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
> JavaCore.VERSION_1_5 );
> options.put( JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5 );
>
> // instantiate the default code formatter with the given options
> final CodeFormatter codeFormatter =
> ToolFactory.createCodeFormatter( options );


Actually, I've just found that instead of this, you can write

// instantiate the default code formatter with the given options
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(
javaProject.getOptions( true ) );

in order to respect the user's code formatting settings. This supposes
that javaProject is an IJavaProject reference to the project containing
the file being edited.

Cheers,
J.-P.
Previous Topic:Adding binary folders as classpath containers
Next Topic:Need a selection dialog box for selecting classes, methods, fields
Goto Forum:
  


Current Time: Wed Apr 23 02:07:33 EDT 2025

Powered by FUDForum. Page generated in 0.04033 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top