I am using an API, which requires lot of getters and setters and then perform the operation. Is there any template based generation of Java code, which reads APIs setters/getters and generate code. Also some code is common for all objects. So I want to keep the common code in the template, and generated getter or setter code based on API. Is there any process/tool to do that?
我正在使用一个API,它需要大量的getter和setter然后执行操作。是否有基于模板的Java代码生成,它读取API setter / getters并生成代码。还有一些代码对所有对象都是通用的。所以我想在模板中保留公共代码,并基于API生成getter或setter代码。有没有任何流程/工具可以做到这一点?
3 个解决方案
#1
0
I'd look into the documentation for the IDE are you using to program your code. A good one will have tools built in or available as add-ons for this. For example, if you are using Eclipse, you can make code templates for new classes, snippets where you plug in a few variables to generate a block of code, or generate getters and setters based on your existing class variables.
我会查看用于编写代码的IDE文档。一个好的工具将内置工具或作为附件提供。例如,如果您使用的是Eclipse,则可以为新类创建代码模板,在其中插入一些变量以生成代码块,或者根据现有的类变量生成getter和setter。
#2
0
using a Java IDE. In Eclipse you just select your Field and do a Right Click > Source > Generate Getters and Setters.
使用Java IDE。在Eclipse中,您只需选择Field并执行右键单击> Source> Generate Getters and Setters。
Same for constructor, toString method, etc.
构造函数,toString方法等相同。
What about creating a Template Project? You add the skeleton of your Class with some dummy Fields and Getters/Setters and you just replace the names and delete the extra ones? You will not type, just rename (maybe using refactoring) and delete.
那么创建一个模板项目呢?你添加了一些虚拟的字段和Getters / Setter你的类的骨架,你只需要替换名称并删除额外的名称?您不会键入,只需重命名(可能使用重构)和删除。
Here is a post on how to Create Your Own Project Template for NetBeans... there must be also for other IDEs. http://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project
这是一篇关于如何为NetBeans创建自己的项目模板的帖子......还必须有其他IDE。 http://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project
#3
0
Use the lombok library: www.projectlombok.org. You put an annotation @Data
on the class and it writes the getters and setters for you, as well as equals, hashcode, tostring. There are other nice features in the library as well.
使用lombok库:www.projectlombok.org。你在类上放了一个注释@Data,它为你写了getters和setter,以及equals,hashcode,tostring。图书馆还有其他不错的功能。
EDIT: To expand on this a bit, the getters and setters do not show up in your code anywhere. Lombok generates the getters and setters as bytecode during the compilation process. There is plugins for most IDEs that will make the generated code available in drop-downs/code-completion as soon as you add the annotation to your class.
编辑:为了扩展这一点,getter和setter不会出现在你的代码中。 Lombok在编译过程中将getter和setter生成为字节码。大多数IDE都有插件,只要您将注释添加到类中,就会使下拉/代码完成中的生成代码可用。
#1
0
I'd look into the documentation for the IDE are you using to program your code. A good one will have tools built in or available as add-ons for this. For example, if you are using Eclipse, you can make code templates for new classes, snippets where you plug in a few variables to generate a block of code, or generate getters and setters based on your existing class variables.
我会查看用于编写代码的IDE文档。一个好的工具将内置工具或作为附件提供。例如,如果您使用的是Eclipse,则可以为新类创建代码模板,在其中插入一些变量以生成代码块,或者根据现有的类变量生成getter和setter。
#2
0
using a Java IDE. In Eclipse you just select your Field and do a Right Click > Source > Generate Getters and Setters.
使用Java IDE。在Eclipse中,您只需选择Field并执行右键单击> Source> Generate Getters and Setters。
Same for constructor, toString method, etc.
构造函数,toString方法等相同。
What about creating a Template Project? You add the skeleton of your Class with some dummy Fields and Getters/Setters and you just replace the names and delete the extra ones? You will not type, just rename (maybe using refactoring) and delete.
那么创建一个模板项目呢?你添加了一些虚拟的字段和Getters / Setter你的类的骨架,你只需要替换名称并删除额外的名称?您不会键入,只需重命名(可能使用重构)和删除。
Here is a post on how to Create Your Own Project Template for NetBeans... there must be also for other IDEs. http://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project
这是一篇关于如何为NetBeans创建自己的项目模板的帖子......还必须有其他IDE。 http://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project
#3
0
Use the lombok library: www.projectlombok.org. You put an annotation @Data
on the class and it writes the getters and setters for you, as well as equals, hashcode, tostring. There are other nice features in the library as well.
使用lombok库:www.projectlombok.org。你在类上放了一个注释@Data,它为你写了getters和setter,以及equals,hashcode,tostring。图书馆还有其他不错的功能。
EDIT: To expand on this a bit, the getters and setters do not show up in your code anywhere. Lombok generates the getters and setters as bytecode during the compilation process. There is plugins for most IDEs that will make the generated code available in drop-downs/code-completion as soon as you add the annotation to your class.
编辑:为了扩展这一点,getter和setter不会出现在你的代码中。 Lombok在编译过程中将getter和setter生成为字节码。大多数IDE都有插件,只要您将注释添加到类中,就会使下拉/代码完成中的生成代码可用。