网站建设 办公系统,ppt制作软件免费模板,优化网站公司价格是多少钱,东营局域网设计Google GitHub托管的项目AutoValue之所以有趣#xff0c;有多种原因。 该项目不仅使为“ 值对象 ”编写更少的Java代码变得容易#xff0c;而且还为Java注释处理的实际应用提供了概念上简单的演示。 该自动/值项目是由提供谷歌的员工凯文Bourrillion和埃蒙麦克马纳斯和许可与… Google GitHub托管的项目AutoValue之所以有趣有多种原因。 该项目不仅使为“ 值对象 ”编写更少的Java代码变得容易而且还为Java注释处理的实际应用提供了概念上简单的演示。 该自动/值项目是由提供谷歌的员工凯文Bourrillion和埃蒙·麦克马纳斯和许可与Apache的版本2的许可 。 《 AutoValue用户指南》简短明了其简洁性反映了项目本身。 用户指南提供了使用AutoValue的简单示例讨论了为什么需要 AutoValue在“ 我如何……”部分中回答了常见问题并概述了与使用AutoValue有关的一些最佳做法 。 以下代码清单包含一个我手写的简单类称为Person 。 编写此类时要牢记AutoValue。 人.java package dustin.examples.autovalue;import com.google.auto.value.AutoValue;/*** Represents an individual as part of demonstration of* GitHub-hosted project google/auto/value* (see https://github.com/google/auto/tree/master/value).*/
AutoValue // concrete extension will be generated by AutoValue
abstract class Person
{/*** Create instance of Person.** param lastName Last name of person.* param firstName First name of person.* param birthYear Birth year of person.* return Instance of Person.*/static Person create(String lastName, String firstName, long birthYear){return new AutoValue_Person(lastName, firstName, birthYear);}/*** Provide Persons last name.** return Last name of person.*/abstract String lastName();/*** Provide Persons first name.** return First name of person.*/abstract String firstName();/*** Provide Persons birth year.** return Persons birth year.*/abstract long birthYear();
} 当使用AutoValue生成完整的“值类”时只需为AutoValue提供一个抽象类故意不支持接口以生成相应的具体扩展。 该abstract类必须使用AutoValue注释进行注释必须提供提供值类实例的static方法并且必须提供暗示值类的受支持字段的public或包范围的abstract访问器方法。 在上面的代码清单中静态实例创建方法实例化了AutoValue_Person对象但是我没有定义这样的AutoValue_Person类。 此类是AutoValue生成的类的名称该类将在对Person.java进行javac编译时执行AutoValue的注释处理时生成。 由此我们可以看到AutoValue生成的类的命名约定 AutoValue_放在源类的名称之前以形成生成的类的名称。 当Person.java在编译过程中应用AutoValue注释处理进行编译时将生成生成的类。 就我而言使用AutoValue 1.2 / auto-value-1.2.jar 生成了以下代码 AutoValue_Person.java由AutoValue生成 package dustin.examples.autovalue;import javax.annotation.Generated;Generated(com.google.auto.value.processor.AutoValueProcessor)final class AutoValue_Person extends Person {private final String lastName;private final String firstName;private final long birthYear;AutoValue_Person(String lastName,String firstName,long birthYear) {if (lastName null) {throw new NullPointerException(Null lastName);}this.lastName lastName;if (firstName null) {throw new NullPointerException(Null firstName);}this.firstName firstName;this.birthYear birthYear;}OverrideString lastName() {return lastName;}OverrideString firstName() {return firstName;}Overridelong birthYear() {return birthYear;}Overridepublic String toString() {return Person{ lastName lastName , firstName firstName , birthYear birthYear };}Overridepublic boolean equals(Object o) {if (o this) {return true;}if (o instanceof Person) {Person that (Person) o;return (this.lastName.equals(that.lastName())) (this.firstName.equals(that.firstName())) (this.birthYear that.birthYear());}return false;}Overridepublic int hashCode() {int h 1;h * 1000003;h ^ this.lastName.hashCode();h * 1000003;h ^ this.firstName.hashCode();h * 1000003;h ^ (this.birthYear 32) ^ this.birthYear;return h;}} 通过检查生成的代码可以得出以下几点结论 生成的类扩展实现继承了手写的抽象类从而允许使用代码使用手写类的API而不必知道正在使用生成的类。 即使没有在源类中直接定义任何字段也将生成字段 AutoValue解释了提供的abstract访问器方法中的字段。 生成的类不为字段提供“设置” / mutator方法get / accessor方法。 这是AutoValue的故意设计决策 因为Value Objects的关键概念是它们是不可变的。 考虑到每个字段的类型将自动为每个字段适当地生成equalsObject hashCode和toString的实现。 在源类和方法上的Javadoc注释不会在生成的扩展类上重现。 使用诸如AutoValue生成之类的方法的主要优点之一是开发人员可以将精力集中在特定类应支持的更高级的概念上并且代码生成可确保一致正确地实现较低级别的细节。 但是使用这种方法时需要牢记一些注意事项该文档的“ 最佳实践”部分是一个不错的地方可以让您早日阅读一下以了解AutoValue的假设是否适合您的情况。 当开发人员受过足够的训练以查看和维护abstract “源” Java类而不是生成的类时AutoValue最有可能会有所帮助。 下次注释处理再次生成该类时对生成类的更改将被覆盖否则必须停止该类的生成以免发生这种情况。 您将需要设置build / IDE以便将生成的类视为“源代码”以便abstract类可以编译。 如果将可变字段与AutoValue一起使用时如果要保持不变性则必须格外小心通常选择使用Value Objects时就是这种情况。 查看“ 最佳实践”和“我如何……”部分以确保没有任何AutoValue的设计假设使它不利于您的需求。 结论 AutoValue允许开发人员编写更简洁的代码重点放在高级细节上并将繁琐的低级通常是容易出错的细节的实现委派给AutoValue以自动生成代码。 这类似于IDE的源代码生成可以执行的操作但是AutoValue相对于IDE方法的优势在于AutoValue可以在每次编译代码时重新生成源代码从而使生成的代码保持最新。 AutoValue的这一优势也是Java自定义注释处理功能强大的一个很好的例子。 翻译自: https://www.javacodegeeks.com/2016/06/autovalue-generated-immutable-value-classes.html