mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#417 Making dateFormat in @IterableMapping optional, adding checks for @IterableMapping and @MapMapping
This commit is contained in:
parent
9eec8ad6c9
commit
151f24d306
@ -41,7 +41,7 @@ public @interface IterableMapping {
|
|||||||
*
|
*
|
||||||
* @return A date format string as processable by {@link SimpleDateFormat}.
|
* @return A date format string as processable by {@link SimpleDateFormat}.
|
||||||
*/
|
*/
|
||||||
String dateFormat();
|
String dateFormat() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
|
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
|
||||||
|
@ -19,9 +19,12 @@
|
|||||||
package org.mapstruct.ap.model.source;
|
package org.mapstruct.ap.model.source;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import javax.annotation.processing.Messager;
|
||||||
import javax.lang.model.element.AnnotationMirror;
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
import javax.lang.model.element.AnnotationValue;
|
import javax.lang.model.element.AnnotationValue;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
import org.mapstruct.ap.prism.IterableMappingPrism;
|
import org.mapstruct.ap.prism.IterableMappingPrism;
|
||||||
|
|
||||||
@ -37,11 +40,21 @@ public class IterableMapping {
|
|||||||
private final AnnotationMirror mirror;
|
private final AnnotationMirror mirror;
|
||||||
private final AnnotationValue dateFormatAnnotationValue;
|
private final AnnotationValue dateFormatAnnotationValue;
|
||||||
|
|
||||||
public static IterableMapping fromPrism(IterableMappingPrism iterableMapping) {
|
public static IterableMapping fromPrism(IterableMappingPrism iterableMapping, ExecutableElement method,
|
||||||
|
Messager messager) {
|
||||||
if ( iterableMapping == null ) {
|
if ( iterableMapping == null ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( iterableMapping.dateFormat().isEmpty() && iterableMapping.qualifiedBy().isEmpty() ) {
|
||||||
|
messager.printMessage(
|
||||||
|
Diagnostic.Kind.ERROR,
|
||||||
|
"'dateformat' and 'qualifiedBy' are are are undefined in @IterableMapping, "
|
||||||
|
+ "define at least one of them.",
|
||||||
|
method
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return new IterableMapping(
|
return new IterableMapping(
|
||||||
iterableMapping.dateFormat(),
|
iterableMapping.dateFormat(),
|
||||||
iterableMapping.qualifiedBy(),
|
iterableMapping.qualifiedBy(),
|
||||||
|
@ -19,8 +19,11 @@
|
|||||||
package org.mapstruct.ap.model.source;
|
package org.mapstruct.ap.model.source;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import javax.annotation.processing.Messager;
|
||||||
import javax.lang.model.element.AnnotationMirror;
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
import org.mapstruct.ap.prism.MapMappingPrism;
|
import org.mapstruct.ap.prism.MapMappingPrism;
|
||||||
|
|
||||||
@ -37,11 +40,24 @@ public class MapMapping {
|
|||||||
private final List<TypeMirror> valueQualifiers;
|
private final List<TypeMirror> valueQualifiers;
|
||||||
private final AnnotationMirror mirror;
|
private final AnnotationMirror mirror;
|
||||||
|
|
||||||
public static MapMapping fromPrism(MapMappingPrism mapMapping) {
|
public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method, Messager messager) {
|
||||||
if ( mapMapping == null ) {
|
if ( mapMapping == null ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( mapMapping.keyDateFormat().isEmpty()
|
||||||
|
&& mapMapping.keyQualifiedBy().isEmpty()
|
||||||
|
&& mapMapping.valueDateFormat().isEmpty()
|
||||||
|
&& mapMapping.valueQualifiedBy().isEmpty() ) {
|
||||||
|
messager.printMessage(
|
||||||
|
Diagnostic.Kind.ERROR,
|
||||||
|
"'keyDateFormat', 'keyQualifiedBy', 'valueDateFormat' and 'valueQualfiedBy' are all undefined in "
|
||||||
|
+ "@MapMapping, define at least one of them.",
|
||||||
|
method
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return new MapMapping(
|
return new MapMapping(
|
||||||
mapMapping.keyDateFormat(),
|
mapMapping.keyDateFormat(),
|
||||||
mapMapping.keyQualifiedBy(),
|
mapMapping.keyQualifiedBy(),
|
||||||
|
@ -169,8 +169,8 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
returnType,
|
returnType,
|
||||||
exceptionTypes,
|
exceptionTypes,
|
||||||
getMappings( method ),
|
getMappings( method ),
|
||||||
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ) ),
|
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ), method, messager ),
|
||||||
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ) ),
|
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager ),
|
||||||
typeUtils,
|
typeUtils,
|
||||||
messager,
|
messager,
|
||||||
typeFactory
|
typeFactory
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.collection.erroneous;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import org.mapstruct.IterableMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface EmptyItererableMappingMapper {
|
||||||
|
|
||||||
|
|
||||||
|
@IterableMapping
|
||||||
|
List<String> stringListToDateList(List<Date> dates);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.collection.erroneous;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.mapstruct.MapMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface EmptyMapMappingMapper {
|
||||||
|
|
||||||
|
@MapMapping
|
||||||
|
Map<String, String> longDateMapToStringStringMap(Map<Long, Date> source);
|
||||||
|
|
||||||
|
}
|
@ -56,4 +56,35 @@ public class ErroneousCollectionMappingTest {
|
|||||||
public void shouldFailToGenerateImplementationBetweenCollectionAndNonCollection() {
|
public void shouldFailToGenerateImplementationBetweenCollectionAndNonCollection() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@IssueKey("417")
|
||||||
|
@WithClasses({ EmptyItererableMappingMapper.class })
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = EmptyItererableMappingMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 35,
|
||||||
|
messageRegExp = "'dateformat' and 'qualifiedBy' are are are undefined in @IterableMapping, "
|
||||||
|
+ "define at least one of them.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldFailOnEmptyIterableAnnotation() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@IssueKey("417")
|
||||||
|
@WithClasses({ EmptyMapMappingMapper.class })
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = EmptyMapMappingMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 34,
|
||||||
|
messageRegExp = "'keyDateFormat', 'keyQualifiedBy', 'valueDateFormat' and 'valueQualfiedBy' are all "
|
||||||
|
+ "undefined in @MapMapping, define at least one of them.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldFailOnEmptyMapAnnotation() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user