diff --git a/README.md b/README.md index bea8dab..b210663 100644 --- a/README.md +++ b/README.md @@ -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' } ``` @@ -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") } ``` @@ -117,19 +117,19 @@ Add this to your project's `pom.xml`: com.driver733.mapstruct-fluent - annotations - 1.0.0 + common + 1.0.2 com.driver733.mapstruct-fluent processor - 1.0.0 + 1.0.2 provided com.driver733.mapstruct-fluent processor-spring - 1.0.0 + 1.0.2 provided diff --git a/processors/common/src/main/kotlin/com/driver733/mapstructfluent/Common.kt b/processors/common/src/main/kotlin/com/driver733/mapstructfluent/Common.kt index b5b19cc..32af79d 100644 --- a/processors/common/src/main/kotlin/com/driver733/mapstructfluent/Common.kt +++ b/processors/common/src/main/kotlin/com/driver733/mapstructfluent/Common.kt @@ -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 @@ -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( diff --git a/processors/processor-spring/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt b/processors/processor-spring/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt index dc1bac5..bebe5c8 100644 --- a/processors/processor-spring/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt +++ b/processors/processor-spring/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt @@ -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 @@ -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( @@ -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() } diff --git a/processors/processor/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt b/processors/processor/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt index ba694d5..939235f 100644 --- a/processors/processor/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt +++ b/processors/processor/src/main/kotlin/com/driver733/mapstructfluent/Processor.kt @@ -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 @@ -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( @@ -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() }