Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.


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) 

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
    
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 Sep 12 '12 at 19:58
    
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 Sep 12 '12 at 19:58
1  
I 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 Sep 14 '12 at 9:21
add comment

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.

share|improve this answer
    
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 Sep 12 '12 at 7:49
    
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 Sep 12 '12 at 19:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.