自动生成抛出 Eclipse 的存根方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1092490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:52:16  来源:igfitidea点击:

Auto-generate stub methods that throw in eclipse

javaeclipsecode-generation

提问by finnw

Similar to How to change "Generate Method Stub" to throw NotImplementedException in VS?, but for Eclipse instead of Visual Studio

类似于如何更改“生成方法存根”以在 VS 中抛出 NotImplementedException?, 但对于 Eclipse 而不是 Visual Studio

Both NetBeans and Eclipse have a function that, if you declare a Java class to implement an interface but omit one or more methods, will automatically generate a stub method for you.

NetBeans 和 Eclipse 都有一个功能,如果您声明一个 Java 类来实现一个接口但省略一个或多个方法,则会自动为您生成一个存根方法。

The difference is that the Eclipse version will do nothing, and return zero or null, e.g.

不同的是Eclipse版本什么都不做,返回0或null,例如

public String munge(String foo) {
    // TODO Auto-generated method stub
    return null;
}

The NetBeans version will throw an exception instead:

NetBeans 版本将改为抛出异常:

public String munge(String foo) {
    throw new UnsupportedOperationException("Not supported yet.");
}

which I prefer.

我更喜欢。

Is it possible to configure Eclipse to do this?

是否可以配置 Eclipse 来执行此操作?

回答by tddmonkey

Go to Windows -> Preferences -> Java -> Code Style -> Code Templates. On the right you'll see "Comments" and "Code". Expand "Code" and the one you're looking for is "Method Body". Click "Edit..." and put whatever you want in there.

转到 Windows -> 首选项 -> Java -> 代码样式 -> 代码模板。在右侧,您会看到“评论”和“代码”。展开“代码”,您要查找的就是“方法主体”。单击“编辑...”并将您想要的任何内容放入其中。