diff --git a/README.md b/README.md index d1c0cd9..be08c61 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,22 @@ Currently, the following examples exist: * _mapstruct-examples-field-mapping_: Shows how MapStruct can be used with "struct" like objects with public fields * _mapstruct-nested-bean-mappings_: Shows how to map object graphs via a main root method * _mapstruct-mapping-with-cycles_: Shows how to map object graphs that can contain cycles +* _mapstruct-on-android-with-autovalue-builders_: Shows how MapStruct works with builders for immutable objects in an Android sample project. * _mapstruct-spi-accessor-naming_: Example on how to use the Service Provider Interface (SPI) for a custom accessor naming strategy. * _mapstruct-protobuf3_: Example on how to use protobuf3 with MapStruct * _mapstruct-kotlin_: Example on how to use MapStruct with Kotlin using KAPT (Kotlin Annotation Processing Tool) * _mapstruct-kotlin-gradle_: Example on how to use MapStruct with Kotlin and Gradle Kotlin DSL using KAPT -* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA) +* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA * _mapstruct-suppress-unmapped_: Shows how mapping to target properties can be ignored without warning by default in a mixed scenario. However bean property mappings that have the same name will still be applied. * _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method. * _mapstruct-clone_: Shows how an object can be deeply cloned by defining all mapping methods. * _mapstruct-metadata-annotations_: Demonstrates how to read annotations and use them as mapping instruction. +# MapStruct Integration tests + +The integration tests are also a valuable resource for inspecting how MapStruct works. Here is the entire collection [integration test resources](https://github.com/mapstruct/mapstruct/tree/master/integrationtest/src/test/resources) +and here is an example for [AutoValue](https://github.com/mapstruct/mapstruct/tree/master/integrationtest/src/test/resources/autoValueBuilderTest/src/main/java/org/mapstruct/itest/auto/value) as one of those integrations tests. + ## License The examples in this project are licensed under the Apache License, Version 2.0. diff --git a/mapstruct-on-android-with-autovalue-builders/.gitignore b/mapstruct-on-android-with-autovalue-builders/.gitignore new file mode 100644 index 0000000..738896a --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/.gitignore @@ -0,0 +1,42 @@ +# Built application files +*.apk +*.ap_ + +# Files for the Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ IDEA module file +*.iml + +# Intellij IDEA project settings folder +#.idea/workspace.xml +#.idea/tasks.xml +#.idea/datasources.xml +#.idea/dataSources.ids +.idea/ \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/README.md b/mapstruct-on-android-with-autovalue-builders/README.md new file mode 100644 index 0000000..65082bf --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/README.md @@ -0,0 +1,13 @@ +# mapstruct-test + +This repository contains a sample project using [MapStruct](http://mapstruct.org/) on Android. + +This sample project originates from [Albert Beade's MapStruct test at GitHub](https://github.com/abeade/mapstruct-test) + +# How to run it + +Open this containing folder with its android gradle build files directly in Android Studio. + +## License + +* [Apache Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) diff --git a/mapstruct-on-android-with-autovalue-builders/app/.gitignore b/mapstruct-on-android-with-autovalue-builders/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/mapstruct-on-android-with-autovalue-builders/app/build.gradle b/mapstruct-on-android-with-autovalue-builders/app/build.gradle new file mode 100644 index 0000000..af50e7f --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/build.gradle @@ -0,0 +1,45 @@ +plugins { + // https://plugins.gradle.org/plugin/net.ltgt.apt + id 'net.ltgt.apt' version '0.21' // mapstruct +} +// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides +apply plugin: 'net.ltgt.apt-idea' // mapstruct +//apply plugin: 'net.ltgt.apt-eclipse' // Not needed with android studio + +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + + defaultConfig { + applicationId "com.example.mapstructtest" + minSdkVersion 21 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + } + compileOptions { + // Just here to demonstrate it compiles with this. Java 8 allows use of repeated annotations, + // See AutoValueMapper.java for more comments on this + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation 'com.android.support:appcompat-v7:28.0.0' // Just for this example + implementation 'org.mapstruct:mapstruct:1.3.0.Final' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final' + // If mappers generated in test code +// testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final' + + // For tests of mapstruct with AutoValue + compileOnly "com.google.auto.value:auto-value:1.5.2" + annotationProcessor "com.google.auto.value:auto-value:1.5.2" +} diff --git a/mapstruct-on-android-with-autovalue-builders/app/proguard-rules.pro b/mapstruct-on-android-with-autovalue-builders/app/proguard-rules.pro new file mode 100644 index 0000000..3e1419f --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\Alberto\Documents\Development\Android\sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/AndroidManifest.xml b/mapstruct-on-android-with-autovalue-builders/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..bca7425 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/MainActivity.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/MainActivity.java new file mode 100644 index 0000000..8650a05 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/MainActivity.java @@ -0,0 +1,101 @@ +package com.example.mapstructtest; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.widget.TextView; + +import com.example.mapstructtest.model.AutoValueCar; +import com.example.mapstructtest.model.AutoValueCarDto; +import com.example.mapstructtest.model.AutoValueFluentCar; +import com.example.mapstructtest.model.AutoValueFluentCarDto; +import com.example.mapstructtest.model.Car; +import com.example.mapstructtest.model.CarDto; +import com.example.mapstructtest.model.CarFluentSetter; +import com.example.mapstructtest.model.CarType; +import com.example.mapstructtest.model.ImmutableConstructableCar; +import com.example.mapstructtest.model.ImmutableConstructibleCarDto; +import com.example.mapstructtest.model.mapping.AutoValueFluentMapper; +import com.example.mapstructtest.model.mapping.AutoValueMapper; +import com.example.mapstructtest.model.mapping.CarMapper; +import com.example.mapstructtest.model.mapping.ImmutableConstructableCarMapper; + +public class MainActivity extends AppCompatActivity { + + private TextView tv; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + tv = findViewById(R.id.tv_text); + } + + @Override + protected void onResume() { + super.onResume(); + StringBuilder sb = new StringBuilder(); + sb.append(manualMappingWithFluentSetter()); + sb.append(withAutoValue()); + sb.append(withAutoValueFluent()); + sb.append(withAutoValueFluent()); +// sb.append(withImmutable()); // Not yet for MapStruct 1.3 + tv.setText(sb.toString()); + } + + String withImmutable() { + // MapStruct 1.3 does not support this immutable style contstructor. + ImmutableConstructableCar immutableConstructableCar = new ImmutableConstructableCar("Immutable Audi", 6, CarType.LARGE); + ImmutableConstructibleCarDto immutableCarDto = ImmutableConstructableCarMapper.INSTANCE.carToCarDto(immutableConstructableCar); + return getString("Immutable: ", immutableConstructableCar, immutableCarDto); + } + + String manualMappingWithFluentSetter() { + Car car = new Car(); + car.setConstructor("Audi"); + car.setNumberOfSeats(5); + car.setType(CarType.LUXURY); + // Map the regular car + CarDto carDto = CarMapper.INSTANCE.carToCarDto(car); + // Then map it via fluent setter version + CarFluentSetter carFluentSetter = CarMapper.INSTANCE.toFluentSetterCar(carDto); + CarDto carDtoAgain = CarMapper.INSTANCE.toDto(carFluentSetter); + return getString("Car and car with fluent setter", car, carDto, carFluentSetter, carDtoAgain); + } + + String withAutoValue() { + AutoValueCar.Builder builder = AutoValueCar.builder(); + builder.setConstructor("Audi"); + builder.setNumberOfSeats(5); + builder.setNumberOfAirbags(3); + builder.setType(CarType.LUXURY); + AutoValueCar car = builder.build(); + + AutoValueCarDto carDto = AutoValueMapper.INSTANCE.toDto(car); + AutoValueCar carAgain = AutoValueMapper.INSTANCE.toModel(carDto); + return getString("AutoValue mapping with MapStruct", car, carDto, carAgain); + } + + String withAutoValueFluent() { + AutoValueFluentCar.Builder builder = AutoValueFluentCar.builder(); + builder.constructor("Audi"); + builder.numberOfSeats(5); + builder.type(CarType.LUXURY); + AutoValueFluentCar car = builder.build(); + // MapStruct 1.3 does NOT work with fluent accessor style for getters, but does work with fluent setters + AutoValueFluentCarDto carDto = AutoValueFluentMapper.INSTANCE.toDto(car); + AutoValueFluentCar carAgain = AutoValueFluentMapper.INSTANCE.toModel(carDto); + return getString("AutoValue with fluent setter mapping", car, carDto, carAgain); + } + + private String getString(String info, Object... objs) { + StringBuilder strb = new StringBuilder(); + strb.append(info); + for (Object obj : objs) { + strb.append('\n'); + strb.append(obj.toString()); + } + strb.append('\n'); + strb.append('\n'); + return strb.toString(); + } +} diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCar.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCar.java new file mode 100644 index 0000000..6241449 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCar.java @@ -0,0 +1,24 @@ +package com.example.mapstructtest.model; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueCar { + public abstract String getConstructor(); + public abstract int getNumberOfSeats(); + public abstract int getNumberOfAirbags(); + public abstract CarType getType(); + + public static Builder builder() { + return new AutoValue_AutoValueCar.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setConstructor(String value); + public abstract Builder setNumberOfSeats(int value); + public abstract Builder setNumberOfAirbags(int value); + public abstract Builder setType(CarType value); + public abstract AutoValueCar build(); + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCarDto.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCarDto.java new file mode 100644 index 0000000..7b178d7 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueCarDto.java @@ -0,0 +1,24 @@ +package com.example.mapstructtest.model; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueCarDto { + public abstract String getConstructor(); + public abstract int getSeatCount(); + public abstract int getAirbagCount(); + public abstract CarType getType(); + + public static Builder builder() { + return new AutoValue_AutoValueCarDto.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setConstructor(String value); + public abstract Builder setSeatCount(int value); + public abstract Builder setAirbagCount(int value); + public abstract Builder setType(CarType value); + public abstract AutoValueCarDto build(); + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCar.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCar.java new file mode 100644 index 0000000..1a75008 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCar.java @@ -0,0 +1,32 @@ +package com.example.mapstructtest.model; + +import android.support.annotation.Nullable; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueFluentCar { + // MapStruct 1.3 does NOT work with fluent accessor style getters, hence the old-fashioned getX + @Nullable + public abstract String getConstructor(); + + public abstract int getNumberOfSeats(); + + @Nullable + public abstract CarType getType(); + + public static Builder builder() { + return new AutoValue_AutoValueFluentCar.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder constructor(String value); + + public abstract Builder numberOfSeats(int value); + + public abstract Builder type(CarType value); + + public abstract AutoValueFluentCar build(); + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCarDto.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCarDto.java new file mode 100644 index 0000000..420c245 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/AutoValueFluentCarDto.java @@ -0,0 +1,32 @@ +package com.example.mapstructtest.model; + +import android.support.annotation.Nullable; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueFluentCarDto { + // MapStruct 1.3 does NOT work with fluent accessor style getters, hence the old-fashioned getX + @Nullable + public abstract String getConstructor(); + + public abstract int getSeatCount(); + + @Nullable + public abstract CarType getType(); + + public static Builder builder() { + return new AutoValue_AutoValueFluentCarDto.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder constructor(String value); + + public abstract Builder seatCount(int value); + + public abstract Builder type(CarType value); + + public abstract AutoValueFluentCarDto build(); + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/Car.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/Car.java new file mode 100644 index 0000000..cb642b0 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/Car.java @@ -0,0 +1,40 @@ +package com.example.mapstructtest.model; + +public class Car { + private String constructor; + private int numberOfSeats; + private CarType type; + + public String getConstructor() { + return constructor; + } + + public void setConstructor(String constructor) { + this.constructor = constructor; + } + + public int getNumberOfSeats() { + return numberOfSeats; + } + + public void setNumberOfSeats(int numberOfSeats) { + this.numberOfSeats = numberOfSeats; + } + + public CarType getType() { + return type; + } + + public void setType(CarType type) { + this.type = type; + } + + @Override + public String toString() { + return "Car{" + + "'" + constructor + '\'' + + ", " + numberOfSeats + + ", " + type + + '}'; + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarDto.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarDto.java new file mode 100644 index 0000000..6ed4143 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarDto.java @@ -0,0 +1,40 @@ +package com.example.mapstructtest.model; + +public class CarDto { + private String constructor; + private int seatCount; + private String type; + + public String getConstructor() { + return constructor; + } + + public void setConstructor(String constructor) { + this.constructor = constructor; + } + + public int getSeatCount() { + return seatCount; + } + + public void setSeatCount(int seatCount) { + this.seatCount = seatCount; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "CarDto{" + + "'" + constructor + '\'' + + ", " + seatCount + + ", '" + type + '\'' + + '}'; + } +} diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarFluentSetter.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarFluentSetter.java new file mode 100644 index 0000000..25b032e --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarFluentSetter.java @@ -0,0 +1,43 @@ +package com.example.mapstructtest.model; + +public class CarFluentSetter { + private String constructor; + private int numberOfSeats; + private CarType type; + + public String getConstructor() { + return constructor; + } + + public CarFluentSetter constructor(String constructor) { + this.constructor = constructor; + return this; + } + + public int getNumberOfSeats() { + return numberOfSeats; + } + + public CarFluentSetter numberOfSeats(int numberOfSeats) { + this.numberOfSeats = numberOfSeats; + return this; + } + + public CarType getType() { + return type; + } + + public CarFluentSetter type(CarType type) { + this.type = type; + return this; + } + + @Override + public String toString() { + return "CarFluentSetter{" + + "'" + constructor + '\'' + + ", " + numberOfSeats + + ", " + type + + '}'; + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarType.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarType.java new file mode 100644 index 0000000..ae76ed2 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/CarType.java @@ -0,0 +1,5 @@ +package com.example.mapstructtest.model; + +public enum CarType { + SPORTS, LUXURY, LARGE, MID_SIZE, SMALL +} diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructableCar.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructableCar.java new file mode 100644 index 0000000..da3b9e8 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructableCar.java @@ -0,0 +1,35 @@ +package com.example.mapstructtest.model; + +public class ImmutableConstructableCar { + private final String constructor; + private final int numberOfSeats; + private final CarType type; + + // MapStruct 1.3 does not support this immutable style contstructor. + public ImmutableConstructableCar(String constructor, int numberOfSeats, CarType type) { + this.constructor = constructor; + this.numberOfSeats = numberOfSeats; + this.type = type; + } + + public String getConstructor() { + return constructor; + } + + public int getNumberOfSeats() { + return numberOfSeats; + } + + public CarType getType() { + return type; + } + + @Override + public String toString() { + return "Car{" + + "constructor='" + constructor + '\'' + + ", numberOfSeats=" + numberOfSeats + + ", type=" + type + + '}'; + } +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructibleCarDto.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructibleCarDto.java new file mode 100644 index 0000000..205a476 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/ImmutableConstructibleCarDto.java @@ -0,0 +1,35 @@ +package com.example.mapstructtest.model; + +public class ImmutableConstructibleCarDto { + private final String constructor; + private final int seatCount; + private final String type; + + // MapStruct 1.3 does not support this immutable style contstructor. + public ImmutableConstructibleCarDto(String constructor, int seatCount, String type) { + this.constructor = constructor; + this.seatCount = seatCount; + this.type = type; + } + + public String getConstructor() { + return constructor; + } + + public int getSeatCount() { + return seatCount; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + return "CarDto{" + + "constructor='" + constructor + '\'' + + ", seatCount=" + seatCount + + ", type='" + type + '\'' + + '}'; + } +} diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueFluentMapper.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueFluentMapper.java new file mode 100644 index 0000000..54e15f9 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueFluentMapper.java @@ -0,0 +1,20 @@ +package com.example.mapstructtest.model.mapping; + +import com.example.mapstructtest.model.AutoValueFluentCar; +import com.example.mapstructtest.model.AutoValueFluentCarDto; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface AutoValueFluentMapper { + AutoValueFluentMapper INSTANCE = Mappers.getMapper(AutoValueFluentMapper.class); + + // MapStruct 1.3 does NOT work with fluent accessor style like shown here + @Mapping(source = "numberOfSeats", target = "seatCount") + AutoValueFluentCarDto toDto(AutoValueFluentCar car); + @InheritInverseConfiguration + AutoValueFluentCar toModel(AutoValueFluentCarDto car); +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueMapper.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueMapper.java new file mode 100644 index 0000000..6bb9f5a --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueMapper.java @@ -0,0 +1,27 @@ +package com.example.mapstructtest.model.mapping; + +import com.example.mapstructtest.model.AutoValueCar; +import com.example.mapstructtest.model.AutoValueCarDto; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface AutoValueMapper { + AutoValueMapper INSTANCE = Mappers.getMapper(AutoValueMapper.class); + + // Java 8 added repeated annotations. If not running java 8, + // wrap in @Mappings annotation as this out-commented example +// @Mappings({ +// @Mapping(source = "numberOfSeats", target = "seatCount"), +// @Mapping(source = "numberOfAirbags", target = "airbagCount") +// }) + @Mapping(source = "numberOfSeats", target = "seatCount") + @Mapping(source = "numberOfAirbags", target = "airbagCount") + AutoValueCarDto toDto(AutoValueCar car); + + @InheritInverseConfiguration + AutoValueCar toModel(AutoValueCarDto car); +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/CarMapper.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/CarMapper.java new file mode 100644 index 0000000..cdd3849 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/CarMapper.java @@ -0,0 +1,24 @@ +package com.example.mapstructtest.model.mapping; + +import com.example.mapstructtest.model.Car; +import com.example.mapstructtest.model.CarDto; +import com.example.mapstructtest.model.CarFluentSetter; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface CarMapper { + CarMapper INSTANCE = Mappers.getMapper(CarMapper.class); + + @Mapping(source = "numberOfSeats", target = "seatCount") + CarDto carToCarDto(Car car); + + @Mapping(source = "numberOfSeats", target = "seatCount") + CarDto toDto(CarFluentSetter car); + + @InheritInverseConfiguration + CarFluentSetter toFluentSetterCar(CarDto car); +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/ImmutableConstructableCarMapper.java b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/ImmutableConstructableCarMapper.java new file mode 100644 index 0000000..d6cdb37 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/java/com/example/mapstructtest/model/mapping/ImmutableConstructableCarMapper.java @@ -0,0 +1,16 @@ +package com.example.mapstructtest.model.mapping; + +import com.example.mapstructtest.model.ImmutableConstructableCar; +import com.example.mapstructtest.model.ImmutableConstructibleCarDto; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +//@Mapper +public interface ImmutableConstructableCarMapper { + ImmutableConstructableCarMapper INSTANCE = Mappers.getMapper(ImmutableConstructableCarMapper.class); + + // MapStruct 1.3 does not support this immutable style contstructor. +// @Mapping(source = "numberOfSeats", target = "seatCount") + ImmutableConstructibleCarDto carToCarDto(ImmutableConstructableCar car); +} \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/app/src/main/res/layout/activity_main.xml b/mapstruct-on-android-with-autovalue-builders/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..0016b52 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,14 @@ + + + + + diff --git a/mapstruct-on-android-with-autovalue-builders/build.gradle b/mapstruct-on-android-with-autovalue-builders/build.gradle new file mode 100644 index 0000000..4eb4168 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/build.gradle @@ -0,0 +1,22 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + google() // Traverse in browser with: https://dl.google.com/dl/android/maven2/index.html + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.3.2' + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/mapstruct-on-android-with-autovalue-builders/gradle.properties b/mapstruct-on-android-with-autovalue-builders/gradle.properties new file mode 100644 index 0000000..1d3591c --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/gradle.properties @@ -0,0 +1,18 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true \ No newline at end of file diff --git a/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.jar b/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..13372ae Binary files /dev/null and b/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.jar differ diff --git a/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.properties b/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..1b2b07c --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/mapstruct-on-android-with-autovalue-builders/gradlew b/mapstruct-on-android-with-autovalue-builders/gradlew new file mode 100644 index 0000000..9d82f78 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/mapstruct-on-android-with-autovalue-builders/gradlew.bat b/mapstruct-on-android-with-autovalue-builders/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/mapstruct-on-android-with-autovalue-builders/settings.gradle b/mapstruct-on-android-with-autovalue-builders/settings.gradle new file mode 100644 index 0000000..e7b4def --- /dev/null +++ b/mapstruct-on-android-with-autovalue-builders/settings.gradle @@ -0,0 +1 @@ +include ':app'