diff --git a/parent/pom.xml b/parent/pom.xml index e5b7138b8..f00b641db 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -120,6 +120,11 @@ junit 4.11 + + commons-io + commons-io + 2.4 + diff --git a/processor/pom.xml b/processor/pom.xml index 36f770a98..df04136bd 100644 --- a/processor/pom.xml +++ b/processor/pom.xml @@ -79,6 +79,11 @@ javax.inject test + + commons-io + commons-io + test + diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToBigIntegerConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToBigIntegerConversion.java index 3b1e617b2..088d03de7 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToBigIntegerConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToBigIntegerConversion.java @@ -18,9 +18,14 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; +import static org.mapstruct.ap.util.Collections.asSet; + import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Conversion between {@link BigDecimal} and {@link BigInteger}. @@ -38,4 +43,9 @@ public class BigDecimalToBigIntegerConversion extends SimpleConversion { public String getFromConversionString(String targetReference, ConversionContext conversionContext) { return "new BigDecimal( " + targetReference + " )"; } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToPrimitiveConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToPrimitiveConversion.java index dd2a71690..c8f0f4c2b 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToPrimitiveConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToPrimitiveConversion.java @@ -18,11 +18,16 @@ */ package org.mapstruct.ap.conversion; +import static org.mapstruct.ap.util.Collections.asSet; + +import java.math.BigDecimal; +import java.util.Set; + import org.mapstruct.ap.model.common.ConversionContext; -import java.math.BigInteger; +import org.mapstruct.ap.model.common.Type; /** - * Conversion between {@link BigInteger} and native number types. + * Conversion between {@link BigDecimal} and native number types. * * @author Gunnar Morling */ @@ -50,4 +55,9 @@ public class BigDecimalToPrimitiveConversion extends SimpleConversion { return conversion.toString(); } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToStringConversion.java index 8ec325aa6..a3dab367f 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToStringConversion.java @@ -18,8 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; +import static org.mapstruct.ap.util.Collections.asSet; + import java.math.BigDecimal; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Conversion between {@link BigDecimal} and {@link String}. @@ -37,4 +42,9 @@ public class BigDecimalToStringConversion extends SimpleConversion { public String getFromConversionString(String targetReference, ConversionContext conversionContext) { return "new BigDecimal( " + targetReference + " )"; } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToWrapperConversion.java index 6c337fb22..e497d7f4c 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToWrapperConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigDecimalToWrapperConversion.java @@ -18,9 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; -import java.math.BigDecimal; +import static org.mapstruct.ap.util.Collections.asSet; +import java.math.BigDecimal; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.util.NativeTypes; /** @@ -53,4 +57,9 @@ public class BigDecimalToWrapperConversion extends SimpleConversion { return conversion.toString(); } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigDecimal.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java index 4ef536bf5..730f59094 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java @@ -18,8 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; +import static org.mapstruct.ap.util.Collections.asSet; + import java.math.BigInteger; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Conversion between {@link BigInteger} and native number types. @@ -55,4 +60,9 @@ public class BigIntegerToPrimitiveConversion extends SimpleConversion { return conversion.toString(); } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java index d8715abba..642ff92f0 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java @@ -18,8 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; +import static org.mapstruct.ap.util.Collections.asSet; + import java.math.BigInteger; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Conversion between {@link BigInteger} and {@link String}. @@ -37,4 +42,9 @@ public class BigIntegerToStringConversion extends SimpleConversion { public String getFromConversionString(String targetReference, ConversionContext conversionContext) { return "new BigInteger( " + targetReference + " )"; } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java index 7b6695020..b8e590c33 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java @@ -18,9 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; -import java.math.BigInteger; +import static org.mapstruct.ap.util.Collections.asSet; +import java.math.BigInteger; +import java.util.Set; + +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.util.NativeTypes; /** @@ -60,4 +64,9 @@ public class BigIntegerToWrapperConversion extends SimpleConversion { return conversion.toString(); } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTypeFactory().getType( BigInteger.class ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java index e5a695812..ae0a854e2 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java @@ -18,7 +18,12 @@ */ package org.mapstruct.ap.conversion; +import static org.mapstruct.ap.util.Collections.asSet; + +import java.util.Set; + import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Conversion between {@link String} and {@link Enum} types. @@ -36,4 +41,9 @@ public class EnumStringConversion extends SimpleConversion { public String getFromConversionString(String targetReference, ConversionContext conversionContext) { return "Enum.valueOf( " + conversionContext.getTargetType().getName() + ".class, " + targetReference + " )"; } + + @Override + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return asSet( conversionContext.getTargetType() ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java index 853aa083d..f9f07fde2 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java @@ -18,8 +18,13 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.common.ConversionContext; +import java.util.Collections; +import java.util.List; +import java.util.Set; + import org.mapstruct.ap.model.TypeConversion; +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.Type; /** * Base class for {@link ConversionProvider}s creating {@link TypeConversion}s @@ -32,6 +37,8 @@ public abstract class SimpleConversion implements ConversionProvider { @Override public TypeConversion to(String sourceReference, ConversionContext conversionContext) { return new TypeConversion( + getToConversionImportTypes( conversionContext ), + getToConversionExceptionTypes( conversionContext ), getToConversionString( sourceReference, conversionContext ) ); } @@ -39,18 +46,49 @@ public abstract class SimpleConversion implements ConversionProvider { @Override public TypeConversion from(String targetReference, ConversionContext conversionContext) { return new TypeConversion( + getFromConversionImportTypes( conversionContext ), + getFromConversionExceptionTypes( conversionContext ), getFromConversionString( targetReference, conversionContext ) ); } + /** + * @param conversionContext the conversion context + * @return exception types required in the from-conversion + */ + protected List getFromConversionExceptionTypes(ConversionContext conversionContext) { + return Collections. emptyList(); + } + + /** + * @param conversionContext the conversion context + * @return conversion types required in the from-conversion + */ + protected Set getFromConversionImportTypes(ConversionContext conversionContext) { + return Collections. emptySet(); + } + + /** + * @param conversionContext the conversion context + * @return exception types required in the to-conversion + */ + protected List getToConversionExceptionTypes(ConversionContext conversionContext) { + return Collections. emptyList(); + } + + /** + * @param conversionContext the conversion context + * @return conversion types required in the to-conversion + */ + protected Set getToConversionImportTypes(ConversionContext conversionContext) { + return Collections. emptySet(); + } + /** * Returns the conversion string from source to target. * - * @param sourceReference A reference to the source object, e.g. - * {@code beanName.getFoo()}. - * @param conversionContext ConversionContext providing optional information required for creating - the conversion. - * + * @param sourceReference A reference to the source object, e.g. {@code beanName.getFoo()}. + * @param conversionContext ConversionContext providing optional information required for creating the conversion. * @return The conversion string from source to target. */ protected abstract String getToConversionString(String sourceReference, ConversionContext conversionContext); diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index f59dd203e..bb16c4d71 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -125,8 +125,12 @@ public class PropertyMapping extends ModelElement { @Override public Set getImportTypes() { Set importTypes = new HashSet(); - importTypes.add( sourceType ); - importTypes.add( targetType ); + + if ( isTargetAccessorSetter() && getMappingMethod() == null + && ( targetType.isCollectionType() || targetType.isMapType() ) ) { + importTypes.addAll( targetType.getImportTypes() ); + } + if ( conversion != null && mappingMethod == null ) { importTypes.addAll( conversion.getImportTypes() ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java index b959ff5c2..7fe19d0a4 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/CalendarToXmlGregorianCalendar.java @@ -45,6 +45,7 @@ public class CalendarToXmlGregorianCalendar extends BuiltInMethod { this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( + returnType, typeFactory.getType( DatatypeFactory.class ), typeFactory.getType( GregorianCalendar.class ), typeFactory.getType( DatatypeConfigurationException.class ) diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java index 0839013bd..08b926311 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/DateToXmlGregorianCalendar.java @@ -45,6 +45,7 @@ public class DateToXmlGregorianCalendar extends BuiltInMethod { this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( + returnType, typeFactory.getType( GregorianCalendar.class ), typeFactory.getType( DatatypeFactory.class ), typeFactory.getType( DatatypeConfigurationException.class ) diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java index 463b7a60f..fb4cd7318 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/JaxbElemToValue.java @@ -18,6 +18,10 @@ */ package org.mapstruct.ap.model.source.builtin; +import static org.mapstruct.ap.util.Collections.asSet; + +import java.util.Set; + import javax.xml.bind.JAXBElement; import org.mapstruct.ap.model.common.Parameter; @@ -31,10 +35,12 @@ public class JaxbElemToValue extends BuiltInMethod { private final Parameter parameter; private final Type returnType; + private final Set importTypes; public JaxbElemToValue(TypeFactory typeFactory) { this.parameter = new Parameter( "element", typeFactory.getType( JAXBElement.class ) ); this.returnType = typeFactory.getType( Object.class ); + this.importTypes = asSet( typeFactory.getType( JAXBElement.class ) ); } @Override @@ -55,4 +61,9 @@ public class JaxbElemToValue extends BuiltInMethod { public Type getReturnType() { return returnType; } + + @Override + public Set getImportTypes() { + return importTypes; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java index c98a4eee8..96775a642 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/ListOfJaxbElemToListOfValue.java @@ -18,7 +18,12 @@ */ package org.mapstruct.ap.model.source.builtin; +import static org.mapstruct.ap.util.Collections.asSet; + +import java.util.ArrayList; import java.util.List; +import java.util.Set; + import javax.xml.bind.JAXBElement; import org.mapstruct.ap.model.common.Parameter; @@ -33,11 +38,17 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod { private final Parameter parameter; private final Type returnType; private final Type genericParam; + private final Set importTypes; public ListOfJaxbElemToListOfValue(TypeFactory typeFactory) { this.parameter = new Parameter( "elementList", typeFactory.getType( List.class ) ); this.returnType = typeFactory.getType( List.class ); this.genericParam = typeFactory.getType( JAXBElement.class ).erasure(); + this.importTypes = asSet( + returnType, + typeFactory.getType( JAXBElement.class ), + typeFactory.getType( ArrayList.class ) + ); } @Override @@ -61,4 +72,9 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMethod { public Type getReturnType() { return returnType; } + + @Override + public Set getImportTypes() { + return importTypes; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java index 9185e2746..3c367e787 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/StringToXmlGregorianCalendar.java @@ -49,6 +49,7 @@ public class StringToXmlGregorianCalendar extends BuiltInMethod { this.parameter = new Parameter( "date", typeFactory.getType( String.class ) ); this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( + returnType, typeFactory.getType( Date.class ), typeFactory.getType( GregorianCalendar.class ), typeFactory.getType( SimpleDateFormat.class ), diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java index a9895cf8c..b2ca7c987 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToCalendar.java @@ -18,7 +18,11 @@ */ package org.mapstruct.ap.model.source.builtin; +import static org.mapstruct.ap.util.Collections.asSet; + import java.util.Calendar; +import java.util.Set; + import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.model.common.Parameter; @@ -32,10 +36,12 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod { private final Parameter parameter; private final Type returnType; + private final Set importTypes; public XmlGregorianCalendarToCalendar(TypeFactory typeFactory) { this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); this.returnType = typeFactory.getType( Calendar.class ); + this.importTypes = asSet( returnType, typeFactory.getType( XMLGregorianCalendar.class ) ); } @Override @@ -47,4 +53,9 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod { public Type getReturnType() { return returnType; } + + @Override + public Set getImportTypes() { + return importTypes; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java index dfdc77c70..c796e7aef 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToDate.java @@ -18,7 +18,11 @@ */ package org.mapstruct.ap.model.source.builtin; +import static org.mapstruct.ap.util.Collections.asSet; + import java.util.Date; +import java.util.Set; + import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.model.common.Parameter; @@ -32,10 +36,12 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod { private final Parameter parameter; private final Type returnType; + private final Set importTypes; public XmlGregorianCalendarToDate(TypeFactory typeFactory) { this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); this.returnType = typeFactory.getType( Date.class ); + this.importTypes = asSet( returnType, typeFactory.getType( XMLGregorianCalendar.class ) ); } @Override @@ -47,4 +53,9 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod { public Type getReturnType() { return returnType; } + + @Override + public Set getImportTypes() { + return importTypes; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java index a40d2ab4b..75decf977 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/XmlGregorianCalendarToString.java @@ -42,6 +42,7 @@ public class XmlGregorianCalendarToString extends BuiltInMethod { this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); this.returnType = typeFactory.getType( String.class ); this.importTypes = asSet( + typeFactory.getType( XMLGregorianCalendar.class ), typeFactory.getType( Date.class ), typeFactory.getType( SimpleDateFormat.class ) ); } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.builtin.JaxbElemToValue.ftl b/processor/src/main/resources/org.mapstruct.ap.model.builtin.JaxbElemToValue.ftl index 89ed1efb9..170b39671 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.builtin.JaxbElemToValue.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.builtin.JaxbElemToValue.ftl @@ -18,7 +18,7 @@ limitations under the License. --> -private T ${name}( JAXBElement element ) { +private T ${name}( JAXBElement element ) { if ( element == null ) { return null; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java b/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java index 214cba6fc..2207fc90d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java @@ -20,12 +20,17 @@ package org.mapstruct.ap.test.imports; import static org.fest.assertions.Assertions.assertThat; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.imports.from.Foo; +import org.mapstruct.ap.test.imports.referenced.NotImportedDatatype; +import org.mapstruct.ap.test.imports.referenced.Source; +import org.mapstruct.ap.test.imports.referenced.Target; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; +import org.mapstruct.ap.testutil.runner.GeneratedSource; /** * Test for generating a mapper which references types whose names clash with names of used annotations and exceptions. @@ -40,11 +45,14 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; List.class, Map.class, Foo.class, - org.mapstruct.ap.test.imports.to.Foo.class + org.mapstruct.ap.test.imports.to.Foo.class, Source.class, Target.class, NotImportedDatatype.class }) @RunWith(AnnotationProcessorTestRunner.class) public class ConflictingTypesNamesTest { + @Rule + public GeneratedSource generatedSource = new GeneratedSource(); + @Test public void mapperImportingTypesWithConflictingNamesCanBeGenerated() { Named source = new Named(); @@ -62,4 +70,17 @@ public class ConflictingTypesNamesTest { assertThat( foo2 ).isNotNull(); assertThat( foo2.getName() ).isEqualTo( "bar" ); } + + @Test + public void mapperNoUnecessaryImports() { + Source source = new Source(); + source.setNotImported( new NotImportedDatatype( 42 ) ); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getNotImported() ).isSameAs( source.getNotImported() ); + + generatedSource.forMapper( SourceTargetMapper.class ).containsNoImportFor( NotImportedDatatype.class ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java index 986995817..a7b684e60 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java @@ -21,6 +21,8 @@ package org.mapstruct.ap.test.imports; import java.util.Date; import org.mapstruct.Mapper; +import org.mapstruct.ap.test.imports.referenced.Source; +import org.mapstruct.ap.test.imports.referenced.Target; import org.mapstruct.ap.test.imports.to.Foo; import org.mapstruct.factory.Mappers; @@ -44,4 +46,6 @@ public interface SourceTargetMapper { java.util.List stringsToDates(java.util.List stringDates); java.util.Map stringsToDates(java.util.Map stringDates); + + Target sourceToTarget(Source target); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/NotImportedDatatype.java b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/NotImportedDatatype.java new file mode 100644 index 000000000..bab1dea7d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/NotImportedDatatype.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2014 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.ap.test.imports.referenced; + +/** + * @author Andreas Gudian + * + */ +public class NotImportedDatatype { + private final int value; + + public NotImportedDatatype(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Source.java b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Source.java new file mode 100644 index 000000000..e04ef07b1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Source.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2014 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.ap.test.imports.referenced; + +/** + * @author Andreas Gudian + * + */ +public class Source { + private NotImportedDatatype notImported; + + public NotImportedDatatype getNotImported() { + return notImported; + } + + public void setNotImported(NotImportedDatatype notImported) { + this.notImported = notImported; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Target.java b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Target.java new file mode 100644 index 000000000..2aeb69748 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/Target.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2014 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.ap.test.imports.referenced; + +/** + * @author Andreas Gudian + * + */ +public class Target { + private NotImportedDatatype notImported; + + public NotImportedDatatype getNotImported() { + return notImported; + } + + public void setNotImported(NotImportedDatatype notImported) { + this.notImported = notImported; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/assertions/JavaFileAssert.java b/processor/src/test/java/org/mapstruct/ap/testutil/assertions/JavaFileAssert.java new file mode 100644 index 000000000..0fef0ab6f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/testutil/assertions/JavaFileAssert.java @@ -0,0 +1,75 @@ +/** + * Copyright 2012-2014 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.ap.testutil.assertions; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.fest.assertions.Assertions; +import org.fest.assertions.FileAssert; +import org.fest.assertions.StringAssert; + +/** + * Allows to perform assertions on .java source files. + * + * @author Andreas Gudian + */ +public class JavaFileAssert extends FileAssert { + /** + * @param actual the actual file + */ + public JavaFileAssert(File actual) { + super( actual ); + } + + /** + * @return assertion on the file content + */ + public StringAssert content() { + exists(); + isFile(); + + try { + return Assertions.assertThat( FileUtils.readFileToString( actual ) ); + } + catch ( IOException e ) { + failIfCustomMessageIsSet( e ); + } + return null; + } + + /** + * Verifies that the specified class is imported in this Java file + * + * @param importedClass the class expected to be imported in this Java file + */ + public void containsImportFor(Class importedClass) { + content().contains( "import " + importedClass.getName() + ";" ); + } + + /** + * Verifies that the specified class is not imported in this Java file + * + * @param importedClass the class expected not to be imported in this Java file + */ + public void containsNoImportFor(Class importedClass) { + content().doesNotContain( "import " + importedClass.getName() + ";" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilingStatement.java b/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilingStatement.java index c24e554c1..6710ddb90 100644 --- a/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilingStatement.java +++ b/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilingStatement.java @@ -113,6 +113,10 @@ class CompilingStatement extends Statement { next.evaluate(); } + static String getSourceOutputDir() { + return COMPILATION_CACHE.get().lastSourceOutputDir; + } + protected void setupCompiler() throws Exception { compiler = ToolProvider.getSystemJavaCompiler(); @@ -287,6 +291,8 @@ class CompilingStatement extends Statement { } setupCompiler(); + cache.lastSourceOutputDir = sourceOutputDir; + DiagnosticCollector diagnostics = new DiagnosticCollector(); StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ); @@ -371,6 +377,7 @@ class CompilingStatement extends Statement { } private static class CompilationCache { + private String lastSourceOutputDir; private CompilationRequest lastRequest; private CompilationResultHolder lastResult; } diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java b/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java new file mode 100644 index 000000000..602062368 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java @@ -0,0 +1,63 @@ +/** + * Copyright 2012-2014 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.ap.testutil.runner; + +import java.io.File; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.mapstruct.ap.testutil.assertions.JavaFileAssert; + +/** + * A {@link TestRule} to perform assertions on generated source files. + *

+ * To add it to the test, use: + * + *

+ * @Rule
+ * public GeneratedSource generatedSources = new GeneratedSource();
+ * 
+ * + * @author Andreas Gudian + */ +public class GeneratedSource implements TestRule { + + @Override + public Statement apply(Statement base, Description description) { + return base; + } + + /** + * @param mapperClass the class annotated with {@code @Mapper} + * @return an assert for the *Impl.java for the given mapper + */ + public JavaFileAssert forMapper(Class mapperClass) { + String generatedJavaFileName = mapperClass.getName().replace( '.', '/' ).concat( "Impl.java" ); + return forJavaFile( generatedJavaFileName ); + } + + /** + * @param path the path relative to the source output directory of the java file to return an assert for + * @return an assert for the file specified by the given path + */ + public JavaFileAssert forJavaFile(String path) { + return new JavaFileAssert( new File( CompilingStatement.getSourceOutputDir() + "/" + path ) ); + } +}