Skip to content

Commit 6e54534

Browse files
committed
mapstruct#1045 Supporting mappers with generated source/target types by deferring their generation to a later round
1 parent 5e59f34 commit 6e54534

10 files changed

Lines changed: 370 additions & 5 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.tests;
20+
21+
import org.junit.runner.RunWith;
22+
import org.mapstruct.itest.testutil.runner.ProcessorSuite;
23+
import org.mapstruct.itest.testutil.runner.ProcessorSuite.ProcessorType;
24+
import org.mapstruct.itest.testutil.runner.ProcessorSuiteRunner;
25+
26+
/**
27+
* Tests usage of MapStruct with another processor that generates the target type of a mapping method.
28+
*
29+
* @author Gunnar Morling
30+
*/
31+
@RunWith( ProcessorSuiteRunner.class )
32+
@ProcessorSuite(baseDir = "targetTypeGenerationTest", processorTypes = ProcessorType.ORACLE_JAVA_8)
33+
public class TargetTypeGenerationTest {
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.mapstruct.itest</groupId>
7+
<artifactId>itest-targettypegeneration-aggregator</artifactId>
8+
<version>1.0.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>itest-targettypegeneration-generator</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-compiler-plugin</artifactId>
28+
<version>3.1</version>
29+
<configuration>
30+
<source>1.6</source>
31+
<target>1.6</target>
32+
<compilerArgs>
33+
<compilerArg>-proc:none</compilerArg>
34+
</compilerArgs>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.targettypegeneration;
20+
21+
import java.io.IOException;
22+
import java.io.Writer;
23+
import java.util.Set;
24+
25+
import javax.annotation.processing.AbstractProcessor;
26+
import javax.annotation.processing.RoundEnvironment;
27+
import javax.annotation.processing.SupportedAnnotationTypes;
28+
import javax.lang.model.element.AnnotationMirror;
29+
import javax.lang.model.element.Element;
30+
import javax.lang.model.element.Name;
31+
import javax.lang.model.element.PackageElement;
32+
import javax.lang.model.element.TypeElement;
33+
import javax.tools.JavaFileObject;
34+
35+
/**
36+
* Generates a DTO.
37+
*
38+
* @author Gunnar Morling
39+
*
40+
*/
41+
@SupportedAnnotationTypes("*")
42+
public class DtoGenerationProcessor extends AbstractProcessor {
43+
44+
private boolean hasRun = false;
45+
46+
@Override
47+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
48+
if ( !hasRun ) {
49+
try {
50+
JavaFileObject dto = processingEnv.getFiler().createSourceFile( "org.mapstruct.itest.targettypegeneration.usage.OrderDto" );
51+
Writer writer = dto.openWriter();
52+
53+
writer.append( "package org.mapstruct.itest.targettypegeneration.usage;" );
54+
writer.append( "\n" );
55+
writer.append( "public class OrderDto {" );
56+
writer.append( "\n" );
57+
writer.append( " private String item;" );
58+
writer.append( "\n" );
59+
writer.append( " public String getItem() {" );
60+
writer.append( " return item;" );
61+
writer.append( " }" );
62+
writer.append( "\n" );
63+
writer.append( " public void setItem(String item) {" );
64+
writer.append( " this.item = item;" );
65+
writer.append( " }" );
66+
writer.append( "}" );
67+
68+
writer.flush();
69+
writer.close();
70+
}
71+
catch (IOException e) {
72+
throw new RuntimeException( e );
73+
}
74+
75+
hasRun = true;
76+
}
77+
78+
return false;
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
2+
# and/or other contributors as indicated by the @authors tag. See the
3+
# copyright.txt file in the distribution for a full listing of all
4+
# contributors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
org.mapstruct.itest.targettypegeneration.DtoGenerationProcessor
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.mapstruct</groupId>
27+
<artifactId>mapstruct-it-parent</artifactId>
28+
<version>1.0.0</version>
29+
<relativePath>../pom.xml</relativePath>
30+
</parent>
31+
32+
<groupId>org.mapstruct.itest</groupId>
33+
<artifactId>itest-targettypegeneration-aggregator</artifactId>
34+
<packaging>pom</packaging>
35+
36+
<modules>
37+
<module>generator</module>
38+
<module>usage</module>
39+
</modules>
40+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.mapstruct.itest</groupId>
7+
<artifactId>itest-targettypegeneration-aggregator</artifactId>
8+
<version>1.0.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>itest-targettypegeneration-usage</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>4.12</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.mapstruct.itest</groupId>
24+
<artifactId>itest-targettypegeneration-generator</artifactId>
25+
<version>1.0.0</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.1</version>
36+
<configuration>
37+
<source>1.6</source>
38+
<target>1.6</target>
39+
<compilerArgs>
40+
<compilerArg>-XprintProcessorInfo</compilerArg>
41+
<compilerArg>-XprintRounds</compilerArg>
42+
</compilerArgs>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.targettypegeneration.usage;
20+
21+
/**
22+
* @author Gunnar Morling
23+
*/
24+
public class Order {
25+
26+
private String item;
27+
28+
public String getItem() {
29+
return item;
30+
}
31+
32+
public void setItem(String item) {
33+
this.item = item;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.targettypegeneration.usage;
20+
21+
import org.mapstruct.Mapper;
22+
import org.mapstruct.factory.Mappers;
23+
24+
/**
25+
* @author Gunnar Morling
26+
*/
27+
@Mapper
28+
public abstract class OrderMapper {
29+
30+
public static final OrderMapper INSTANCE = Mappers.getMapper( OrderMapper.class );
31+
32+
public abstract OrderDto orderToDto(Order order);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.itest.targettypegeneration.usage;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
import org.junit.Test;
24+
25+
/**
26+
* Integration test for using MapStruct with another annotation processor that generates the target type of a mapping
27+
* method.
28+
*
29+
* @author Gunnar Morling
30+
*/
31+
public class GeneratedTargetTypeTest {
32+
33+
@Test
34+
public void considersPropertiesOnGeneratedSourceAndTargetTypes() {
35+
Order order = new Order();
36+
order.setItem( "my item" );
37+
38+
OrderDto dto = OrderMapper.INSTANCE.orderToDto( order );
39+
assertEquals( order.getItem(), dto.getItem() );
40+
}
41+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ public Type getType(TypeElement typeElement) {
147147
}
148148

149149
public Type getType(TypeMirror mirror) {
150-
151-
if ( mirror.getKind() == TypeKind.ERROR ) {
152-
throw new AnnotationProcessingException( "Encountered erroneous type " + mirror );
153-
}
154-
155150
if ( !canBeProcessed( mirror ) ) {
156151
throw new TypeHierarchyErroneousException( mirror );
157152
}
@@ -580,6 +575,10 @@ static String trimSimpleClassName(String className) {
580575
* processed the type.
581576
*/
582577
private boolean canBeProcessed(TypeMirror type) {
578+
if ( type.getKind() == TypeKind.ERROR ) {
579+
return false;
580+
}
581+
583582
if ( type.getKind() != TypeKind.DECLARED ) {
584583
return true;
585584
}

0 commit comments

Comments
 (0)