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 开发者_C百科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.
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.
Just check the appropriate access modifier when creating the new class with the New Class Wizard.
New Java Class Wizard
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.
Maybe this will help you?
eclipse\plugins\org.eclipse.jdt.ui_*.jar\templates\
Eclipse custom variable for java code templates
Here is my workaround:
Edit the template:
${filecomment}
${package_declaration}
${typecomment}
final class ${type_name} {
/* ${type_declaration} //delete */
/**
* @see {@link Object#toString()}
* @return String representation of this instance.
*/
public String toString() {
return "some impl";
}
}
Comment out the ${type_declaration} because it is required. You have comment to delete, but requirement is achieved. Sorry if this is a 2 year old thread, but to me it is still relevant.
精彩评论