mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#109 Making Executables and Filters methods static
This commit is contained in:
parent
c281711aaa
commit
e4037c7acd
@ -82,8 +82,6 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
private TypeFactory typeFactory;
|
||||
private Conversions conversions;
|
||||
private Executables executables;
|
||||
private Filters filters;
|
||||
|
||||
@Override
|
||||
public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, List<Method> sourceModel) {
|
||||
@ -94,8 +92,6 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
this.typeFactory = context.getTypeFactory();
|
||||
this.conversions = new Conversions( elementUtils, typeFactory );
|
||||
this.executables = new Executables();
|
||||
this.filters = new Filters( executables );
|
||||
|
||||
return getMapper( mapperTypeElement, sourceModel );
|
||||
}
|
||||
@ -234,12 +230,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
private PropertyMapping getPropertyMapping(List<Method> methods, Method method, ExecutableElement targetAcessor,
|
||||
Parameter parameter) {
|
||||
String targetPropertyName = executables.getPropertyName( targetAcessor );
|
||||
String targetPropertyName = Executables.getPropertyName( targetAcessor );
|
||||
Mapping mapping = method.getMapping( targetPropertyName );
|
||||
String dateFormat = mapping != null ? mapping.getDateFormat() : null;
|
||||
String sourcePropertyName = mapping != null ? mapping.getSourcePropertyName() : targetPropertyName;
|
||||
TypeElement parameterElement = parameter.getType().getTypeElement();
|
||||
List<ExecutableElement> sourceGetters = filters.getterMethodsIn(
|
||||
List<ExecutableElement> sourceGetters = Filters.getterMethodsIn(
|
||||
elementUtils.getAllMembers( parameterElement )
|
||||
);
|
||||
|
||||
@ -249,7 +245,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
if ( method.getMappings().containsKey( sourcePropertyName ) ) {
|
||||
for ( Mapping sourceMapping : sourceMappings ) {
|
||||
boolean mapsToOtherTarget = !sourceMapping.getTargetName().equals( targetPropertyName );
|
||||
if ( executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName ) &&
|
||||
if ( Executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName ) &&
|
||||
!mapsToOtherTarget ) {
|
||||
return getPropertyMapping(
|
||||
methods,
|
||||
@ -262,7 +258,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName ) ) {
|
||||
else if ( Executables.getPropertyName( sourceAccessor ).equals( sourcePropertyName ) ) {
|
||||
return getPropertyMapping(
|
||||
methods,
|
||||
method,
|
||||
@ -287,7 +283,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
|
||||
TypeElement resultTypeElement = method.getResultType().getTypeElement();
|
||||
List<ExecutableElement> targetAccessors = filters.setterMethodsIn(
|
||||
List<ExecutableElement> targetAccessors = Filters.setterMethodsIn(
|
||||
elementUtils.getAllMembers( resultTypeElement )
|
||||
);
|
||||
targetAccessors.addAll(
|
||||
@ -297,7 +293,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
|
||||
for ( ExecutableElement targetAccessor : targetAccessors ) {
|
||||
String targetPropertyName = executables.getPropertyName( targetAccessor );
|
||||
String targetPropertyName = Executables.getPropertyName( targetAccessor );
|
||||
|
||||
Mapping mapping = method.getMapping( targetPropertyName );
|
||||
|
||||
@ -335,7 +331,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> targetProperties = executables.getPropertyNames( targetAccessors );
|
||||
Set<String> targetProperties = Executables.getPropertyNames( targetAccessors );
|
||||
|
||||
reportErrorForUnmappedTargetPropertiesIfRequired(
|
||||
method,
|
||||
@ -388,11 +384,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
private boolean hasSourceProperty(Parameter parameter, String propertyName) {
|
||||
TypeElement parameterTypeElement = parameter.getType().getTypeElement();
|
||||
List<ExecutableElement> getters = filters.getterMethodsIn(
|
||||
List<ExecutableElement> getters = Filters.getterMethodsIn(
|
||||
elementUtils.getAllMembers( parameterTypeElement )
|
||||
);
|
||||
|
||||
return executables.getPropertyNames( getters ).contains( propertyName );
|
||||
return Executables.getPropertyNames( getters ).contains( propertyName );
|
||||
}
|
||||
|
||||
private boolean reportErrorIfMappedPropertiesDontExist(Method method) {
|
||||
@ -402,7 +398,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
|
||||
TypeElement resultTypeElement = method.getResultType().getTypeElement();
|
||||
List<ExecutableElement> targetAccessors = filters.setterMethodsIn(
|
||||
List<ExecutableElement> targetAccessors = Filters.setterMethodsIn(
|
||||
elementUtils.getAllMembers( resultTypeElement )
|
||||
);
|
||||
targetAccessors.addAll(
|
||||
@ -410,7 +406,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
elementUtils.getAllMembers( resultTypeElement )
|
||||
)
|
||||
);
|
||||
Set<String> targetProperties = executables.getPropertyNames( targetAccessors );
|
||||
Set<String> targetProperties = Executables.getPropertyNames( targetAccessors );
|
||||
|
||||
boolean foundUnmappedProperty = false;
|
||||
|
||||
@ -493,10 +489,10 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
String conversionString = null;
|
||||
conversionString = parameter.getName() + "." + sourceAccessor.getSimpleName().toString() + "()";
|
||||
|
||||
if ( executables.isSetterMethod( targetAcessor ) ) {
|
||||
if ( Executables.isSetterMethod( targetAcessor ) ) {
|
||||
targetType = typeFactory.getSingleParameter( targetAcessor ).getType();
|
||||
}
|
||||
else if ( executables.isGetterMethod( targetAcessor ) ) {
|
||||
else if ( Executables.isGetterMethod( targetAcessor ) ) {
|
||||
targetType = typeFactory.getReturnType( targetAcessor );
|
||||
}
|
||||
|
||||
@ -510,10 +506,10 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
PropertyMapping property = new PropertyMapping(
|
||||
parameter.getName(),
|
||||
executables.getPropertyName( sourceAccessor ),
|
||||
Executables.getPropertyName( sourceAccessor ),
|
||||
sourceAccessor.getSimpleName().toString(),
|
||||
sourceType,
|
||||
executables.getPropertyName( targetAcessor ),
|
||||
Executables.getPropertyName( targetAcessor ),
|
||||
targetAcessor.getSimpleName().toString(),
|
||||
targetType,
|
||||
propertyMappingMethod,
|
||||
@ -799,8 +795,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
* @return
|
||||
*/
|
||||
private List<ExecutableElement> alternativeTargetAccessorMethodsIn(Iterable<? extends Element> elements) {
|
||||
List<ExecutableElement> setterMethods = filters.setterMethodsIn( elements );
|
||||
List<ExecutableElement> getterMethods = filters.getterMethodsIn( elements );
|
||||
List<ExecutableElement> setterMethods = Filters.setterMethodsIn( elements );
|
||||
List<ExecutableElement> getterMethods = Filters.getterMethodsIn( elements );
|
||||
List<ExecutableElement> alternativeTargetAccessorsMethods = new LinkedList<ExecutableElement>();
|
||||
|
||||
if ( getterMethods.size() > setterMethods.size() ) {
|
||||
@ -809,9 +805,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
// (assuming it is initialized)
|
||||
for ( ExecutableElement getterMethod : getterMethods ) {
|
||||
boolean matchFound = false;
|
||||
String getterPropertyName = executables.getPropertyName( getterMethod );
|
||||
String getterPropertyName = Executables.getPropertyName( getterMethod );
|
||||
for ( ExecutableElement setterMethod : setterMethods ) {
|
||||
String setterPropertyName = executables.getPropertyName( setterMethod );
|
||||
String setterPropertyName = Executables.getPropertyName( setterMethod );
|
||||
if ( getterPropertyName.equals( setterPropertyName ) ) {
|
||||
matchFound = true;
|
||||
break;
|
||||
|
@ -47,7 +47,6 @@ import org.mapstruct.ap.model.source.IterableMapping;
|
||||
import org.mapstruct.ap.model.source.MapMapping;
|
||||
import org.mapstruct.ap.model.source.Mapping;
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.util.Executables;
|
||||
|
||||
/**
|
||||
* A {@link ModelElementProcessor} which retrieves a list of {@link Method}s
|
||||
@ -61,13 +60,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
||||
|
||||
private Messager messager;
|
||||
private TypeFactory typeFactory;
|
||||
private Executables executables;
|
||||
|
||||
@Override
|
||||
public List<Method> process(ProcessorContext context, TypeElement mapperTypeElement, Void sourceModel) {
|
||||
this.messager = context.getMessager();
|
||||
this.typeFactory = context.getTypeFactory();
|
||||
this.executables = new Executables();
|
||||
|
||||
return retrieveMethods( mapperTypeElement, true );
|
||||
}
|
||||
|
@ -34,14 +34,14 @@ import javax.lang.model.type.TypeKind;
|
||||
*/
|
||||
public class Executables {
|
||||
|
||||
public Executables() {
|
||||
private Executables() {
|
||||
}
|
||||
|
||||
public boolean isGetterMethod(ExecutableElement method) {
|
||||
public static boolean isGetterMethod(ExecutableElement method) {
|
||||
return isPublic( method ) && ( isNonBooleanGetterMethod( method ) || isBooleanGetterMethod( method ) );
|
||||
}
|
||||
|
||||
private boolean isNonBooleanGetterMethod(ExecutableElement method) {
|
||||
private static boolean isNonBooleanGetterMethod(ExecutableElement method) {
|
||||
String name = method.getSimpleName().toString();
|
||||
|
||||
return method.getParameters().isEmpty() &&
|
||||
@ -50,7 +50,7 @@ public class Executables {
|
||||
method.getReturnType().getKind() != TypeKind.VOID;
|
||||
}
|
||||
|
||||
private boolean isBooleanGetterMethod(ExecutableElement method) {
|
||||
private static boolean isBooleanGetterMethod(ExecutableElement method) {
|
||||
String name = method.getSimpleName().toString();
|
||||
|
||||
return method.getParameters().isEmpty() &&
|
||||
@ -59,7 +59,7 @@ public class Executables {
|
||||
method.getReturnType().getKind() == TypeKind.BOOLEAN;
|
||||
}
|
||||
|
||||
public boolean isSetterMethod(ExecutableElement method) {
|
||||
public static boolean isSetterMethod(ExecutableElement method) {
|
||||
String name = method.getSimpleName().toString();
|
||||
|
||||
if ( isPublic( method ) && name.startsWith( "set" ) && name.length() > 3 && method.getParameters()
|
||||
@ -70,11 +70,11 @@ public class Executables {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isPublic(ExecutableElement method) {
|
||||
private static boolean isPublic(ExecutableElement method) {
|
||||
return method.getModifiers().contains( Modifier.PUBLIC );
|
||||
}
|
||||
|
||||
public String getPropertyName(ExecutableElement getterOrSetterMethod) {
|
||||
public static String getPropertyName(ExecutableElement getterOrSetterMethod) {
|
||||
if ( isNonBooleanGetterMethod( getterOrSetterMethod ) ) {
|
||||
return Introspector.decapitalize(
|
||||
getterOrSetterMethod.getSimpleName().toString().substring( 3 )
|
||||
@ -94,7 +94,7 @@ public class Executables {
|
||||
throw new IllegalArgumentException( "Executable " + getterOrSetterMethod + " is not getter or setter method." );
|
||||
}
|
||||
|
||||
public Set<String> getPropertyNames(List<ExecutableElement> propertyAccessors) {
|
||||
public static Set<String> getPropertyNames(List<ExecutableElement> propertyAccessors) {
|
||||
Set<String> propertyNames = new HashSet<String>();
|
||||
|
||||
for ( ExecutableElement executableElement : propertyAccessors ) {
|
||||
|
@ -33,17 +33,14 @@ import javax.lang.model.element.ExecutableElement;
|
||||
*/
|
||||
public class Filters {
|
||||
|
||||
private final Executables executables;
|
||||
|
||||
public Filters(Executables executables) {
|
||||
this.executables = executables;
|
||||
private Filters() {
|
||||
}
|
||||
|
||||
public List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
||||
public static List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
||||
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
||||
|
||||
for ( ExecutableElement method : methodsIn( elements ) ) {
|
||||
if ( executables.isGetterMethod( method ) ) {
|
||||
if ( Executables.isGetterMethod( method ) ) {
|
||||
getterMethods.add( method );
|
||||
}
|
||||
}
|
||||
@ -51,11 +48,11 @@ public class Filters {
|
||||
return getterMethods;
|
||||
}
|
||||
|
||||
public List<ExecutableElement> setterMethodsIn(Iterable<? extends Element> elements) {
|
||||
public static List<ExecutableElement> setterMethodsIn(Iterable<? extends Element> elements) {
|
||||
List<ExecutableElement> setterMethods = new LinkedList<ExecutableElement>();
|
||||
|
||||
for ( ExecutableElement method : methodsIn( elements ) ) {
|
||||
if ( executables.isSetterMethod( method ) ) {
|
||||
if ( Executables.isSetterMethod( method ) ) {
|
||||
setterMethods.add( method );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user