Skip to content

Commit

Permalink
fix: nullable types handling
Browse files Browse the repository at this point in the history
  • Loading branch information
driver733 committed Aug 10, 2020
1 parent a179b15 commit 5382592
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ Add this to your project's `build.gradle`:

```groovy
dependencies {
implementation 'com.driver733.mapstruct-fluent:annotation:1.0.0'
annotationProcessor 'com.driver733.mapstruct-fluent:processor:1.0.0'
annotationProcessor 'com.driver733.mapstruct-fluent:processor-spring:1.0.0'
implementation 'com.driver733.mapstruct-fluent:common:1.0.2'
annotationProcessor 'com.driver733.mapstruct-fluent:processor:1.0.2'
annotationProcessor 'com.driver733.mapstruct-fluent:processor-spring:1.0.2'
}
```

Expand All @@ -103,9 +103,9 @@ dependencies {

```kotlin
dependencies {
implementation("com.driver733.mapstruct-fluent:annotation:1.0.0")
kapt("com.driver733.mapstruct-fluent:processor:1.0.0")
kapt("com.driver733.mapstruct-fluent:processor-spring:1.0.0")
implementation("com.driver733.mapstruct-fluent:common:1.0.2")
kapt("com.driver733.mapstruct-fluent:processor:1.0.2")
kapt("com.driver733.mapstruct-fluent:processor-spring:1.0.2")
}
```

Expand All @@ -117,19 +117,19 @@ Add this to your project's `pom.xml`:
<dependencies>
<dependency>
<groupId>com.driver733.mapstruct-fluent</groupId>
<artifactId>annotations</artifactId>
<version>1.0.0</version>
<artifactId>common</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.driver733.mapstruct-fluent</groupId>
<artifactId>processor</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.driver733.mapstruct-fluent</groupId>
<artifactId>processor-spring</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.driver733.mapstructfluent

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import org.jetbrains.annotations.Nullable
import org.mapstruct.AfterMapping
import org.mapstruct.BeforeMapping
import org.mapstruct.Mapper
Expand Down Expand Up @@ -58,11 +59,19 @@ fun Name.capitalize() = this.toString().capitalize()

fun FileSpec.Companion.builder(fileName: String) = builder("", fileName)

fun className(vararg simpleNames: String) = ClassName("", *simpleNames)
fun className(vararg elements: Element) = ClassName("", *elements.map { it.simpleName.toString() }.toTypedArray())

fun VariableElement.kotlinType() = this.asType().asTypeName().toKotlinType()
fun VariableElement.kotlinType() = kotlinTypeWithInferredNullability()

fun TypeName.toKotlinType(): TypeName =
private fun VariableElement.kotlinTypeWithInferredNullability() =
typeToKotlinType()
.let { if (isNullable()) it.copy(true) else it.copy(false) }

private fun VariableElement.typeToKotlinType() = asType().asTypeName().toKotlinType()

fun Element.isNullable() = this.getAnnotation(Nullable::class.java) != null

private fun TypeName.toKotlinType(): TypeName =
when (this) {
is ParameterizedTypeName -> {
(rawType.toKotlinType() as ClassName).parameterizedBy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.driver733.mapstructfluent
import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.asTypeName
import java.io.File
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
Expand All @@ -22,10 +21,11 @@ class MapstructSpringFluentExtensionsAnnotationProcessor : AbstractProcessor() {

private fun processMapper(method: ExecutableElement, mapper: Element, src: String?) {
FileSpec.builder(
"${method.simpleName.capitalize()}FluentSpringExtensions"
processingEnv.elementUtils.getPackageOf(method).toString(),
"${className(mapper).simpleName.capitalize()}$${method.simpleName.capitalize()}FluentSpringExtensions"
).addImport(
processingEnv.elementUtils.getPackageOf(method).toString(),
className(mapper.simpleName.toString()).simpleName
className(mapper).simpleName
).addImport(
"com.driver733.mapstructfluent", "getBean"
).addFunction(
Expand All @@ -34,7 +34,6 @@ class MapstructSpringFluentExtensionsAnnotationProcessor : AbstractProcessor() {
.addStatement(
"return ${mapper.simpleName}::class.java.getBean().${method.simpleName}(this)"
)
.returns(method.returnType.asTypeName().toKotlinType())
.build()
).build().writeTo(
File(src!!).apply { mkdir() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.driver733.mapstructfluent
import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.asTypeName
import java.io.File
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
Expand All @@ -22,10 +21,11 @@ class MapstructFluentExtensionsAnnotationProcessor : AbstractProcessor() {

private fun processMapper(method: ExecutableElement, mapper: Element, src: String?) {
FileSpec.builder(
"${method.simpleName.capitalize()}FluentExtensions"
processingEnv.elementUtils.getPackageOf(method).toString(),
"${className(mapper).simpleName.capitalize()}$${method.simpleName.capitalize()}FluentExtensions"
).addImport(
processingEnv.elementUtils.getPackageOf(method).toString(),
className(mapper.simpleName.toString()).simpleName
className(mapper).simpleName
).addImport(
"org.mapstruct.factory.Mappers", ""
).addFunction(
Expand All @@ -34,7 +34,6 @@ class MapstructFluentExtensionsAnnotationProcessor : AbstractProcessor() {
.addStatement(
"return Mappers.getMapper(${mapper.simpleName}::class.java).${method.simpleName}(this)"
)
.returns(method.returnType.asTypeName().toKotlinType())
.build()
).build().writeTo(
File(src!!).apply { mkdir() }
Expand Down

0 comments on commit 5382592

Please sign in to comment.