mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#35 Extracting Mapping instantitation from visitor class
This commit is contained in:
parent
a5fd73f255
commit
895a715727
@ -442,24 +442,18 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all properties of the parameter type of the given method which
|
||||||
|
* are mapped to a corresponding property of the return type of the given
|
||||||
|
* method.
|
||||||
|
*
|
||||||
|
* @param method The method of interest
|
||||||
|
*
|
||||||
|
* @return All mapped properties for the given method
|
||||||
|
*/
|
||||||
private List<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
|
private List<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
|
||||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
Map<String, Mapping> mappings = getMappings( method );
|
||||||
|
|
||||||
MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method );
|
|
||||||
MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method );
|
|
||||||
|
|
||||||
if ( mappingAnnotation != null ) {
|
|
||||||
mappings.put( mappingAnnotation.source(), getMapping( mappingAnnotation ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( mappingsAnnotation != null ) {
|
|
||||||
mappings.putAll( getMappings( mappingsAnnotation ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return getMappedProperties( method, mappings );
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<MappedProperty> getMappedProperties(ExecutableElement method, Map<String, Mapping> mappings) {
|
|
||||||
TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getReturnType() );
|
TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getReturnType() );
|
||||||
TypeElement parameterElement = (TypeElement) typeUtils.asElement( method.getParameters().get( 0 ).asType() );
|
TypeElement parameterElement = (TypeElement) typeUtils.asElement( method.getParameters().get( 0 ).asType() );
|
||||||
|
|
||||||
@ -544,26 +538,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Mapping> getMappings(MappingsPrism mappingsAnnotation) {
|
/**
|
||||||
|
* Retrieves the mappings configured via {@code @Mapping} from the given
|
||||||
|
* method.
|
||||||
|
*
|
||||||
|
* @param method The method of interest
|
||||||
|
*
|
||||||
|
* @return The mappings for the given method, keyed by source property name
|
||||||
|
*/
|
||||||
|
private Map<String, Mapping> getMappings(ExecutableElement method) {
|
||||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||||
|
|
||||||
for ( MappingPrism mapping : mappingsAnnotation.value() ) {
|
MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method );
|
||||||
mappings.put( mapping.source(), getMapping( mapping ) );
|
MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method );
|
||||||
|
|
||||||
|
if ( mappingAnnotation != null ) {
|
||||||
|
mappings.put( mappingAnnotation.source(), Mapping.fromMappingPrism( mappingAnnotation ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( mappingsAnnotation != null ) {
|
||||||
|
mappings.putAll( Mapping.fromMappingsPrism( mappingsAnnotation ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappings;
|
return mappings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mapping getMapping(MappingPrism mapping) {
|
|
||||||
return new Mapping(
|
|
||||||
mapping.source(),
|
|
||||||
mapping.target(),
|
|
||||||
mapping.mirror,
|
|
||||||
mapping.values.source(),
|
|
||||||
mapping.values.target()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Parameter retrieveParameter(ExecutableElement method) {
|
private Parameter retrieveParameter(ExecutableElement method) {
|
||||||
List<? extends VariableElement> parameters = method.getParameters();
|
List<? extends VariableElement> parameters = method.getParameters();
|
||||||
|
|
||||||
|
@ -38,9 +38,9 @@ import org.mapstruct.ap.model.Options;
|
|||||||
|
|
||||||
@SupportedAnnotationTypes("org.mapstruct.Mapper")
|
@SupportedAnnotationTypes("org.mapstruct.Mapper")
|
||||||
@GeneratePrisms({
|
@GeneratePrisms({
|
||||||
@GeneratePrism(value = Mapper.class),
|
@GeneratePrism(value = Mapper.class, publicAccess = true),
|
||||||
@GeneratePrism(value = Mapping.class),
|
@GeneratePrism(value = Mapping.class, publicAccess = true),
|
||||||
@GeneratePrism(value = Mappings.class)
|
@GeneratePrism(value = Mappings.class, publicAccess = true)
|
||||||
})
|
})
|
||||||
@SupportedOptions(MappingProcessor.SUPPRESS_GENERATOR_TIMESTAMP)
|
@SupportedOptions(MappingProcessor.SUPPRESS_GENERATOR_TIMESTAMP)
|
||||||
public class MappingProcessor extends AbstractProcessor {
|
public class MappingProcessor extends AbstractProcessor {
|
||||||
|
@ -18,9 +18,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model.source;
|
package org.mapstruct.ap.model.source;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
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 org.mapstruct.ap.MappingPrism;
|
||||||
|
import org.mapstruct.ap.MappingsPrism;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a property mapping as configured via {@code @Mapping}.
|
* Represents a property mapping as configured via {@code @Mapping}.
|
||||||
*
|
*
|
||||||
@ -34,8 +39,29 @@ public class Mapping {
|
|||||||
private final AnnotationValue sourceAnnotationValue;
|
private final AnnotationValue sourceAnnotationValue;
|
||||||
private final AnnotationValue targetAnnotationValue;
|
private final AnnotationValue targetAnnotationValue;
|
||||||
|
|
||||||
public Mapping(String sourceName, String targetName, AnnotationMirror mirror, AnnotationValue sourceAnnotationValue,
|
public static Map<String, Mapping> fromMappingsPrism(MappingsPrism mappingsAnnotation) {
|
||||||
AnnotationValue targetAnnotationValue) {
|
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||||
|
|
||||||
|
for ( MappingPrism mapping : mappingsAnnotation.value() ) {
|
||||||
|
mappings.put( mapping.source(), fromMappingPrism( mapping ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Mapping fromMappingPrism(MappingPrism mapping) {
|
||||||
|
return new Mapping(
|
||||||
|
mapping.source(),
|
||||||
|
mapping.target(),
|
||||||
|
mapping.mirror,
|
||||||
|
mapping.values.source(),
|
||||||
|
mapping.values.target()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Mapping(String sourceName, String targetName, AnnotationMirror mirror,
|
||||||
|
AnnotationValue sourceAnnotationValue,
|
||||||
|
AnnotationValue targetAnnotationValue) {
|
||||||
this.sourceName = sourceName;
|
this.sourceName = sourceName;
|
||||||
this.targetName = targetName;
|
this.targetName = targetName;
|
||||||
this.mirror = mirror;
|
this.mirror = mirror;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user