[英]In Eclipse, how do I change the default modifiers in the class/type template?
Eclipse's default template for new types (Window > Preferences > Code Style > Code Templates > New Java Files) looks like this: Eclipse的新类型的默认模板(Window> Preferences> Code Style> Code Templates> New Java Files)如下所示:
${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
: 现在,我热切地认为访问应该尽可能地限制,除非明确允许,否则应该禁止继承,所以我想更改
${type_declaration}
以将所有类声明为final
而不是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.
我发现谷歌搜索的唯一问题是关于Eclipse邮件列表的2004年问题,该问题未得到答复。
So, the question in short: How can I change the default class/type modifiers in Eclipse? 那么,简而言之: 如何在Eclipse中更改默认的类/类型修饰符?
I'm using Eclipse Galileo (3.5) if that matters. 如果重要的话,我正在使用Eclipse Galileo(3.5)。
Looks like it is not possible. 看起来不可能。 The
${type_declaration}
is internal stuff. ${type_declaration}
是内部的东西。
What you can do is to click everytime the final checkbox in the "New Java Class"-Dialog. 你可以做的是每次点击“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 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. 如果现在按Control + Shift + O组织导入,则旧类型声明将消失。 You could also add organize imports to save action to automate.
您还可以添加组织导入以保存操作以自动执行。
I know it's bad, but it does what you want. 我知道这很糟糕,但它可以满足您的需求。
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. 注释掉$ {type_declaration}因为它是必需的。 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.
对不起,如果这是一个2岁的线程,但对我来说它仍然相关。
Maybe this will help you? 也许这对你有帮助吗?
eclipse\plugins\org.eclipse.jdt.ui_*.jar\templates\
Eclipse custom variable for java code templates 用于java代码模板的Eclipse自定义变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.