简体   繁体   English

在Eclipse中,如何更改类/类型模板中的默认修饰符?

[英]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创建新类时,只需检查相应的访问修饰符。

New Java Class Wizard 新的Java类向导

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.

相关问题 如何更改Eclipse中生成的Javadocs模板? - How do I change the Javadocs template generated in Eclipse? 如何使用修饰符控制继承? - How do I control Inheritance with modifiers? 如何更改 Intellij 自动从未知用法创建的方法修饰符? - How do I change the method modifiers that Intellij auto creates from unknown usage? 如何为eclipse编写代码模板? - How do I write a code template for eclipse? 如何将默认的Eclipse Oxygen编译器合规性级别从9更改为1.8,而不是将其恢复为9? - How do I change the default Eclipse Oxygen compiler compliance level from 9 to 1.8 and not have it revert back to 9? eclipse插件来更改方法和属性的访问修饰符 - eclipse plugin to change accessor modifiers for methods and attributes Java Eclipse-如何更改类路径? - Java Eclipse - How do I change the classpath? 如何在Eclipse中更改代码约定 - How do I change the code convention in Eclipse 如何在 eclipse 中找到 class 的用法? - How do I find usages of class in eclipse? 如何在 java 代码中使用属性/属性修饰符来更改 Minecraft 玩家的最大生命值(不是吸收效果或生命值提升) - How do I use attributes/attribute modifiers within java code to change a Minecraft players maximum health (not absorption effect or health boost)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM