Skip to content

Commit 823b5ed

Browse files
committed
mapstruct#2213: primitive arrays should be directly mapped (we are cloning them anyways)
Additionally fix problem when annotations `ElementType.TYPE_USE` not handled correctly for javac
1 parent 233fc6d commit 823b5ed

8 files changed

Lines changed: 194 additions & 2 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ else if (componentTypeMirror.getKind().isPrimitive()) {
274274
else {
275275
isEnumType = false;
276276
isInterface = false;
277-
name = mirror.toString();
277+
// When the component type is primitive and is annotated with ElementType.TYPE_USE then
278+
// the typeMirror#toString returns (@CustomAnnotation :: byte) for the javac compiler
279+
name = mirror.getKind().isPrimitive() ? NativeTypes.getName( mirror.getKind() ) : mirror.toString();
278280
packageName = null;
279281
qualifiedName = name;
280282
typeElement = null;

processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private boolean allowDirect(Type type) {
367367
}
368368

369369
if ( type.isArrayType() ) {
370-
return type.isJavaLangType();
370+
return type.isJavaLangType() || type.getComponentType().isPrimitive();
371371
}
372372

373373
if ( type.isIterableOrStreamType() ) {
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.ap.test.bugs._2213;
7+
8+
public class Car {
9+
private int[] intData;
10+
private Long[] longData;
11+
12+
public int[] getIntData() {
13+
return intData;
14+
}
15+
16+
public void setIntData(int[] intData) {
17+
this.intData = intData;
18+
}
19+
20+
public Long[] getLongData() {
21+
return longData;
22+
}
23+
24+
public void setLongData(Long[] longData) {
25+
this.longData = longData;
26+
}
27+
28+
}
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.ap.test.bugs._2213;
7+
8+
public class Car2 {
9+
private int[] intData;
10+
private Long[] longData;
11+
12+
public @NotNull int[] getIntData() {
13+
return intData;
14+
}
15+
16+
public void setIntData(int[] intData) {
17+
this.intData = intData;
18+
}
19+
20+
public @NotNull Long[] getLongData() {
21+
return longData;
22+
}
23+
24+
public void setLongData(Long[] longData) {
25+
this.longData = longData;
26+
}
27+
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.ap.test.bugs._2213;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.control.DeepClone;
10+
import org.mapstruct.factory.Mappers;
11+
12+
/**
13+
* @author Filip Hrisafov
14+
*/
15+
@Mapper(mappingControl = DeepClone.class)
16+
public interface CarMapper {
17+
18+
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
19+
20+
Car toCar(Car2 car2);
21+
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.ap.test.bugs._2213;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mapstruct.ap.testutil.IssueKey;
12+
import org.mapstruct.ap.testutil.WithClasses;
13+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
14+
import org.mapstruct.ap.testutil.runner.GeneratedSource;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
/**
19+
* @author Filip Hrisafov
20+
*/
21+
@WithClasses({
22+
NotNull.class,
23+
CarMapper.class,
24+
Car.class,
25+
Car2.class
26+
})
27+
@RunWith(AnnotationProcessorTestRunner.class)
28+
@IssueKey("2213")
29+
public class Issue2213Test {
30+
31+
@Rule
32+
public final GeneratedSource generatedSource = new GeneratedSource()
33+
.addComparisonToFixtureFor( CarMapper.class );
34+
35+
@Test
36+
public void testShouldNotGenerateIntermediatePrimitiveMappingMethod() {
37+
Car2 car = new Car2();
38+
int[] sourceInt = { 1, 2, 3 };
39+
car.setIntData( sourceInt );
40+
Long[] sourceLong = { 1L, 2L, 3L };
41+
car.setLongData( sourceLong );
42+
Car target = CarMapper.INSTANCE.toCar( car );
43+
44+
assertThat( target ).isNotNull();
45+
assertThat( target.getIntData() )
46+
.containsExactly( 1, 2, 3 )
47+
.isNotSameAs( sourceInt );
48+
assertThat( target.getLongData() )
49+
.containsExactly( 1L, 2L, 3L )
50+
.isNotSameAs( sourceLong );
51+
}
52+
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.ap.test.bugs._2213;
7+
8+
import java.lang.annotation.Documented;
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
@Target({
15+
ElementType.METHOD,
16+
ElementType.TYPE_USE
17+
})
18+
@Retention(RetentionPolicy.RUNTIME)
19+
@Documented
20+
public @interface NotNull {
21+
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.ap.test.bugs._2213;
7+
8+
import java.util.Arrays;
9+
import javax.annotation.Generated;
10+
11+
@Generated(
12+
value = "org.mapstruct.ap.MappingProcessor",
13+
date = "2020-10-03T10:12:15+0200",
14+
comments = "version: , compiler: javac, environment: Java 11.0.4 (AdoptOpenJDK)"
15+
)
16+
public class CarMapperImpl implements CarMapper {
17+
18+
@Override
19+
public Car toCar(Car2 car2) {
20+
if ( car2 == null ) {
21+
return null;
22+
}
23+
24+
Car car = new Car();
25+
26+
int[] intData = car2.getIntData();
27+
if ( intData != null ) {
28+
car.setIntData( Arrays.copyOf( intData, intData.length ) );
29+
}
30+
Long[] longData = car2.getLongData();
31+
if ( longData != null ) {
32+
car.setLongData( Arrays.copyOf( longData, longData.length ) );
33+
}
34+
35+
return car;
36+
}
37+
}

0 commit comments

Comments
 (0)