Skip to content

Commit 853ff7f

Browse files
committed
#2060: MapStruct should work properly on the module path
1 parent 4f76208 commit 853ff7f

8 files changed

Lines changed: 168 additions & 4 deletions

File tree

integrationtest/src/test/java/org/mapstruct/itest/tests/MavenIntegrationTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,10 @@ void superTypeGenerationTest() {
130130
void targetTypeGenerationTest() {
131131
}
132132

133+
@ProcessorTest(baseDir = "moduleInfoTest")
134+
@EnabledForJreRange(min = JRE.JAVA_11)
135+
void moduleInfoTest() {
136+
137+
}
138+
133139
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright MapStruct Authors.
5+
6+
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
7+
8+
-->
9+
<project xmlns="http://maven.apache.org/POM/4.0.0"
10+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<parent>
15+
<groupId>org.mapstruct</groupId>
16+
<artifactId>mapstruct-it-parent</artifactId>
17+
<version>1.0.0</version>
18+
<relativePath>../pom.xml</relativePath>
19+
</parent>
20+
21+
<artifactId>modulesTest</artifactId>
22+
<packaging>jar</packaging>
23+
24+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
module test.mapstruct {
7+
requires org.mapstruct;
8+
exports org.mapstruct.itest.modules;
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.itest.modules;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class CustomerDto {
12+
13+
private String name;
14+
private String email;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getEmail() {
25+
return email;
26+
}
27+
28+
public void setEmail(String email) {
29+
this.email = email;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.itest.modules;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class CustomerEntity {
12+
13+
private String name;
14+
private String mail;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getMail() {
25+
return mail;
26+
}
27+
28+
public void setMail(String mail) {
29+
this.mail = mail;
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.itest.modules;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.factory.Mappers;
11+
12+
/**
13+
* @author Filip Hrisafov
14+
*/
15+
@Mapper
16+
public interface CustomerMapper {
17+
18+
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
19+
20+
@Mapping(target = "mail", source = "email")
21+
CustomerEntity fromDto(CustomerDto record);
22+
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.itest.modules;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import org.junit.Test;
11+
import org.mapstruct.itest.modules.CustomerDto;
12+
import org.mapstruct.itest.modules.CustomerEntity;
13+
import org.mapstruct.itest.modules.CustomerMapper;
14+
15+
public class ModulesTest {
16+
17+
@Test
18+
public void shouldMapRecord() {
19+
CustomerDto dto = new CustomerDto();
20+
dto.setName( "Kermit" );
21+
dto.setEmail( "kermit@test.com" );
22+
CustomerEntity customer = CustomerMapper.INSTANCE.fromDto( dto );
23+
24+
assertThat( customer ).isNotNull();
25+
assertThat( customer.getName() ).isEqualTo( "Kermit" );
26+
assertThat( customer.getMail() ).isEqualTo( "kermit@test.com" );
27+
}
28+
}

processor/src/main/java/org/mapstruct/ap/internal/conversion/Conversions.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ public Conversions(TypeFactory typeFactory) {
184184
register( Enum.class, String.class, new EnumStringConversion() );
185185
register( Date.class, String.class, new DateToStringConversion() );
186186
register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() );
187-
register( Date.class, Time.class, new DateToSqlTimeConversion() );
188-
register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() );
189-
register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() );
187+
188+
registerJavaTimeSqlConversions();
190189

191190
// java.util.Currency <~> String
192191
register( Currency.class, String.class, new CurrencyToStringConversion() );
@@ -227,15 +226,28 @@ private void registerJava8TimeConversions() {
227226
register( ZonedDateTime.class, Date.class, new JavaZonedDateTimeToDateConversion() );
228227
register( LocalDateTime.class, Date.class, new JavaLocalDateTimeToDateConversion() );
229228
register( LocalDate.class, Date.class, new JavaLocalDateToDateConversion() );
230-
register( LocalDate.class, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
231229
register( Instant.class, Date.class, new JavaInstantToDateConversion() );
232230

233231
}
234232

233+
private void registerJavaTimeSqlConversions() {
234+
if ( isJavaSqlAvailable() ) {
235+
register( LocalDate.class, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
236+
237+
register( Date.class, Time.class, new DateToSqlTimeConversion() );
238+
register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() );
239+
register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() );
240+
}
241+
}
242+
235243
private boolean isJodaTimeAvailable() {
236244
return typeFactory.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
237245
}
238246

247+
private boolean isJavaSqlAvailable() {
248+
return typeFactory.isTypeAvailable( "java.sql.Date" );
249+
}
250+
239251
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
240252
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
241253
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );

0 commit comments

Comments
 (0)