Skip to content

Commit cf668be

Browse files
authored
mapstruct#1576 Delay determining whether a Type needs to be imported & java.time cleanup (mapstruct#1642)
1 parent 3ff4ebd commit cf668be

30 files changed

+417
-230
lines changed

parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@
613613
<configuration>
614614
<signature>
615615
<groupId>org.codehaus.mojo.signature</groupId>
616-
<artifactId>java16</artifactId>
616+
<artifactId>java18</artifactId>
617617
<version>1.0</version>
618618
</signature>
619619
</configuration>

processor/src/main/java/org/mapstruct/ap/MappingProcessor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
import java.util.HashSet;
1414
import java.util.Iterator;
1515
import java.util.List;
16+
import java.util.Map;
1617
import java.util.ServiceLoader;
1718
import java.util.Set;
18-
19+
import java.util.stream.Collectors;
1920
import javax.annotation.processing.AbstractProcessor;
2021
import javax.annotation.processing.ProcessingEnvironment;
2122
import javax.annotation.processing.Processor;
@@ -25,6 +26,7 @@
2526
import javax.lang.model.SourceVersion;
2627
import javax.lang.model.element.Element;
2728
import javax.lang.model.element.ElementKind;
29+
import javax.lang.model.element.Name;
2830
import javax.lang.model.element.TypeElement;
2931
import javax.lang.model.util.ElementKindVisitor6;
3032
import javax.tools.Diagnostic.Kind;
@@ -41,6 +43,8 @@
4143
import org.mapstruct.ap.internal.util.RoundContext;
4244
import org.mapstruct.ap.spi.TypeHierarchyErroneousException;
4345

46+
import static javax.lang.model.element.ElementKind.CLASS;
47+
4448
/**
4549
* A JSR 269 annotation {@link Processor} which generates the implementations for mapper interfaces (interfaces
4650
* annotated with {@code @Mapper}).
@@ -209,8 +213,9 @@ private void processMapperElements(Set<TypeElement> mapperElements, RoundContext
209213
// note that this assumes that a new source file is created for each mapper which must not
210214
// necessarily be the case, e.g. in case of several mapper interfaces declared as inner types
211215
// of one outer interface
216+
List<? extends Element> tst = mapperElement.getEnclosedElements();
212217
ProcessorContext context = new DefaultModelElementProcessorContext(
213-
processingEnv, options, roundContext
218+
processingEnv, options, roundContext, getDeclaredTypesNotToBeImported( mapperElement )
214219
);
215220

216221
processMapperTypeElement( context, mapperElement );
@@ -225,6 +230,14 @@ private void processMapperElements(Set<TypeElement> mapperElements, RoundContext
225230
}
226231
}
227232

233+
private Map<String, String> getDeclaredTypesNotToBeImported(TypeElement element) {
234+
return element.getEnclosedElements().stream()
235+
.filter( e -> CLASS.equals( e.getKind() ) )
236+
.map( Element::getSimpleName )
237+
.map( Name::toString )
238+
.collect( Collectors.toMap( k -> k, v -> element.getQualifiedName().toString() + "." + v ) );
239+
}
240+
228241
private void handleUncaughtError(Element element, Throwable thrown) {
229242
StringWriter sw = new StringWriter();
230243
thrown.printStackTrace( new PrintWriter( sw ) );

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
package org.mapstruct.ap.internal.conversion;
77

8+
import java.time.format.DateTimeFormatter;
89
import java.util.Set;
910

1011
import org.mapstruct.ap.internal.model.common.ConversionContext;
1112
import org.mapstruct.ap.internal.model.common.Type;
1213
import org.mapstruct.ap.internal.util.Collections;
13-
import org.mapstruct.ap.internal.util.JavaTimeConstants;
1414
import org.mapstruct.ap.internal.util.Strings;
1515

1616
/**
@@ -53,7 +53,7 @@ private String dateTimeFormatter(ConversionContext conversionContext) {
5353
@Override
5454
protected String getFromExpression(ConversionContext conversionContext) {
5555
// See http://docs.oracle.com/javase/tutorial/datetime/iso/format.html for how to parse Dates
56-
return new StringBuilder().append( conversionContext.getTargetType().getReferenceName() )
56+
return new StringBuilder().append( conversionContext.getTargetType().createReferenceName() )
5757
.append( ".parse( " )
5858
.append( parametersListForParsing( conversionContext ) )
5959
.append( " )" ).toString();
@@ -72,7 +72,7 @@ private String parametersListForParsing(ConversionContext conversionContext) {
7272
@Override
7373
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
7474
return Collections.asSet(
75-
conversionContext.getTypeFactory().getType( JavaTimeConstants.DATE_TIME_FORMATTER_FQN )
75+
conversionContext.getTypeFactory().getType( DateTimeFormatter.class )
7676
);
7777
}
7878

@@ -81,7 +81,7 @@ protected Set<Type> getFromConversionImportTypes(ConversionContext conversionCon
8181
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
8282
return Collections.asSet(
8383
conversionContext.getTargetType(),
84-
conversionContext.getTypeFactory().getType( JavaTimeConstants.DATE_TIME_FORMATTER_FQN )
84+
conversionContext.getTypeFactory().getType( DateTimeFormatter.class )
8585
);
8686
}
8787

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
import java.sql.Timestamp;
1212
import java.text.DecimalFormat;
1313
import java.text.SimpleDateFormat;
14+
import java.time.LocalDateTime;
15+
import java.time.ZoneId;
16+
import java.time.ZoneOffset;
17+
import java.time.ZonedDateTime;
18+
import java.time.format.DateTimeFormatter;
1419
import java.util.Currency;
1520
import java.util.Locale;
1621

1722
import org.mapstruct.ap.internal.model.common.ConversionContext;
18-
import org.mapstruct.ap.internal.util.JavaTimeConstants;
1923
import org.mapstruct.ap.internal.util.JodaTimeConstants;
2024

2125
/**
@@ -37,7 +41,7 @@ private ConversionUtils() {
3741
* @return Name or fully-qualified name.
3842
*/
3943
private static String typeReferenceName(ConversionContext conversionContext, String canonicalName) {
40-
return conversionContext.getTypeFactory().getType( canonicalName ).getReferenceName();
44+
return conversionContext.getTypeFactory().getType( canonicalName ).createReferenceName();
4145
}
4246

4347
/**
@@ -49,7 +53,7 @@ private static String typeReferenceName(ConversionContext conversionContext, Str
4953
* @return Name or fully-qualified name.
5054
*/
5155
private static String typeReferenceName(ConversionContext conversionContext, Class<?> type) {
52-
return conversionContext.getTypeFactory().getType( type ).getReferenceName();
56+
return conversionContext.getTypeFactory().getType( type ).createReferenceName();
5357
}
5458

5559
/**
@@ -170,7 +174,7 @@ public static String date(ConversionContext conversionContext) {
170174
* @return Name or fully-qualified name.
171175
*/
172176
public static String zoneOffset(ConversionContext conversionContext) {
173-
return typeReferenceName( conversionContext, JavaTimeConstants.ZONE_OFFSET_FQN );
177+
return typeReferenceName( conversionContext, ZoneOffset.class );
174178
}
175179

176180
/**
@@ -181,7 +185,7 @@ public static String zoneOffset(ConversionContext conversionContext) {
181185
* @return Name or fully-qualified name.
182186
*/
183187
public static String zoneId(ConversionContext conversionContext) {
184-
return typeReferenceName( conversionContext, JavaTimeConstants.ZONE_ID_FQN );
188+
return typeReferenceName( conversionContext, ZoneId.class );
185189
}
186190

187191
/**
@@ -192,7 +196,7 @@ public static String zoneId(ConversionContext conversionContext) {
192196
* @return Name or fully-qualified name.
193197
*/
194198
public static String localDateTime(ConversionContext conversionContext) {
195-
return typeReferenceName( conversionContext, JavaTimeConstants.LOCAL_DATE_TIME_FQN );
199+
return typeReferenceName( conversionContext, LocalDateTime.class );
196200
}
197201

198202
/**
@@ -203,7 +207,7 @@ public static String localDateTime(ConversionContext conversionContext) {
203207
* @return Name or fully-qualified name.
204208
*/
205209
public static String zonedDateTime(ConversionContext conversionContext) {
206-
return typeReferenceName( conversionContext, JavaTimeConstants.ZONED_DATE_TIME_FQN );
210+
return typeReferenceName( conversionContext, ZonedDateTime.class );
207211
}
208212

209213
/**
@@ -214,7 +218,7 @@ public static String zonedDateTime(ConversionContext conversionContext) {
214218
* @return Name or fully-qualified name.
215219
*/
216220
public static String dateTimeFormatter(ConversionContext conversionContext) {
217-
return typeReferenceName( conversionContext, JavaTimeConstants.DATE_TIME_FORMATTER_FQN );
221+
return typeReferenceName( conversionContext, DateTimeFormatter.class );
218222
}
219223

220224
/**

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
import java.math.BigInteger;
1010
import java.sql.Time;
1111
import java.sql.Timestamp;
12+
import java.time.Instant;
13+
import java.time.LocalDate;
14+
import java.time.LocalDateTime;
15+
import java.time.LocalTime;
16+
import java.time.ZonedDateTime;
1217
import java.util.Calendar;
1318
import java.util.Currency;
1419
import java.util.Date;
@@ -18,7 +23,6 @@
1823

1924
import org.mapstruct.ap.internal.model.common.Type;
2025
import org.mapstruct.ap.internal.model.common.TypeFactory;
21-
import org.mapstruct.ap.internal.util.JavaTimeConstants;
2226
import org.mapstruct.ap.internal.util.JodaTimeConstants;
2327

2428
import static org.mapstruct.ap.internal.conversion.ReverseConversion.reverse;
@@ -207,33 +211,26 @@ private void registerJodaConversions() {
207211
}
208212

209213
private void registerJava8TimeConversions() {
210-
if ( !isJava8TimeAvailable() ) {
211-
return;
212-
}
213214

214215
// Java 8 time to String
215-
register( JavaTimeConstants.ZONED_DATE_TIME_FQN, String.class, new JavaZonedDateTimeToStringConversion() );
216-
register( JavaTimeConstants.LOCAL_DATE_FQN, String.class, new JavaLocalDateToStringConversion() );
217-
register( JavaTimeConstants.LOCAL_DATE_TIME_FQN, String.class, new JavaLocalDateTimeToStringConversion() );
218-
register( JavaTimeConstants.LOCAL_TIME_FQN, String.class, new JavaLocalTimeToStringConversion() );
216+
register( ZonedDateTime.class, String.class, new JavaZonedDateTimeToStringConversion() );
217+
register( LocalDate.class, String.class, new JavaLocalDateToStringConversion() );
218+
register( LocalDateTime.class, String.class, new JavaLocalDateTimeToStringConversion() );
219+
register( LocalTime.class, String.class, new JavaLocalTimeToStringConversion() );
219220

220221
// Java 8 to Date
221-
register( JavaTimeConstants.ZONED_DATE_TIME_FQN, Date.class, new JavaZonedDateTimeToDateConversion() );
222-
register( JavaTimeConstants.LOCAL_DATE_TIME_FQN, Date.class, new JavaLocalDateTimeToDateConversion() );
223-
register( JavaTimeConstants.LOCAL_DATE_FQN, Date.class, new JavaLocalDateToDateConversion() );
224-
register( JavaTimeConstants.LOCAL_DATE_FQN, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
225-
register( JavaTimeConstants.INSTANT, Date.class, new JavaInstantToDateConversion() );
222+
register( ZonedDateTime.class, Date.class, new JavaZonedDateTimeToDateConversion() );
223+
register( LocalDateTime.class, Date.class, new JavaLocalDateTimeToDateConversion() );
224+
register( LocalDate.class, Date.class, new JavaLocalDateToDateConversion() );
225+
register( LocalDate.class, java.sql.Date.class, new JavaLocalDateToSqlDateConversion() );
226+
register( Instant.class, Date.class, new JavaInstantToDateConversion() );
226227

227228
}
228229

229230
private boolean isJodaTimeAvailable() {
230231
return typeFactory.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
231232
}
232233

233-
private boolean isJava8TimeAvailable() {
234-
return typeFactory.isTypeAvailable( JavaTimeConstants.ZONED_DATE_TIME_FQN );
235-
}
236-
237234
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
238235
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
239236
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public String getToExpression(ConversionContext conversionContext) {
2626

2727
@Override
2828
public String getFromExpression(ConversionContext conversionContext) {
29-
return "Enum.valueOf( " + conversionContext.getTargetType().getReferenceName()
29+
return "Enum.valueOf( " + conversionContext.getTargetType().createReferenceName()
3030
+ ".class, <SOURCE> )";
3131
}
3232

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
package org.mapstruct.ap.internal.conversion;
77

8+
import java.time.LocalDateTime;
9+
import java.time.ZoneId;
10+
import java.time.ZoneOffset;
811
import java.util.Date;
912
import java.util.Set;
1013

@@ -16,9 +19,6 @@
1619
import static org.mapstruct.ap.internal.conversion.ConversionUtils.localDateTime;
1720
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zoneId;
1821
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zoneOffset;
19-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.LOCAL_DATE_TIME_FQN;
20-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONE_ID_FQN;
21-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONE_OFFSET_FQN;
2222

2323
/**
2424
* SimpleConversion for mapping {@link java.time.LocalDateTime} to
@@ -38,7 +38,7 @@ protected String getToExpression(ConversionContext conversionContext) {
3838
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
3939
return Collections.asSet(
4040
conversionContext.getTypeFactory().getType( Date.class ),
41-
conversionContext.getTypeFactory().getType( ZONE_OFFSET_FQN )
41+
conversionContext.getTypeFactory().getType( ZoneOffset.class )
4242
);
4343
}
4444

@@ -53,8 +53,8 @@ protected String getFromExpression(ConversionContext conversionContext) {
5353
@Override
5454
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
5555
return Collections.asSet(
56-
conversionContext.getTypeFactory().getType( LOCAL_DATE_TIME_FQN ),
57-
conversionContext.getTypeFactory().getType( ZONE_ID_FQN )
56+
conversionContext.getTypeFactory().getType( LocalDateTime.class ),
57+
conversionContext.getTypeFactory().getType( ZoneId.class )
5858
);
5959
}
6060
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.mapstruct.ap.internal.conversion;
77

8+
import java.time.LocalDateTime;
9+
import java.time.ZoneOffset;
810
import java.util.Date;
911
import java.util.Set;
1012

@@ -15,8 +17,6 @@
1517
import static org.mapstruct.ap.internal.conversion.ConversionUtils.date;
1618
import static org.mapstruct.ap.internal.conversion.ConversionUtils.localDateTime;
1719
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zoneOffset;
18-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.LOCAL_DATE_TIME_FQN;
19-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONE_OFFSET_FQN;
2020

2121
/**
2222
* SimpleConversion for mapping {@link java.time.LocalDate} to
@@ -36,7 +36,7 @@ protected String getToExpression(ConversionContext conversionContext) {
3636
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
3737
return Collections.asSet(
3838
conversionContext.getTypeFactory().getType( Date.class ),
39-
conversionContext.getTypeFactory().getType( ZONE_OFFSET_FQN )
39+
conversionContext.getTypeFactory().getType( ZoneOffset.class )
4040
);
4141
}
4242

@@ -51,8 +51,8 @@ protected String getFromExpression(ConversionContext conversionContext) {
5151
@Override
5252
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
5353
return Collections.asSet(
54-
conversionContext.getTypeFactory().getType( LOCAL_DATE_TIME_FQN ),
55-
conversionContext.getTypeFactory().getType( ZONE_OFFSET_FQN )
54+
conversionContext.getTypeFactory().getType( LocalDateTime.class ),
55+
conversionContext.getTypeFactory().getType( ZoneOffset.class )
5656
);
5757
}
5858
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.mapstruct.ap.internal.conversion;
77

88
import java.sql.Date;
9+
import java.time.ZoneOffset;
910
import java.util.Set;
1011

1112
import org.mapstruct.ap.internal.model.common.ConversionContext;
@@ -14,7 +15,6 @@
1415

1516
import static org.mapstruct.ap.internal.conversion.ConversionUtils.sqlDate;
1617
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zoneOffset;
17-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONE_OFFSET_FQN;
1818

1919
/**
2020
* SimpleConversion for mapping {@link java.time.LocalDate} to
@@ -34,7 +34,7 @@ protected String getToExpression(ConversionContext conversionContext) {
3434
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
3535
return Collections.asSet(
3636
conversionContext.getTypeFactory().getType( Date.class ),
37-
conversionContext.getTypeFactory().getType( ZONE_OFFSET_FQN )
37+
conversionContext.getTypeFactory().getType( ZoneOffset.class )
3838
);
3939
}
4040

@@ -46,7 +46,7 @@ protected String getFromExpression(ConversionContext conversionContext) {
4646
@Override
4747
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
4848
return Collections.asSet(
49-
conversionContext.getTypeFactory().getType( ZONE_OFFSET_FQN )
49+
conversionContext.getTypeFactory().getType( ZoneOffset.class )
5050
);
5151
}
5252

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.mapstruct.ap.internal.conversion;
77

8+
import java.time.ZoneId;
9+
import java.time.ZonedDateTime;
810
import java.util.Date;
911
import java.util.Set;
1012

@@ -15,8 +17,6 @@
1517
import static org.mapstruct.ap.internal.conversion.ConversionUtils.date;
1618
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zoneId;
1719
import static org.mapstruct.ap.internal.conversion.ConversionUtils.zonedDateTime;
18-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONED_DATE_TIME_FQN;
19-
import static org.mapstruct.ap.internal.util.JavaTimeConstants.ZONE_ID_FQN;
2020

2121
/**
2222
* SimpleConversion for mapping {@link java.time.ZonedDateTime} to
@@ -48,8 +48,8 @@ protected String getFromExpression(ConversionContext conversionContext) {
4848
@Override
4949
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
5050
return Collections.asSet(
51-
conversionContext.getTypeFactory().getType( ZONED_DATE_TIME_FQN ),
52-
conversionContext.getTypeFactory().getType( ZONE_ID_FQN )
51+
conversionContext.getTypeFactory().getType( ZonedDateTime.class ),
52+
conversionContext.getTypeFactory().getType( ZoneId.class )
5353
);
5454
}
5555
}

0 commit comments

Comments
 (0)