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

时间:2021-10-01 16:16:24

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版本什么也不做,并返回零或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来执行此操作?

1 个解决方案

#1


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 - >代码样式 - >代码模板。在右侧,您将看到“评论”和“代码”。展开“代码”,您正在寻找的是“方法主体”。点击“编辑...”并在其中放置您想要的任何内容。

#1


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 - >代码样式 - >代码模板。在右侧,您将看到“评论”和“代码”。展开“代码”,您正在寻找的是“方法主体”。点击“编辑...”并在其中放置您想要的任何内容。