英文:
Take final parameters from interface to generated method stubs in implementation
问题
我在使用类似于 C/C++ 中的 "const" 关键字的 "final" 关键字来修饰方法参数,即使在 JAVA 中它的含义与 C/C++ 中的不完全相同。我主要这样做是为了更容易区分输入参数和输出参数,就像这样:
interface Test
{
public void myMethod(final int input1, final float input2, SomeResults output);
}
然后,当我创建接口(或抽象类)的实现,并让 Eclipse 生成重载方法时,Eclipse 会省略所有的 "final" 关键字,这真的很糟糕。在 Java 编辑器设置的 "SaveActions" 部分中有一个选项,但在那里我只能强制 Eclipse 在所有可能的地方都使用 "final",这绝对不是我的意图。我如何能够强制 Eclipse 在接口方法或抽象方法中不忽略我的 "final" 关键字,并将它们放入生成的实现方法和子类的方法框架中呢?
英文:
I'm using the "final"-keyword for method-parameters like the "const"-keyword in C/C++ even if it's meaning in JAVA is not 100% the same as in C/C++. I do that mainly to easily distinguish between input- and output-parameters. Like here:
interface Test
{
public void myMethod(final int input1, final float input2, SomeResults output);
}
When I then create an implemention of the interface (or abstract class) and let eclipse generate the overloaded methods eclipse will ommit all "final"-keywords which is really bad. There is an option within the SaveActions-Part of the Java-Editor settings but there I can just enforce eclipse to use "final" everywhere where it is possible which is definitely not my intention. How can I force eclipse to NOT IGNORE my "final"-keywords in interface methods or abstract methods and put them in generated method stubs of implementions and child classes instead?
答案1
得分: 1
如何强制 Eclipse 不忽略我的接口方法或抽象方法中的
final
关键字,并将它们放入生成的实现方法和子类的方法桩中?
我认为这是不可能的……除非你愿意 修改 Eclipse。
恐怕你正在做的事情没有任何效果。
如前所述,我只在使用
final
关键字时是为了澄清参数是一个“纯输入”变量。关于原始数据类型始终按值传递的问题,我是了解的。
那不是 final
的意思。
-
所有参数都是“纯输入”……在这里它们是按值传递的。在 Java 中不存在所谓的“输出”参数。
-
相反,如果参数的类型是引用类型,那么将其声明为
final
并不会阻止方法体改变实际参数。换句话说,它不能阻止方法体使用参数来 模拟 “输出” 参数。 -
在 Java 中,在抽象方法声明中将形式参数声明为
final
是没有任何意义的。只有在方法声明后跟随方法体时,它才有意义。(然后它意味着该变量不能被赋值。)因此,如果工具(例如 Eclipse 的存根生成器)在前面的上下文中赋予final
任何意义,这将是可疑的。
我的建议是不要在接口或类中赋予 final
这种(Java 设计者未预期的)意义。如果你想表达“这不是一个‘输出’参数”,可以在 javadocs 中说明,或者发明一个自定义注释来表达。在后一种情况下,你还可以实现一个静态检查器,以确保方法体不会改变参数对象。
英文:
> How can I force eclipse to NOT IGNORE my final
keywords in interface methods or abstract methods and put them in generated method stubs of implementations and child classes instead?
I don't think it is possible ... unless you are willing to modify Eclipse.
I am afraid that what you are doing doesn't have any effect.
> As stated I use the final
keyword only to clarify that a parameter is a "pure input" variable. Regarding that primitives will always be passed by value is known to me.
That is not what final
means.
-
All parameters are "pure input" ... in the sense that they are passed by value. There is no such thing as an "out" parameter in Java.
-
Conversely, if a parameter's type is a reference type, then declaring it
final
does not stop the method body from mutating the actual parameter. In other words, it doesn't stop the method body from using the parameter to simulate an "out" parameter. -
In Java, declaring a formal parameter as
final
in an abstract method declarator doesn't have any meaning. It only has meaning if the method declarator is followed by a method body. (Then it means that the variable cannot be assigned to.) Therefore, it would be suspect if a tool (e.g. an Eclipse stub generator) were to place any meaning on thefinal
in the the former context.
My advice would be not to place this unintended (by the Java designers) meaning on final
, in either interfaces or in classes. If you want to express "this is not an "out" parameter, either do it in the javadocs, or invent a custom annotation to express it. (In the latter case, you could potentially implement a static checker to ensure that the method body does not mutate the parameter object.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论