views:

108

answers:

4

Eclipse's default template for new types (Window > Preferences > Code Style > Code Templates > New Java Files) looks like this:

${filecomment}
${package_declaration}

${typecomment}
${type_declaration}

Creating a new class, it'll look something like this:

package pkg;

import blah.blah;

public class FileName {
    // Class is accessible to everyone, and can be inherited 
}

Now, I'm fervent in my belief that access should be as restricted as possible, and inheritance should be forbidden unless explicitly permitted, so I'd like to change the ${type_declaration} to declare all classes as final rather than public:

package pkg;

import blah.blah;

final class FileName {
    // Class is only accessible in package, and can't be inherited
}

That seems easier said than done. The only thing I've found googling is a 2004 question on Eclipse's mailing list which was unanswered.

So, the question in short: How can I change the default class/type modifiers in Eclipse?

I'm using Eclipse Galileo (3.5) if that matters.

+1  A: 

Looks like it is not possible. The ${type_declaration} is internal stuff.

What you can do is to click everytime the final checkbox in the "New Java Class"-Dialog. But that's not something you want to.

michael.kebe
You're right. It's not something I want to do :)
gustafc
A: 

Just check the appropriate access modifier when creating the new class with the New Class Wizard.

New Java Class Wizard

jitter
That's what I do today. The thing is that it doesn't remember my choice, so I forget to do it every now and then.
gustafc
@gustafc You can let Checkstyle warn you if you forget it.
starblue
@starblue, that's a good tip. Thanks.
gustafc
A: 

Maybe this will help you?

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

Eclipse custom variable for java code templates

codedevour
Not really, it just seems to be where eclipse picks up the templates. The `${type_declaration}` isn't there. I'm beginning to suspect it's generated rather than declared.
gustafc
+1  A: 

Okay, i think there isn't any cool answer, so what about that "hack"?

${filecomment}
${package_declaration}

${typecomment}
import invalid;/* ${type_declaration} */

final class ${type_name} { }

If you now hit Control + Shift + O to organize imports, the old type declaration disappears. You could also add organize imports to save action to automate.

I know it's bad, but it does what you want.

codedevour
Well... interesting thought. Unfortunately, it then declares everything as `final class`, including interfaces, enums, etc, and it misses any superclasses/interfaces I declare in the wizard. I also have to include the `${type_declaration}` in a comment as you can't save the template without it.
gustafc