mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#32 Simplifying Method type
This commit is contained in:
parent
cadc20f7b3
commit
17ffcfe104
@ -240,7 +240,14 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
elementUtils.getAllMembers( returnTypeElement )
|
elementUtils.getAllMembers( returnTypeElement )
|
||||||
);
|
);
|
||||||
|
|
||||||
reportErrorIfMappedPropertiesDontExist( method );
|
Set<String> sourceProperties = Executables.getPropertyNames(
|
||||||
|
Filters.getterMethodsIn( sourceGetters )
|
||||||
|
);
|
||||||
|
Set<String> targetProperties = Executables.getPropertyNames(
|
||||||
|
Filters.setterMethodsIn( targetSetters )
|
||||||
|
);
|
||||||
|
|
||||||
|
reportErrorIfMappedPropertiesDontExist( method, sourceProperties, targetProperties );
|
||||||
|
|
||||||
for ( ExecutableElement getterMethod : sourceGetters ) {
|
for ( ExecutableElement getterMethod : sourceGetters ) {
|
||||||
String sourcePropertyName = Executables.getPropertyName( getterMethod );
|
String sourcePropertyName = Executables.getPropertyName( getterMethod );
|
||||||
@ -267,6 +274,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
reportErrorForUnmappedTargetPropertiesIfRequired(
|
reportErrorForUnmappedTargetPropertiesIfRequired(
|
||||||
method,
|
method,
|
||||||
unmappedTargetPolicy,
|
unmappedTargetPolicy,
|
||||||
|
targetProperties,
|
||||||
mappedTargetProperties
|
mappedTargetProperties
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -338,17 +346,18 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
|
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
|
||||||
ReportingPolicy unmappedTargetPolicy,
|
ReportingPolicy unmappedTargetPolicy,
|
||||||
|
Set<String> targetProperties,
|
||||||
Set<String> mappedTargetProperties) {
|
Set<String> mappedTargetProperties) {
|
||||||
|
|
||||||
if ( method.getTargetProeprties().size() > mappedTargetProperties.size() &&
|
if ( targetProperties.size() > mappedTargetProperties.size() &&
|
||||||
unmappedTargetPolicy.requiresReport() ) {
|
unmappedTargetPolicy.requiresReport() ) {
|
||||||
method.getTargetProeprties().removeAll( mappedTargetProperties );
|
targetProperties.removeAll( mappedTargetProperties );
|
||||||
printMessage(
|
printMessage(
|
||||||
unmappedTargetPolicy,
|
unmappedTargetPolicy,
|
||||||
MessageFormat.format(
|
MessageFormat.format(
|
||||||
"Unmapped target {0,choice,1#property|1<properties}: \"{1}\"",
|
"Unmapped target {0,choice,1#property|1<properties}: \"{1}\"",
|
||||||
method.getTargetProeprties().size(),
|
targetProperties.size(),
|
||||||
Strings.join( method.getTargetProeprties(), ", " )
|
Strings.join( targetProperties, ", " )
|
||||||
),
|
),
|
||||||
method.getExecutable()
|
method.getExecutable()
|
||||||
);
|
);
|
||||||
@ -450,8 +459,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
|
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
|
||||||
Parameter parameter = retrieveParameter( method );
|
Parameter parameter = retrieveParameter( method );
|
||||||
Type returnType = retrieveReturnType( method );
|
Type returnType = retrieveReturnType( method );
|
||||||
Element returnTypeElement = typeUtils.asElement( method.getReturnType() );
|
|
||||||
Element parameterElement = typeUtils.asElement( method.getParameters().get( 0 ).asType() );
|
|
||||||
|
|
||||||
boolean mappingErroneous = false;
|
boolean mappingErroneous = false;
|
||||||
|
|
||||||
@ -496,21 +503,12 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
//add method with property mappings if an implementation needs to be generated
|
//add method with property mappings if an implementation needs to be generated
|
||||||
if ( implementationRequired ) {
|
if ( implementationRequired ) {
|
||||||
Set<String> sourceProperties = Executables.getPropertyNames(
|
|
||||||
Filters.getterMethodsIn( parameterElement.getEnclosedElements() )
|
|
||||||
);
|
|
||||||
Set<String> targetProperties = Executables.getPropertyNames(
|
|
||||||
Filters.setterMethodsIn( returnTypeElement.getEnclosedElements() )
|
|
||||||
);
|
|
||||||
|
|
||||||
methods.add(
|
methods.add(
|
||||||
Method.forMethodRequiringImplementation(
|
Method.forMethodRequiringImplementation(
|
||||||
method,
|
method,
|
||||||
parameter.getName(),
|
parameter.getName(),
|
||||||
parameter.getType(),
|
parameter.getType(),
|
||||||
returnType,
|
returnType,
|
||||||
sourceProperties,
|
|
||||||
targetProperties,
|
|
||||||
getMappings( method )
|
getMappings( method )
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -544,9 +542,10 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reportErrorIfMappedPropertiesDontExist(Method method) {
|
private void reportErrorIfMappedPropertiesDontExist(Method method, Set<String> sourceProperties,
|
||||||
|
Set<String> targetProperties) {
|
||||||
for ( Mapping mappedProperty : method.getMappings().values() ) {
|
for ( Mapping mappedProperty : method.getMappings().values() ) {
|
||||||
if ( !method.getSourceProperties().contains( mappedProperty.getSourceName() ) ) {
|
if ( !sourceProperties.contains( mappedProperty.getSourceName() ) ) {
|
||||||
printMessage(
|
printMessage(
|
||||||
ReportingPolicy.ERROR,
|
ReportingPolicy.ERROR,
|
||||||
String.format(
|
String.format(
|
||||||
@ -556,7 +555,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
), method.getExecutable(), mappedProperty.getMirror(), mappedProperty.getSourceAnnotationValue()
|
), method.getExecutable(), mappedProperty.getMirror(), mappedProperty.getSourceAnnotationValue()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if ( !method.getTargetProeprties().contains( mappedProperty.getTargetName() ) ) {
|
if ( !targetProperties.contains( mappedProperty.getTargetName() ) ) {
|
||||||
printMessage(
|
printMessage(
|
||||||
ReportingPolicy.ERROR,
|
ReportingPolicy.ERROR,
|
||||||
String.format(
|
String.format(
|
||||||
|
@ -20,7 +20,6 @@ package org.mapstruct.ap.model.source;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.Type;
|
import org.mapstruct.ap.model.Type;
|
||||||
@ -38,14 +37,10 @@ public class Method {
|
|||||||
private final String parameterName;
|
private final String parameterName;
|
||||||
private final Type sourceType;
|
private final Type sourceType;
|
||||||
private final Type targetType;
|
private final Type targetType;
|
||||||
private final Set<String> sourceProperties;
|
|
||||||
private final Set<String> targetProeprties;
|
|
||||||
private Map<String, Mapping> mappings;
|
private Map<String, Mapping> mappings;
|
||||||
|
|
||||||
public static Method forMethodRequiringImplementation(ExecutableElement executable, String parameterName,
|
public static Method forMethodRequiringImplementation(ExecutableElement executable, String parameterName,
|
||||||
Type sourceType,
|
Type sourceType, Type targetType,
|
||||||
Type targetType, Set<String> sourceProperties,
|
|
||||||
Set<String> targetProperties,
|
|
||||||
Map<String, Mapping> mappings) {
|
Map<String, Mapping> mappings) {
|
||||||
|
|
||||||
return new Method(
|
return new Method(
|
||||||
@ -54,8 +49,6 @@ public class Method {
|
|||||||
parameterName,
|
parameterName,
|
||||||
sourceType,
|
sourceType,
|
||||||
targetType,
|
targetType,
|
||||||
sourceProperties,
|
|
||||||
targetProperties,
|
|
||||||
mappings
|
mappings
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -69,22 +62,17 @@ public class Method {
|
|||||||
parameterName,
|
parameterName,
|
||||||
sourceType,
|
sourceType,
|
||||||
targetType,
|
targetType,
|
||||||
Collections.<String>emptySet(),
|
|
||||||
Collections.<String>emptySet(),
|
|
||||||
Collections.<String, Mapping>emptyMap()
|
Collections.<String, Mapping>emptyMap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Method(Type declaringMapper, ExecutableElement executable, String parameterName, Type sourceType,
|
private Method(Type declaringMapper, ExecutableElement executable, String parameterName, Type sourceType,
|
||||||
Type targetType, Set<String> sourceProperties, Set<String> targetProperties,
|
Type targetType, Map<String, Mapping> mappings) {
|
||||||
Map<String, Mapping> mappings) {
|
|
||||||
this.declaringMapper = declaringMapper;
|
this.declaringMapper = declaringMapper;
|
||||||
this.executable = executable;
|
this.executable = executable;
|
||||||
this.parameterName = parameterName;
|
this.parameterName = parameterName;
|
||||||
this.sourceType = sourceType;
|
this.sourceType = sourceType;
|
||||||
this.targetType = targetType;
|
this.targetType = targetType;
|
||||||
this.sourceProperties = sourceProperties;
|
|
||||||
this.targetProeprties = targetProperties;
|
|
||||||
this.mappings = mappings;
|
this.mappings = mappings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,14 +107,6 @@ public class Method {
|
|||||||
return targetType;
|
return targetType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getSourceProperties() {
|
|
||||||
return sourceProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getTargetProeprties() {
|
|
||||||
return targetProeprties;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Mapping> getMappings() {
|
public Map<String, Mapping> getMappings() {
|
||||||
return mappings;
|
return mappings;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user