NoteDeep

正在学习: mapstruct

澄清本质

学习的本质是使得大脑的突触建立更有力的连接。学习后,当有信息输入大脑时,能产出正确的输出。
如当遇到需求、bug、问题时,能清楚地知道这是不是 xxx 的问题,以及能否通过 xxx 解决
Learn by do、teach、create

目标

让 mapstruct 支持复杂 proto 对象间的转化,并且不影响旧的转化

提出问题 & 收集信息

文档 https://mapstruct.org/documentation/stable/reference/html/
源码 https://github.com/mapstruct/mapstruct
示例 https://github.com/mapstruct/mapstruct-examples
mapstruct 是什么?
MapStruct is a Java annotation processor for the generation of type-safe bean mapping classes.
The general philosophy of MapStruct is to generate code which looks as much as possible as if you had written it yourself from hand. In particular this means that the values are copied from source to target by plain getter/setter invocations instead of reflection or similar.
JavaBean 是什么?
A JavaBean is just a standard
  1. All properties are private (use getters/setters)
  2. Implements Serializable.
That's it. It's just a convention. Lots of libraries depend on it though.
Annotation Processing Tool (apt) 是什么 ?
apt is a command-line utility for annotation processing.
mapstruct 是怎么实现生成代码的?生成什么样的代码?
by using plain method invocations instead of reflection(不使用反射)
e.g. seatCount for a property with the accessor methods getSeatCount() and setSeatCount().
protobuf message 类是基于 builder 的模式
https://mapstruct.org/documentation/stable/reference/html/#mapping-with-builders
getter/setter 的原理:
JavaX/lang.model/element/* 是什么?
https://docs.oracle.com/javase/7/docs/api/javax/lang/model/element/package-summary.html
https://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/package-summary.html
生成代码的更详细的原理,是如何选择到 AccessorNamingStrategy 的?
在 AnnotationProcessorContext 中选择的
如何查看 verbose 输出、AnnotationProcessorPath 以及 compileClasspath?
compileJava { options.compilerArgs += [ '-Amapstruct.defaultComponentModel=spring', '-Amapstruct.verbose=true' ] } tasks.withType(JavaCompile) { doFirst { println "AnnotationProcessorPath for $name is ${options.getAnnotationProcessorPath().getFiles()}" sourceSets.main.compileClasspath.each { println it} } }

评论列表

    正在学习: mapstruct