├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.gradle ├── debug ├── README.md ├── build.gradle └── src │ └── main │ └── kotlin │ └── com │ └── github │ └── pozo │ └── domain │ └── Domain.kt ├── example ├── application │ ├── build.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── github │ │ │ │ └── pozo │ │ │ │ └── TestJavaClass.java │ │ └── kotlin │ │ │ └── com │ │ │ └── github │ │ │ └── pozo │ │ │ ├── App.kt │ │ │ └── mapper │ │ │ ├── DivisionMapper.kt │ │ │ ├── OrderMapper.kt │ │ │ ├── PersonMapper.kt │ │ │ ├── ProductMapper.kt │ │ │ └── RoleMapper.kt │ │ └── test │ │ └── kotlin │ │ └── com │ │ └── github │ │ └── pozo │ │ └── MapperTest.kt ├── build.gradle ├── domain │ ├── build.gradle │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── github │ │ └── pozo │ │ └── domain │ │ └── Domain.kt ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── mapstruct-kotlin-builder ├── build.gradle └── src │ └── main │ └── kotlin │ └── com │ └── github │ └── pozo │ └── KotlinBuilder.kt ├── mapstruct-kotlin-processor ├── build.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── pozo │ │ │ └── KotlinBuilderProvider.java │ ├── kotlin │ │ └── com │ │ │ └── github │ │ │ └── pozo │ │ │ ├── BuilderGenerator.kt │ │ │ └── BuilderProcessor.kt │ └── resources │ │ └── META-INF │ │ ├── gradle │ │ └── incremental.annotation.processors │ │ └── services │ │ └── org.mapstruct.ap.spi.BuilderProvider │ └── test │ └── java │ └── com │ └── github │ └── pozo │ ├── KotlinBuilderProviderTest.java │ └── KotlinBuilderTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore binaries 2 | 3 | *.class 4 | *.log 5 | *.ctxt 6 | .mtj.tmp/ 7 | *.jar 8 | *.war 9 | *.nar 10 | *.ear 11 | *.zip 12 | *.tar.gz 13 | *.rar 14 | hs_err_pid* 15 | 16 | # Secrets 17 | 18 | /**/*.pem 19 | /**/*.gpg 20 | 21 | # Ignore idea related stuff 22 | 23 | /**/.idea/** 24 | /**/out/ 25 | 26 | # Ignore gradle related stuff 27 | 28 | .gradle 29 | /**/gradle.properties 30 | /**/gradle-app.setting 31 | /**/local.properties 32 | /**/.gradletasknamecache 33 | /**/gradle-wrapper.jar 34 | /**/gradle-wrapper.properties 35 | /**/build/ 36 | 37 | # Allow only the root gradle wrapper 38 | 39 | !/gradle/wrapper/gradle-wrapper.jar 40 | !/gradle/wrapper/gradle-wrapper.properties 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.4.0.0 (2020/07/25) 4 | 5 | #### Enhancements: 6 | 7 | - [#18](https://github.com/Pozo/mapstruct-kotlin/issues/18) Add support for Gradle incremental annotation processing 8 | 9 | ## 1.3.1.2 (2020/04/13) 10 | 11 | #### Enhancements: 12 | 13 | - [#8](https://github.com/Pozo/mapstruct-kotlin/issues/8) Add compiler error if @KotlinBuilder applied to non data class 14 | 15 | #### Bug Fixes: 16 | 17 | - [#10](https://github.com/Pozo/mapstruct-kotlin/issues/10) Fix missing support for generic types 18 | - [#11](https://github.com/Pozo/mapstruct-kotlin/issues/11) Fix missing imports for nested types 19 | 20 | ## 1.3.1.1 (2020/01/19) 21 | 22 | #### Bug Fixes: 23 | 24 | - [#4](https://github.com/Pozo/mapstruct-kotlin/issues/4) Fix data classes in another module 25 | 26 | ## 1.3.1.0 (2019/12/27) 27 | *No changelog for this release.* 28 | 29 | 30 | ## 1.3.0.Beta2 (2019/06/04) 31 | Proof of concept implementation [#1672](https://github.com/mapstruct/mapstruct/issues/1672) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Zoltan Polgar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## mapstruct-kotlin 2 | 3 | > :warning: Since 1.4 MapStruct has support for using constructor arguments when instantiating mapping targets. [This also works with Kotlin data classes](https://github.com/mapstruct/mapstruct/issues/1672#issuecomment-733843277). 4 | 5 | Since mapstruct [1.3.0.Beta2](http://mapstruct.org/news/2018-07-15-mapstruct-1_3_0_Beta1-is-out-with-builder-support/) it's possible to use builders for immutable classes. [According to the documentation](http://mapstruct.org/documentation/dev/reference/html/#mapping-with-builders) you can implement your custom builder provider logic. This project take advantage of this and provide a custom `BuilderProvider` for kotlin data classes. 6 | 7 | So instead of this ([source](https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-kotlin)) 8 | ```kotlin 9 | data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) { 10 | // Necessary for MapStruct 11 | constructor() : this(null, null, null, null) 12 | } 13 | ``` 14 | We can do this 15 | ```kotlin 16 | @KotlinBuilder 17 | data class PersonDto(val firstName: String, val lastName: String, val phone: String, val birthdate: LocalDate) 18 | ``` 19 | With a mapper 20 | ```kotlin 21 | @Mapper 22 | interface PersonMapper { 23 | fun map(person: Person): PersonDto 24 | } 25 | ``` 26 | #### Usage 27 | First apply kapt plugin 28 | ```groovy 29 | apply plugin: 'kotlin-kapt' 30 | ``` 31 | Then add these to your project as dependency 32 | ```groovy 33 | api("com.github.pozo:mapstruct-kotlin:1.3.1.2") 34 | kapt("com.github.pozo:mapstruct-kotlin-processor:1.3.1.2") 35 | ``` 36 | Check out the directory `example` for a basic usage example. 37 | #### Versioning 38 | 39 | For example in case of `1.3.1.1` the first part `1.3.1` is the mapstruct version number and the last digit `1` reserved for future patches. 40 | #### Project structure 41 | 42 | - `mapstruct-kotlin-builder` contains only the `KotlinBuilder` annotation 43 | - `mapstruct-kotlin-processor` responsible for generating the builders for the kotlin data classes with the help of a custom `DefaultBuilderProvider` 44 | - `example` responsible for demonstrating this library usage 45 | 46 | #### Build and run the example application 47 | 48 | ./gradlew -p example clean build 49 | 50 | # TODO 51 | 52 | - ~~Map with custom types are not working~~ 53 | - ~~Look over [kotlin-builder-annotation](https://github.com/ThinkingLogic/kotlin-builder-annotation) project and replace with class generating module (builder-processor)~~ 54 | - ~~Writing tests~~ 55 | - ~~Versioning and release process~~ 56 | 57 | # Licensing 58 | 59 | Please see LICENSE file 60 | 61 | # Contact 62 | 63 | Zoltan Polgar - pozo@gmx.com 64 | 65 | Please do not hesitate to contact me if you have any further questions. 66 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | allprojects { 2 | group 'com.github.pozo' 3 | version '1.4.0.0' 4 | } 5 | 6 | buildscript { 7 | ext { 8 | kotlin_version = '1.3.72' 9 | kotlinxMetadataVersion = '0.0.5' 10 | kotlinBuilderAnnotationVersion = '1.0-SNAPSHOT' 11 | autoServiceVersion = '1.0-rc7' 12 | mapstructVersion = '1.4.0.Beta3' 13 | javaPoetVersion = '1.11.1' 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | maven { 19 | url('https://plugins.gradle.org/m2/') 20 | } 21 | } 22 | 23 | dependencies { 24 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /debug/README.md: -------------------------------------------------------------------------------- 1 | ## This module is responsible for help the debugging of the kotlin-processor 2 | 3 | First you have to create a `Remote run configuration` in IntelliJ 4 | 5 | Host: localhost 6 | Port: 5005 7 | Command line arguments: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 8 | 9 | Then run the following command 10 | 11 | gradle --no-daemon -Dorg.gradle.debug=true clean build 12 | 13 | And run the previously created Remote configuration in `Debug` mode. Please note that without the `kapt.use.worker.api=true` entry in the `gradle.properties` it won't work. -------------------------------------------------------------------------------- /debug/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply plugin: 'kotlin-kapt' 3 | 4 | sourceCompatibility = JavaVersion.VERSION_1_8 5 | 6 | repositories { 7 | mavenCentral() 8 | maven { url "https://kotlin.bintray.com/kotlinx" } 9 | } 10 | 11 | dependencies { 12 | api(project(':mapstruct-kotlin-builder')) 13 | kapt(project(':mapstruct-kotlin-processor')) 14 | 15 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 16 | testCompile group: 'junit', name: 'junit', version: '4.12' 17 | } 18 | 19 | compileKotlin { 20 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 21 | } 22 | compileTestKotlin { 23 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 24 | } 25 | -------------------------------------------------------------------------------- /debug/src/main/kotlin/com/github/pozo/domain/Domain.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.domain 2 | 3 | import com.github.pozo.KotlinBuilder 4 | 5 | interface Bar 6 | 7 | @KotlinBuilder 8 | data class Foo( 9 | val bar: T, 10 | val kar: K 11 | ) { 12 | 13 | fun bar(k: K): T { 14 | return bar 15 | } 16 | } 17 | 18 | @KotlinBuilder 19 | data class Light( 20 | val bar: Bar, 21 | val lol: String 22 | ) -------------------------------------------------------------------------------- /example/application/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.github.pozo' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext { 6 | kotlin_version = '1.3.72' 7 | mapstructVersion = '1.3.1.Final' 8 | mapstructKotlinVersion = '1.3.1.2' 9 | } 10 | 11 | repositories { 12 | mavenCentral() 13 | maven { 14 | url('https://plugins.gradle.org/m2/') 15 | } 16 | } 17 | 18 | dependencies { 19 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") 20 | } 21 | } 22 | 23 | apply plugin: 'kotlin' 24 | apply plugin: 'kotlin-kapt' 25 | 26 | sourceCompatibility = JavaVersion.VERSION_1_8 27 | 28 | repositories { 29 | mavenLocal() 30 | mavenCentral() 31 | maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 32 | maven { url "https://kotlin.bintray.com/kotlinx" } 33 | } 34 | 35 | dependencies { 36 | implementation project(":domain") 37 | 38 | api("com.github.pozo:mapstruct-kotlin:${mapstructKotlinVersion}") 39 | api("org.mapstruct:mapstruct:${mapstructVersion}") 40 | 41 | kapt("org.mapstruct:mapstruct-processor:${mapstructVersion}") 42 | kapt("com.github.pozo:mapstruct-kotlin-processor:${mapstructKotlinVersion}") 43 | 44 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 45 | 46 | testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" 47 | testImplementation 'junit:junit:4.12' 48 | } 49 | 50 | compileKotlin { 51 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 52 | } 53 | compileTestKotlin { 54 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 55 | } 56 | 57 | -------------------------------------------------------------------------------- /example/application/src/main/java/com/github/pozo/TestJavaClass.java: -------------------------------------------------------------------------------- 1 | package com.github.pozo; 2 | 3 | @KotlinBuilder 4 | public class TestJavaClass { 5 | 6 | private String name; 7 | 8 | private int age; 9 | 10 | public static void main(String[] args) { 11 | System.out.println("args = " + args); 12 | } 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public int getAge() { 23 | return age; 24 | } 25 | 26 | public void setAge(int age) { 27 | this.age = age; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/App.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo 2 | 3 | import com.github.pozo.domain.Person 4 | 5 | fun main(args: Array) { 6 | println("args = $args") 7 | Person("Test", "Test", 2, null) 8 | } -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/mapper/DivisionMapper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.mapper 2 | 3 | import com.github.pozo.domain.Division 4 | import com.github.pozo.domain.DivisionDto 5 | import org.mapstruct.Mapper 6 | import org.mapstruct.Mappings 7 | 8 | @Mapper 9 | interface DivisionMapper { 10 | 11 | @Mappings 12 | fun toDto(division: Division): DivisionDto 13 | 14 | 15 | @Mappings 16 | fun toOrder(division: Division): Division 17 | } -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/mapper/OrderMapper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.mapper 2 | 3 | import com.github.pozo.domain.Order 4 | import com.github.pozo.domain.OrderDto 5 | import org.mapstruct.Mapper 6 | import org.mapstruct.Mapping 7 | import org.mapstruct.Mappings 8 | 9 | @Mapper 10 | interface OrderMapper { 11 | 12 | @Mappings( 13 | value = [ 14 | Mapping(source = "shipped", target = "isShipped") 15 | ] 16 | ) 17 | fun toDto(order: Order): OrderDto 18 | 19 | 20 | @Mappings( 21 | value = [ 22 | Mapping(source = "shipped", target = "isShipped") 23 | ] 24 | ) 25 | fun toOrder(order: OrderDto): Order 26 | } -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/mapper/PersonMapper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.mapper 2 | 3 | import com.github.pozo.domain.Person 4 | import com.github.pozo.domain.PersonDto 5 | import org.mapstruct.InheritInverseConfiguration 6 | import org.mapstruct.Mapper 7 | import org.mapstruct.Mapping 8 | import org.mapstruct.Mappings 9 | 10 | @Mapper(uses = [RoleMapper::class]) 11 | interface PersonMapper { 12 | 13 | @Mappings( 14 | value = [ 15 | Mapping(target = "role", ignore = true), 16 | Mapping(target = "phone", ignore = true), 17 | Mapping(target = "birthDate", ignore = true), 18 | Mapping(target = "role.id", source = "role.id"), 19 | Mapping(target = "role.name", source = "role.name") 20 | ] 21 | ) 22 | fun toDto(person: Person): PersonDto 23 | 24 | @Mappings( 25 | value = [ 26 | Mapping(target = "age", ignore = true), 27 | Mapping(target = "role.abbreviation", ignore = true) 28 | ] 29 | ) 30 | @InheritInverseConfiguration 31 | fun toPerson(person: PersonDto): Person 32 | 33 | } -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/mapper/ProductMapper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.mapper 2 | 3 | import com.github.pozo.domain.Product 4 | import com.github.pozo.domain.ProductDto 5 | import org.mapstruct.Mapper 6 | import org.mapstruct.Mappings 7 | 8 | @Mapper 9 | interface ProductMapper { 10 | 11 | @Mappings 12 | fun toDto(product: Product): ProductDto 13 | 14 | 15 | @Mappings 16 | fun toProduct(product: ProductDto): Product 17 | } -------------------------------------------------------------------------------- /example/application/src/main/kotlin/com/github/pozo/mapper/RoleMapper.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.mapper 2 | 3 | import com.github.pozo.domain.Role 4 | import com.github.pozo.domain.RoleDto 5 | import org.mapstruct.InheritInverseConfiguration 6 | import org.mapstruct.Mapper 7 | import org.mapstruct.Mapping 8 | import org.mapstruct.Mappings 9 | 10 | @Mapper 11 | interface RoleMapper { 12 | 13 | @Mappings( 14 | value = [Mapping(target = "ignoredAttr", ignore = true)] 15 | ) 16 | fun toDto(role: Role): RoleDto 17 | 18 | @InheritInverseConfiguration 19 | fun toRole(role: RoleDto): Role 20 | } -------------------------------------------------------------------------------- /example/application/src/test/kotlin/com/github/pozo/MapperTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo 2 | 3 | import com.github.pozo.domain.* 4 | import com.github.pozo.mapper.* 5 | import org.junit.Assert.* 6 | import org.junit.Before 7 | import org.junit.Test 8 | 9 | class MapperTest { 10 | 11 | private lateinit var person: Person 12 | 13 | private lateinit var role: Role 14 | 15 | private lateinit var personMapper: PersonMapper 16 | 17 | private lateinit var roleMapper: RoleMapper 18 | 19 | private lateinit var orderMapper: OrderMapper 20 | 21 | private lateinit var productMapper: ProductMapper 22 | 23 | @Before 24 | fun setup() { 25 | personMapper = PersonMapperImpl() 26 | roleMapper = RoleMapperImpl() 27 | orderMapper = OrderMapperImpl() 28 | productMapper = ProductMapperImpl() 29 | person = Person("Test", "Test", 2, null) 30 | role = Role(1, "role one", "R1") 31 | } 32 | 33 | @Test 34 | fun testDto() { 35 | val personDto = personMapper.toDto(person) 36 | assertEquals(person.firstName, personDto.firstName) 37 | assertEquals(person.lastName, personDto.lastName) 38 | assertNull(personDto.birthDate) 39 | assertNull(personDto.phone) 40 | assertNull(personDto.role) 41 | } 42 | 43 | @Test 44 | fun testPerson() { 45 | val personDto = personMapper.toDto(person) 46 | val personFromDto = personMapper.toPerson(personDto) 47 | assertEquals(person.firstName, personFromDto.firstName) 48 | assertEquals(person.lastName, personFromDto.lastName) 49 | } 50 | 51 | @Test 52 | fun testRoleDto() { 53 | val roleDto = roleMapper.toDto(role) 54 | assertEquals(role.id, roleDto.id) 55 | assertEquals(role.name, roleDto.name) 56 | assertNull(roleDto.ignoredAttr) 57 | } 58 | 59 | @Test 60 | fun testRole() { 61 | val roleDto = roleMapper.toDto(role) 62 | val roleFromDto = roleMapper.toRole(roleDto) 63 | assertEquals(roleFromDto.id, roleDto.id) 64 | assertEquals(roleFromDto.name, roleDto.name) 65 | } 66 | 67 | @Test 68 | fun testPersonsRoleDto() { 69 | val copy = person.copy(role = role) 70 | val personDto = personMapper.toDto(copy) 71 | val roleDto = roleMapper.toDto(role) 72 | 73 | assertNotNull(personDto.role) 74 | assertEquals(personDto.role, roleDto) 75 | } 76 | 77 | @Test 78 | fun testOrderRoleDto() { 79 | val role = Role(666, "Developer", "R1") 80 | val person = Person("Test", "Michael", 2, role) 81 | val division = Division(666, person) 82 | val product = Product(5, 999, division) 83 | val order = Order(isShipped = true, sent = true, product = product) 84 | 85 | val orderDto = orderMapper.toDto(order) 86 | val orderMapped = orderMapper.toOrder(orderDto) 87 | 88 | assertEquals(order, orderMapped) 89 | } 90 | } -------------------------------------------------------------------------------- /example/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.github.pozo' 2 | version '1.0-SNAPSHOT' 3 | 4 | 5 | -------------------------------------------------------------------------------- /example/domain/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.github.pozo' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext { 6 | kotlin_version = '1.3.72' 7 | mapstructVersion = '1.3.1.Final' 8 | mapstructKotlinVersion = '1.3.1.2' 9 | } 10 | 11 | repositories { 12 | mavenCentral() 13 | maven { 14 | url('https://plugins.gradle.org/m2/') 15 | } 16 | } 17 | 18 | dependencies { 19 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") 20 | } 21 | } 22 | 23 | apply plugin: 'kotlin' 24 | apply plugin: 'kotlin-kapt' 25 | 26 | sourceCompatibility = JavaVersion.VERSION_1_8 27 | 28 | repositories { 29 | mavenLocal() 30 | mavenCentral() 31 | maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 32 | maven { url "https://kotlin.bintray.com/kotlinx" } 33 | } 34 | 35 | dependencies { 36 | api("com.github.pozo:mapstruct-kotlin:${mapstructKotlinVersion}") 37 | 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 39 | 40 | kapt("com.github.pozo:mapstruct-kotlin-processor:${mapstructKotlinVersion}") 41 | 42 | testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" 43 | testImplementation 'junit:junit:4.12' 44 | } 45 | 46 | compileKotlin { 47 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 48 | } 49 | compileTestKotlin { 50 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 51 | } 52 | 53 | -------------------------------------------------------------------------------- /example/domain/src/main/kotlin/com/github/pozo/domain/Domain.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo.domain 2 | 3 | import com.github.pozo.KotlinBuilder 4 | import java.time.LocalDate 5 | 6 | // dto elements 7 | 8 | @KotlinBuilder 9 | data class RoleDto(val id: Int, val name: String, val abbreviation: String, val ignoredAttr: Int?) 10 | 11 | @KotlinBuilder 12 | data class PersonDto( 13 | val firstName: String, 14 | val phone: String?, 15 | val birthDate: LocalDate?, 16 | val lastName: String, 17 | val age: Int, 18 | val role: RoleDto? 19 | ) 20 | 21 | @KotlinBuilder 22 | data class DivisionDto(val level: Int, val coworker: PersonDto) 23 | 24 | @KotlinBuilder 25 | data class ProductDto(val quality: Int, val price: Int, val division: DivisionDto) 26 | 27 | @KotlinBuilder 28 | data class OrderDto(val isShipped: Boolean, val sent: Boolean, val product: ProductDto) 29 | 30 | // domain elements 31 | @KotlinBuilder 32 | data class Role(val id: Int, val name: String, val abbreviation: String?) 33 | 34 | @KotlinBuilder 35 | data class Person(val firstName: String, val lastName: String, val age: Int, val role: Role?) 36 | 37 | @KotlinBuilder 38 | data class Division(val level: Int, val coworker: Person) 39 | 40 | @KotlinBuilder 41 | data class Product(val quality: Int, val price: Int, val division: Division) 42 | 43 | @KotlinBuilder 44 | data class Order(val isShipped: Boolean, val sent: Boolean, val product: Product) -------------------------------------------------------------------------------- /example/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /example/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'example' 2 | include 'domain' 3 | include 'application' 4 | 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pozo/mapstruct-kotlin/85b95d912f263e0bbeb40e5158c2593d08ce96db/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 24 21:54:25 CET 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /mapstruct-kotlin-builder/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin' 2 | apply plugin: 'maven-publish' 3 | apply plugin: 'signing' 4 | 5 | sourceCompatibility = JavaVersion.VERSION_1_8 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 13 | } 14 | 15 | task sourcesJar(type: Jar, dependsOn: classes) { 16 | classifier = 'sources' 17 | from sourceSets.main.allSource 18 | } 19 | 20 | task javadocJar(type: Jar, dependsOn: javadoc) { 21 | classifier = 'javadoc' 22 | from javadoc.destinationDir 23 | } 24 | 25 | artifacts { 26 | archives sourcesJar 27 | archives javadocJar 28 | } 29 | 30 | publishing { 31 | publications { 32 | maven(MavenPublication) { 33 | artifactId = 'mapstruct-kotlin' 34 | 35 | from components.java 36 | artifact sourcesJar 37 | artifact javadocJar 38 | 39 | pom { 40 | name = 'mapstruct-kotlin-builder' 41 | description = 'KotlinBuilder annotation for plugin for mapstruct' 42 | url = 'https://github.com/Pozo/mapstruct-kotlin' 43 | licenses { 44 | license { 45 | name = 'MIT License' 46 | url = 'https://opensource.org/licenses/MIT' 47 | } 48 | } 49 | developers { 50 | developer { 51 | id = 'pozo' 52 | name = 'Zoltan Polgar' 53 | email = 'pozo@gmx.com' 54 | } 55 | } 56 | scm { 57 | url = 'https://github.com/Pozo/mapstruct-kotlin/tree/master' 58 | connection = 'scm:git:git://github.com/Pozo/mapstruct-kotlin.git' 59 | developerConnection = 'scm:git:ssh://github.com:Pozo/mapstruct-kotlin.git' 60 | } 61 | } 62 | } 63 | } 64 | repositories { 65 | maven { 66 | url "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 67 | credentials { 68 | username "${nexusUsername}" 69 | password "${nexusPassword}" 70 | } 71 | } 72 | } 73 | } 74 | 75 | signing { 76 | useGpgCmd() 77 | sign publishing.publications.maven 78 | } 79 | 80 | compileKotlin { 81 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 82 | } 83 | compileTestKotlin { 84 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 85 | } -------------------------------------------------------------------------------- /mapstruct-kotlin-builder/src/main/kotlin/com/github/pozo/KotlinBuilder.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo 2 | 3 | @Target(AnnotationTarget.CLASS) 4 | @Retention(AnnotationRetention.RUNTIME) 5 | annotation class KotlinBuilder 6 | -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'kotlin' 3 | apply plugin: 'kotlin-kapt' 4 | apply plugin: 'maven-publish' 5 | apply plugin: 'signing' 6 | 7 | sourceCompatibility = JavaVersion.VERSION_1_8 8 | 9 | repositories { 10 | mavenCentral() 11 | maven { url "https://kotlin.bintray.com/kotlinx" } 12 | } 13 | 14 | dependencies { 15 | implementation("org.mapstruct:mapstruct:${mapstructVersion}") 16 | implementation "org.mapstruct:mapstruct-processor:${mapstructVersion}" 17 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 18 | implementation "org.jetbrains.kotlinx:kotlinx-metadata-jvm:${kotlinxMetadataVersion}" 19 | implementation "com.google.auto.service:auto-service:${autoServiceVersion}" 20 | implementation "com.squareup:javapoet:${javaPoetVersion}" 21 | 22 | implementation(project(':mapstruct-kotlin-builder')) 23 | 24 | kapt "com.google.auto.service:auto-service:${autoServiceVersion}" 25 | 26 | testImplementation 'org.mockito:mockito-core:2.28.2' 27 | testImplementation 'junit:junit:4.12' 28 | } 29 | 30 | task sourcesJar(type: Jar, dependsOn: classes) { 31 | classifier = 'sources' 32 | from sourceSets.main.allSource 33 | } 34 | 35 | task javadocJar(type: Jar, dependsOn: javadoc) { 36 | classifier = 'javadoc' 37 | from javadoc.destinationDir 38 | } 39 | 40 | artifacts { 41 | archives sourcesJar 42 | archives javadocJar 43 | } 44 | 45 | publishing { 46 | publications { 47 | maven(MavenPublication) { 48 | from components.java 49 | 50 | artifact sourcesJar 51 | artifact javadocJar 52 | pom { 53 | name = 'mapstruct-kotlin-processor' 54 | description = 'KotlinBuilder annotation processor' 55 | url = 'https://github.com/Pozo/mapstruct-kotlin' 56 | licenses { 57 | license { 58 | name = 'MIT License' 59 | url = 'https://opensource.org/licenses/MIT' 60 | } 61 | } 62 | developers { 63 | developer { 64 | id = 'pozo' 65 | name = 'Zoltan Polgar' 66 | email = 'pozo@gmx.com' 67 | } 68 | } 69 | scm { 70 | url = 'https://github.com/Pozo/mapstruct-kotlin/tree/master' 71 | connection = 'scm:git:git://github.com/Pozo/mapstruct-kotlin.git' 72 | developerConnection = 'scm:git:ssh://github.com:Pozo/mapstruct-kotlin.git' 73 | } 74 | } 75 | } 76 | } 77 | repositories { 78 | maven { 79 | url "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 80 | credentials { 81 | username "${nexusUsername}" 82 | password "${nexusPassword}" 83 | } 84 | } 85 | } 86 | } 87 | 88 | signing { 89 | useGpgCmd() 90 | sign publishing.publications.maven 91 | } 92 | 93 | compileKotlin { 94 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 95 | } 96 | compileTestKotlin { 97 | kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8 98 | } -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/main/java/com/github/pozo/KotlinBuilderProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.pozo; 2 | 3 | import org.mapstruct.ap.spi.BuilderInfo; 4 | import org.mapstruct.ap.spi.BuilderProvider; 5 | import org.mapstruct.ap.spi.DefaultBuilderProvider; 6 | import org.mapstruct.ap.spi.MapStructProcessingEnvironment; 7 | import org.mapstruct.ap.spi.TypeHierarchyErroneousException; 8 | 9 | import javax.lang.model.element.Element; 10 | import javax.lang.model.element.ElementKind; 11 | import javax.lang.model.element.ExecutableElement; 12 | import javax.lang.model.element.PackageElement; 13 | import javax.lang.model.element.TypeElement; 14 | import javax.lang.model.type.TypeMirror; 15 | import javax.lang.model.util.ElementFilter; 16 | import java.util.List; 17 | import java.util.stream.Collectors; 18 | 19 | public class KotlinBuilderProvider extends DefaultBuilderProvider implements BuilderProvider { 20 | 21 | private static final String BUILDER_CLASS_POSTFIX = "Builder"; 22 | 23 | private static final String CREATE_METHOD_NAME = "create"; 24 | 25 | private static final String BUILDER_METHOD_NAME = "builder"; 26 | 27 | private static final String KOTLIN_METADATA_CLASS_NAME = "kotlin.Metadata"; 28 | 29 | private static final String KOTLIN_BUILDER_ANNOTATION_CLASS_NAME = "com.github.pozo.KotlinBuilder"; 30 | 31 | @Override 32 | public void init(MapStructProcessingEnvironment processingEnvironment) { 33 | super.init(processingEnvironment); 34 | } 35 | 36 | @Override 37 | public BuilderInfo findBuilderInfo(TypeMirror type) { 38 | final TypeElement typeElement = getTypeElement(type); 39 | if (typeElement == null) { 40 | return null; 41 | } 42 | if (isKotlinBuilder(typeElement)) { 43 | return findBuilder(typeElement); 44 | } else { 45 | return super.findBuilderInfo(type); 46 | } 47 | } 48 | 49 | boolean isKotlinBuilder(TypeElement typeElement) { 50 | return isAnnotatedByKotlin(typeElement) && isAnnotatedByKotlinBuilder(typeElement); 51 | } 52 | 53 | BuilderInfo findBuilder(TypeElement typeElement) { 54 | final TypeElement builder = asBuilderElement(typeElement); 55 | if (builder == null) { 56 | throw new TypeHierarchyErroneousException(typeElement); 57 | } 58 | 59 | List builderMethods = ElementFilter.methodsIn(builder.getEnclosedElements()) 60 | .stream() 61 | .filter(executableElement -> CREATE_METHOD_NAME.equals(executableElement.getSimpleName().toString())) 62 | .collect(Collectors.toList()); 63 | 64 | List builderCreatonMethods = ElementFilter.methodsIn(builder.getEnclosedElements()) 65 | .stream() 66 | .filter(executableElement -> BUILDER_METHOD_NAME.equals(executableElement.getSimpleName().toString())) 67 | .collect(Collectors.toList()); 68 | 69 | return new BuilderInfo.Builder() 70 | .builderCreationMethod(builderCreatonMethods.get(0)) 71 | .buildMethod(builderMethods) 72 | .build(); 73 | } 74 | 75 | // from org.mapstruct.ap.spi.ImmutablesBuilderProvider 76 | TypeElement asBuilderElement(TypeElement typeElement) { 77 | Element enclosingElement = typeElement.getEnclosingElement(); 78 | StringBuilder builderQualifiedName = new StringBuilder(); 79 | if (enclosingElement.getKind() == ElementKind.PACKAGE) { 80 | builderQualifiedName.append(((PackageElement) enclosingElement).getQualifiedName().toString()); 81 | } else { 82 | builderQualifiedName.append(((TypeElement) enclosingElement).getQualifiedName().toString()); 83 | } 84 | 85 | if (builderQualifiedName.length() > 0) { 86 | builderQualifiedName.append("."); 87 | } 88 | 89 | builderQualifiedName.append(typeElement.getSimpleName()).append(BUILDER_CLASS_POSTFIX); 90 | return elementUtils.getTypeElement(builderQualifiedName); 91 | } 92 | 93 | private boolean isAnnotatedByKotlin(TypeElement typeElement) { 94 | return typeElement.getAnnotationMirrors() 95 | .stream() 96 | .anyMatch(annotationMirror -> KOTLIN_METADATA_CLASS_NAME.equals(annotationMirror.getAnnotationType().toString())); 97 | } 98 | 99 | private boolean isAnnotatedByKotlinBuilder(TypeElement typeElement) { 100 | return typeElement.getAnnotationMirrors() 101 | .stream() 102 | .anyMatch(annotationMirror -> KOTLIN_BUILDER_ANNOTATION_CLASS_NAME.equals(annotationMirror.getAnnotationType().toString())); 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/main/kotlin/com/github/pozo/BuilderGenerator.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo 2 | 3 | import com.squareup.javapoet.ClassName 4 | import com.squareup.javapoet.FieldSpec 5 | import com.squareup.javapoet.MethodSpec 6 | import com.squareup.javapoet.TypeName 7 | import com.squareup.javapoet.TypeSpec 8 | import kotlinx.metadata.jvm.KotlinClassHeader 9 | import javax.lang.model.element.Element 10 | import javax.lang.model.element.Modifier 11 | import javax.lang.model.element.TypeElement 12 | import javax.lang.model.element.VariableElement 13 | 14 | object BuilderGenerator { 15 | 16 | object Field { 17 | 18 | fun TypeSpec.Builder.addPrivateField(variableElement: VariableElement): FieldSpec { 19 | return FieldSpec.builder( 20 | variableElement.type(), 21 | variableElement.name(), 22 | Modifier.PRIVATE 23 | ).build().apply { 24 | this@addPrivateField.addField(this) 25 | } 26 | } 27 | } 28 | 29 | object Setter { 30 | 31 | fun TypeSpec.Builder.addSetterMethod( 32 | variableElement: VariableElement, packageName: String, builderClassName: String 33 | ): VariableElement { 34 | createSetterMethodSpec( 35 | variableElement.name(), 36 | packageName, 37 | builderClassName, 38 | variableElement.type() 39 | ).apply { 40 | this@addSetterMethod.addMethod(this) 41 | } 42 | return variableElement 43 | } 44 | 45 | private fun createSetterMethodSpec( 46 | fieldName: String, packageName: String, builderClassName: String, fieldType: TypeName 47 | ): MethodSpec { 48 | return MethodSpec.methodBuilder("set${fieldName.capitalize()}") 49 | .addModifiers(Modifier.PUBLIC) 50 | .returns(ClassName.get(packageName, builderClassName)) 51 | .addParameter(fieldType, fieldName) 52 | .addStatement("this.\$N = \$N", fieldName, fieldName) 53 | .addStatement("return this") 54 | .build() 55 | } 56 | } 57 | 58 | object Create { 59 | 60 | private const val METHOD_NAME = "create" 61 | 62 | fun TypeSpec.Builder.addCreateMethod(it: TypeElement, fields: List) { 63 | val guessedReturnValueType = ClassName.bestGuess("${it.qualifiedName}") 64 | val createMethod = createCreateMethodSpec(guessedReturnValueType, fields) 65 | this.addMethod(createMethod) 66 | } 67 | 68 | private fun createCreateMethodSpec(guessedReturnValueType: ClassName?, fields: List): MethodSpec { 69 | return MethodSpec.methodBuilder(METHOD_NAME) 70 | .addModifiers(Modifier.PUBLIC) 71 | .returns(guessedReturnValueType) 72 | .addStatement("return new \$T(\$L)", guessedReturnValueType, fields.joinToString { it.name }) 73 | .build() 74 | } 75 | } 76 | 77 | object Builder { 78 | 79 | private const val METHOD_NAME = "builder" 80 | 81 | fun TypeSpec.Builder.addBuilderMethod(packageName: String, builderClassName: String) { 82 | val builderMethod = createBuilderMethodSpec(packageName, builderClassName) 83 | this.addMethod(builderMethod) 84 | } 85 | 86 | private fun createBuilderMethodSpec(packageName: String, builderClassName: String): MethodSpec { 87 | return MethodSpec.methodBuilder(METHOD_NAME) 88 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC) 89 | .returns(ClassName.get(packageName, builderClassName)) 90 | .addStatement("return new \$T()", ClassName.get(packageName, builderClassName)) 91 | .build() 92 | } 93 | } 94 | 95 | // https://github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm 96 | // https://github.com/square/moshi/pull/570/files 97 | @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") 98 | fun Element.readHeader(): KotlinClassHeader { 99 | return getAnnotation(Metadata::class.java).run { 100 | KotlinClassHeader(kind, metadataVersion, bytecodeVersion, data1, data2, extraString, packageName, extraInt) 101 | } 102 | } 103 | 104 | fun VariableElement.name(): String = this.simpleName.toString() 105 | 106 | fun VariableElement.type(): TypeName = ClassName.get(this.asType()) 107 | } -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/main/kotlin/com/github/pozo/BuilderProcessor.kt: -------------------------------------------------------------------------------- 1 | package com.github.pozo 2 | 3 | import com.github.pozo.BuilderGenerator.Builder.addBuilderMethod 4 | import com.github.pozo.BuilderGenerator.Create.addCreateMethod 5 | import com.github.pozo.BuilderGenerator.Field.addPrivateField 6 | import com.github.pozo.BuilderGenerator.Setter.addSetterMethod 7 | import com.github.pozo.BuilderGenerator.readHeader 8 | import com.google.auto.service.AutoService 9 | import com.squareup.javapoet.JavaFile 10 | import com.squareup.javapoet.TypeSpec 11 | import com.squareup.javapoet.TypeVariableName 12 | import kotlinx.metadata.Flag 13 | import kotlinx.metadata.Flags 14 | import kotlinx.metadata.KmClassVisitor 15 | import kotlinx.metadata.jvm.KotlinClassMetadata 16 | import javax.annotation.processing.AbstractProcessor 17 | import javax.annotation.processing.Processor 18 | import javax.annotation.processing.RoundEnvironment 19 | import javax.lang.model.SourceVersion 20 | import javax.lang.model.element.Modifier 21 | import javax.lang.model.element.TypeElement 22 | import javax.lang.model.util.ElementFilter 23 | import kotlin.streams.toList 24 | 25 | 26 | @AutoService(Processor::class) 27 | class BuilderProcessor : AbstractProcessor() { 28 | 29 | override fun process(annotations: Set, roundEnv: RoundEnvironment): Boolean { 30 | val (dataClasses, nonDataClasses) = roundEnv.getElementsAnnotatedWith(KotlinBuilder::class.java) 31 | .filterIsInstance() 32 | .filter { isAnnotatedByKotlin(it) } 33 | .partition { isDataClass(it) } 34 | 35 | nonDataClasses.forEach { 36 | processingEnv.messager.printMessage( 37 | javax.tools.Diagnostic.Kind.ERROR, 38 | "@KotlinBuilder cannot be applied to non data classes", 39 | it 40 | ) 41 | } 42 | 43 | dataClasses.forEach { generateBuilder(it) } 44 | 45 | return true 46 | } 47 | 48 | private fun isAnnotatedByKotlin(it: TypeElement): Boolean { 49 | return it.annotationMirrors 50 | .stream() 51 | .filter { Metadata::class.java.canonicalName == it.annotationType.toString() } 52 | .count() > 0 53 | } 54 | 55 | private fun isDataClass(it: TypeElement): Boolean { 56 | val kotlinClassHeader = it.readHeader() 57 | val kotlinClassMetadata = KotlinClassMetadata.read(kotlinClassHeader) 58 | 59 | var isDataClass = false 60 | when (kotlinClassMetadata) { 61 | is KotlinClassMetadata.Class -> { 62 | kotlinClassMetadata.accept(object : KmClassVisitor() { 63 | override fun visit(flags: Flags, name: kotlinx.metadata.ClassName) { 64 | isDataClass = Flag.Class.IS_DATA(flags) 65 | } 66 | }) 67 | } 68 | } 69 | return isDataClass 70 | } 71 | 72 | private fun generateBuilder(typeElement: TypeElement) { 73 | val packageName = processingEnv.elementUtils.getPackageOf(typeElement).toString() 74 | val builderClassName = "${typeElement.simpleName}Builder" 75 | val classBuilder = TypeSpec.classBuilder(builderClassName) 76 | 77 | with(classBuilder) { 78 | this.addModifiers(Modifier.PUBLIC, Modifier.FINAL) 79 | this.addTypeVariables(typeElement.typeParameters.map { TypeVariableName.get(it) }) 80 | val constructors = ElementFilter.constructorsIn(typeElement.enclosedElements) 81 | 82 | constructors.stream() 83 | .flatMap { it.parameters.stream() } 84 | .peek { this.addSetterMethod(it, packageName, builderClassName) } 85 | .map { this.addPrivateField(it) } 86 | .toList() 87 | .apply { 88 | this@with.addCreateMethod(typeElement, this) 89 | this@with.addBuilderMethod(packageName, builderClassName) 90 | } 91 | this 92 | }.let { 93 | JavaFile.builder( 94 | packageName, 95 | it.build() 96 | ) 97 | }.apply { 98 | this.build().writeTo(processingEnv.filer) 99 | } 100 | } 101 | 102 | override fun getSupportedAnnotationTypes(): Set { 103 | return setOf(KotlinBuilder::class.java.canonicalName) 104 | } 105 | 106 | override fun getSupportedSourceVersion(): SourceVersion { 107 | return SourceVersion.RELEASE_8 108 | } 109 | 110 | override fun getSupportedOptions(): Set { 111 | return setOf("kapt.kotlin.generated") 112 | } 113 | } -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors: -------------------------------------------------------------------------------- 1 | com.github.pozo.BuilderProcessor,isolating 2 | 3 | -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/main/resources/META-INF/services/org.mapstruct.ap.spi.BuilderProvider: -------------------------------------------------------------------------------- 1 | com.github.pozo.KotlinBuilderProvider -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/test/java/com/github/pozo/KotlinBuilderProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.pozo; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import javax.lang.model.element.TypeElement; 7 | 8 | import static junit.framework.TestCase.assertTrue; 9 | import static org.junit.Assert.assertFalse; 10 | import static org.mockito.Mockito.spy; 11 | 12 | public class KotlinBuilderProviderTest extends KotlinBuilderTest { 13 | 14 | private KotlinBuilderProvider underTest; 15 | 16 | @Before 17 | public void init() { 18 | underTest = spy(KotlinBuilderProvider.class); 19 | } 20 | 21 | @Test 22 | public void isKotlinBuilder_When_There_Is_Just_Kotlin_Data_Class() { 23 | // GIVEN 24 | TypeElement typeElement = typeElement( 25 | annotationMirror( 26 | declaredType("kotlin.Metadata") 27 | ) 28 | ); 29 | // WHEN 30 | final boolean builderInfo = underTest.isKotlinBuilder(typeElement); 31 | 32 | // THEN 33 | assertFalse(builderInfo); 34 | } 35 | 36 | @Test 37 | public void isKotlinBuilder_When_There_Is_Just_Kotlin_Builder_Annotation() { 38 | // GIVEN 39 | TypeElement typeElement = typeElement( 40 | annotationMirror( 41 | declaredType("com.github.pozo.KotlinBuilder") 42 | ) 43 | ); 44 | 45 | // WHEN 46 | final boolean builderInfo = underTest.isKotlinBuilder(typeElement); 47 | 48 | // THEN 49 | assertFalse(builderInfo); 50 | } 51 | 52 | @Test 53 | public void isKotlinBuilderTest() { 54 | // GIVEN 55 | TypeElement typeElement = typeElement( 56 | annotationMirror( 57 | declaredType("kotlin.Metadata") 58 | ), 59 | annotationMirror( 60 | declaredType("com.github.pozo.KotlinBuilder") 61 | ) 62 | ); 63 | // WHEN 64 | final boolean builderInfo = underTest.isKotlinBuilder(typeElement); 65 | 66 | // THEN 67 | assertTrue(builderInfo); 68 | } 69 | } -------------------------------------------------------------------------------- /mapstruct-kotlin-processor/src/test/java/com/github/pozo/KotlinBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.pozo; 2 | 3 | import javax.lang.model.element.AnnotationMirror; 4 | import javax.lang.model.element.TypeElement; 5 | import javax.lang.model.type.DeclaredType; 6 | import java.util.Arrays; 7 | 8 | import static org.mockito.Mockito.doReturn; 9 | import static org.mockito.Mockito.mock; 10 | import static org.mockito.Mockito.when; 11 | 12 | class KotlinBuilderTest { 13 | 14 | TypeElement typeElement(AnnotationMirror... annotationMirror) { 15 | TypeElement typeElement = mock(TypeElement.class); 16 | doReturn(Arrays.asList(annotationMirror)).when(typeElement).getAnnotationMirrors(); 17 | return typeElement; 18 | } 19 | 20 | AnnotationMirror annotationMirror(DeclaredType declaredType) { 21 | AnnotationMirror kotlinBuilder = mock(AnnotationMirror.class); 22 | when(kotlinBuilder.getAnnotationType()).thenReturn(declaredType); 23 | return kotlinBuilder; 24 | } 25 | 26 | DeclaredType declaredType(String toString) { 27 | DeclaredType kotlinBuilderType = mock(DeclaredType.class); 28 | when(kotlinBuilderType.toString()).thenReturn(toString); 29 | return kotlinBuilderType; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'mapstruct-kotlin' 2 | include 'debug' 3 | include 'mapstruct-kotlin-builder' 4 | include 'mapstruct-kotlin-processor' 5 | --------------------------------------------------------------------------------