Is it possible to automate comments for classes, methods, constructors, etc.. in Eclipse?

4


Say if I create a new class in Eclipse called "MyClass", the code will be generated as follows:

public class MyClass
{

}


By personal convention, I always end the last bracket with a comment such as this:

public class MyClass
{

} // end of class MyClass



  • So, would it be possible to insert such a comment for every class, method, constructor, etc..?
  • And would it be possible to "seed" the comment with the relevant name?

...such as:  // end of class (class name here) 

Ian Campbell

Posted 2012-09-12T07:04:16.237

Reputation: 239

Answers

2

Yes, almost, if you disregard a carriage return.

Go to Window -> Preferences -> Java -> Code Style - Code Templates

This is on Eclipse Indigo - it might be different in other versions.

Now in the "Configure generated code..." panel on the right, open up the "Code" branch and select and edit the "New Java files" entry.

In the Edit dialog, in the Pattern text box, you see the following:

${filecomment}
${package_declaration}

${typecomment}
${type_declaration}

which you need to modify to this:

${filecomment}
${package_declaration}

${typecomment}
${type_declaration}//end of ${type_name}

and then save and close. Try creating a new class and you'll see it comes out like this:

package com.nomadsoft.cortex.application;

public class AdamsClass {

}
//end of AdamsClass

Unfortunately you can't get rid of the carriage return that puts the comment you want on the next line down. It is hard-coded within Eclipse.

Depending on how much effort you want to go to, you have a choice of how to get the comment on the same line as the end-bracket:

(Option 1) you could edit the template files in the JDT jar:

eclipse\plugins\org.eclipse.jdt.ui_*.jar

open up templates/default-codetemplates.xml and find the "newtype" template in the xml, and replace the "${type_declaration}" with

public class ${type_name} \{
\} // end of ${type_name}

(Option 2) write a complete plugin to do this. I'd love to have the time to do that. Maybe you do. See enter link description here

Adam

Posted 2012-09-12T07:04:16.237

Reputation: 178

Thanks @Adam! These are excellent solutions, very interesting... I am using Eclipse Indigo, so I will be able to experiment with this. I feel somewhat scared to mess with the JDT jar, but I will definitely look at it -- the edit you are suggesting does seem minimal though. And I don't have time to write a plugin, but that would be an awesome project! However, Mechanical snail here is suggesting a plugin that seems to do this stuff as well. – Ian Campbell – 2012-09-12T19:58:12.083

Any idea though on how to generate a similar comment for the end of the main method? (as in "// end of main method ") – Ian Campbell – 2012-09-12T19:58:30.630

1I thought there might be a solution to it, because if you look at Preferences -> Java -> Templates, there is a "main", but the two frameworks aren't linked - the code generation framework doesn't use the hotkey template framework. So if you create your main method as part of the class file generation, then no, you can't do it. But you can edit the hotkey template and use "main ctrl-space" to insert it after you've created the class. – Adam – 2012-09-14T09:21:15.377

3

In my opinion this is a bad practice, because the comments can get out of sync.

However, you can get the same visual effect using the Bracketeer plugin (see also this blog post). The plugin automatically displays closing comments, as you desire:

Bracketeer example

without saving them to the source file. Thus the "phantom" comments always reflect the current state of the code, giving you the best of both worlds.

The comment style is configurable.

Mechanical snail

Posted 2012-09-12T07:04:16.237

Reputation: 6 625

Good answer though I agree it is a bad idea. Your code should make it clear what is being ended where through indentation or something similar without resorting to this. – soandos – 2012-09-12T07:49:02.273

Thanks @Mechanicalsnail! My question may be somewhat misleading in that I don't actually comment the end brackets of everything, but in general only of the class and the main method. I agree for sure, that code should be clear and organized so that it is self-explanatory. However, I will sometimes comment outer nestings if there is a lot of inner nesting taking place. But yes, it is nice to have a plugin that gives you the option. ;) – Ian Campbell – 2012-09-12T19:50:44.403