#1045 Supporting mappers with generated source/target types by deferring their generation to a later round

This commit is contained in:
Gunnar Morling 2017-01-31 22:03:31 +01:00
parent 5e59f3484c
commit 6e545347d0
10 changed files with 370 additions and 5 deletions

View File

@ -0,0 +1,34 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.itest.tests;
import org.junit.runner.RunWith;
import org.mapstruct.itest.testutil.runner.ProcessorSuite;
import org.mapstruct.itest.testutil.runner.ProcessorSuite.ProcessorType;
import org.mapstruct.itest.testutil.runner.ProcessorSuiteRunner;
/**
* Tests usage of MapStruct with another processor that generates the target type of a mapping method.
*
* @author Gunnar Morling
*/
@RunWith( ProcessorSuiteRunner.class )
@ProcessorSuite(baseDir = "targetTypeGenerationTest", processorTypes = ProcessorType.ORACLE_JAVA_8)
public class TargetTypeGenerationTest {
}

View File

@ -0,0 +1,39 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct.itest</groupId>
<artifactId>itest-targettypegeneration-aggregator</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>itest-targettypegeneration-generator</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgs>
<compilerArg>-proc:none</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,80 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.itest.targettypegeneration;
import java.io.IOException;
import java.io.Writer;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
/**
* Generates a DTO.
*
* @author Gunnar Morling
*
*/
@SupportedAnnotationTypes("*")
public class DtoGenerationProcessor extends AbstractProcessor {
private boolean hasRun = false;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if ( !hasRun ) {
try {
JavaFileObject dto = processingEnv.getFiler().createSourceFile( "org.mapstruct.itest.targettypegeneration.usage.OrderDto" );
Writer writer = dto.openWriter();
writer.append( "package org.mapstruct.itest.targettypegeneration.usage;" );
writer.append( "\n" );
writer.append( "public class OrderDto {" );
writer.append( "\n" );
writer.append( " private String item;" );
writer.append( "\n" );
writer.append( " public String getItem() {" );
writer.append( " return item;" );
writer.append( " }" );
writer.append( "\n" );
writer.append( " public void setItem(String item) {" );
writer.append( " this.item = item;" );
writer.append( " }" );
writer.append( "}" );
writer.flush();
writer.close();
}
catch (IOException e) {
throw new RuntimeException( e );
}
hasRun = true;
}
return false;
}
}

View File

@ -0,0 +1,17 @@
# Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
# and/or other contributors as indicated by the @authors tag. See the
# copyright.txt file in the distribution for a full listing of all
# contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
org.mapstruct.itest.targettypegeneration.DtoGenerationProcessor

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-it-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.mapstruct.itest</groupId>
<artifactId>itest-targettypegeneration-aggregator</artifactId>
<packaging>pom</packaging>
<modules>
<module>generator</module>
<module>usage</module>
</modules>
</project>

View File

@ -0,0 +1,47 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct.itest</groupId>
<artifactId>itest-targettypegeneration-aggregator</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>itest-targettypegeneration-usage</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct.itest</groupId>
<artifactId>itest-targettypegeneration-generator</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgs>
<compilerArg>-XprintProcessorInfo</compilerArg>
<compilerArg>-XprintRounds</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,35 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.itest.targettypegeneration.usage;
/**
* @author Gunnar Morling
*/
public class Order {
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}

View File

@ -0,0 +1,33 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.itest.targettypegeneration.usage;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Gunnar Morling
*/
@Mapper
public abstract class OrderMapper {
public static final OrderMapper INSTANCE = Mappers.getMapper( OrderMapper.class );
public abstract OrderDto orderToDto(Order order);
}

View File

@ -0,0 +1,41 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.itest.targettypegeneration.usage;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* Integration test for using MapStruct with another annotation processor that generates the target type of a mapping
* method.
*
* @author Gunnar Morling
*/
public class GeneratedTargetTypeTest {
@Test
public void considersPropertiesOnGeneratedSourceAndTargetTypes() {
Order order = new Order();
order.setItem( "my item" );
OrderDto dto = OrderMapper.INSTANCE.orderToDto( order );
assertEquals( order.getItem(), dto.getItem() );
}
}

View File

@ -147,11 +147,6 @@ public class TypeFactory {
}
public Type getType(TypeMirror mirror) {
if ( mirror.getKind() == TypeKind.ERROR ) {
throw new AnnotationProcessingException( "Encountered erroneous type " + mirror );
}
if ( !canBeProcessed( mirror ) ) {
throw new TypeHierarchyErroneousException( mirror );
}
@ -580,6 +575,10 @@ public class TypeFactory {
* processed the type.
*/
private boolean canBeProcessed(TypeMirror type) {
if ( type.getKind() == TypeKind.ERROR ) {
return false;
}
if ( type.getKind() != TypeKind.DECLARED ) {
return true;
}