装饰公司网站源码,福建工商网上登记平台,哪个网站适合 做红本抵押,莱芜金点子2023最新招聘代码生成是减少无聊任务的不健康负担的一种常用方法#xff0c;这些任务常常使我们急切的代码苦恼。 我见过的许多代码生成框架都使用模板替换重复方法#xff0c;在该方法中#xff0c;您可以编写一个模板#xff0c;以了解生成的代码文件的外观#xff0c;然后替换某些关… 代码生成是减少无聊任务的不健康负担的一种常用方法这些任务常常使我们急切的代码苦恼。 我见过的许多代码生成框架都使用模板替换重复方法在该方法中您可以编写一个模板以了解生成的代码文件的外观然后替换某些关键字并重复其他部分以生成所需的特定文件。 这种让我烦恼的方法的问题是要编译生成的代码真的很难知道生成的代码是否可以正常工作。 您可能已经更改了一个类的名称并且突然生成的代码无法构建。 为了解决这个问题 我启动了一个名为CodeGen的项目 该项目旨在完全面向对象以便从模板到可执行代码的所有类型安全中受益。 生成器的主要用例是Speedment软件 但它可以用于各种项目。 考虑以下代码 final Generator generator new JavaGenerator();final File file File.of(org/example/Foo.java).add(Class.of(Foo).public_().add(Field.of(x, DOUBLE_PRIMITIVE).final_()).add(Field.of(y, DOUBLE_PRIMITIVE).final_()).add(Field.of(z, DOUBLE_PRIMITIVE).final_()).call(new AutoConstructor()).call(new AutoSetGetAdd()).call(new AutoEquals())).call(new AutoJavadoc()).call(new AutoImports(generator))
; 应用程序的模型树是使用bean构建的。 可以将新方法和成员变量添加到树中以创建同一类的变体。 当要呈现代码时可以轻松地将其传递给生成器类。 String code generator.on(file).get(); 生成的代码如下所示 /*** Write some documentation here.*/
package org.example;import java.util.Optional;/*** author You name here*/
public class Foo {private final double x;private final double y;private final double z;/*** Initializes the Foo component.** param x the x* param y the y* param z the z*/public Foo(double x, double y, double z) {this.x x;this.y y;this.z z;}/*** Returns the value of x.** return the value of x*/public double getX() {return x;}/*** Sets a new value for x.** param x the new value of x*/public void setX(double x) {this.x x;}/*** Returns the value of y.** return the value of y*/public double getY() {return y;}/*** Sets a new value for y.** param y the new value of y*/public void setY(double y) {this.y y;}/*** Returns the value of z.** return the value of z*/public double getZ() {return z;}/*** Sets a new value for z.** param z the new value of z*/public void setZ(double z) {this.z z;}/*** Generates a hashCode for this object. If any field is* changed to another value, the hashCode may be different.* Two objects with the same values are guaranteed to have* the same hashCode. Two objects with the same hashCode are* not guaranteed to have the same hashCode.** return the hash code*/Overridepublic int hashCode() {int hash 7;hash 31 * hash (Double.hashCode(this.x));hash 31 * hash (Double.hashCode(this.y));hash 31 * hash (Double.hashCode(this.z));return hash;}/*** Compares this object with the specified one for equality.* The other object must be of the same type and not null for* the method to return true.** param other the object to compare with* return {code true} if the objects are equal*/Overridepublic boolean equals(Object other) {return Optional.ofNullable(other).filter(o - getClass().equals(o.getClass())).map(o - (Foo) o).filter(o - this.x o.x).filter(o - this.y o.y).filter(o - this.z o.z).isPresent();}
} 每个组件都实现为一个接口类对因此您可以动态更改实现而无需重写系统的其他部分。 希望这对其他人有帮助 翻译自: https://www.javacodegeeks.com/2016/02/object-oriented-approach-code-generation.html