mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#12 Extracting executable filter methods into separat class
This commit is contained in:
parent
cff12a5b49
commit
a448dbc98d
@ -30,7 +30,6 @@ import javax.lang.model.element.ExecutableElement;
|
|||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.element.VariableElement;
|
import javax.lang.model.element.VariableElement;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.type.TypeKind;
|
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.lang.model.util.ElementKindVisitor6;
|
import javax.lang.model.util.ElementKindVisitor6;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
@ -48,6 +47,7 @@ import org.mapstruct.ap.model.source.MappedProperty;
|
|||||||
import org.mapstruct.ap.model.source.Mapping;
|
import org.mapstruct.ap.model.source.Mapping;
|
||||||
import org.mapstruct.ap.model.source.Method;
|
import org.mapstruct.ap.model.source.Method;
|
||||||
import org.mapstruct.ap.model.source.Parameter;
|
import org.mapstruct.ap.model.source.Parameter;
|
||||||
|
import org.mapstruct.ap.util.Filters;
|
||||||
import org.mapstruct.ap.util.TypeUtil;
|
import org.mapstruct.ap.util.TypeUtil;
|
||||||
import org.mapstruct.ap.writer.ModelWriter;
|
import org.mapstruct.ap.writer.ModelWriter;
|
||||||
|
|
||||||
@ -273,7 +273,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Type> usedMapperTypes = new LinkedList<Type>();
|
|
||||||
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
||||||
|
|
||||||
if ( mapperPrism != null ) {
|
if ( mapperPrism != null ) {
|
||||||
@ -287,7 +286,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,12 +312,12 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
List<MappedProperty> properties = new ArrayList<MappedProperty>();
|
List<MappedProperty> properties = new ArrayList<MappedProperty>();
|
||||||
|
|
||||||
for ( ExecutableElement getterMethod : getterMethodsIn( parameterElement.getEnclosedElements() ) ) {
|
for ( ExecutableElement getterMethod : Filters.getterMethodsIn( parameterElement.getEnclosedElements() ) ) {
|
||||||
|
|
||||||
String sourcePropertyName = getPropertyName( getterMethod );
|
String sourcePropertyName = getPropertyName( getterMethod );
|
||||||
Mapping mapping = mappings.get( sourcePropertyName );
|
Mapping mapping = mappings.get( sourcePropertyName );
|
||||||
|
|
||||||
for ( ExecutableElement setterMethod : setterMethodsIn( returnTypeElement.getEnclosedElements() ) ) {
|
for ( ExecutableElement setterMethod : Filters.setterMethodsIn( returnTypeElement.getEnclosedElements() ) ) {
|
||||||
|
|
||||||
String targetPropertyName = getPropertyName( setterMethod );
|
String targetPropertyName = getPropertyName( setterMethod );
|
||||||
|
|
||||||
@ -379,36 +377,4 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
private Type retrieveReturnType(ExecutableElement method) {
|
private Type retrieveReturnType(ExecutableElement method) {
|
||||||
return typeUtil.retrieveType( method.getReturnType() );
|
return typeUtil.retrieveType( method.getReturnType() );
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
|
||||||
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
|
||||||
|
|
||||||
for ( ExecutableElement method : methodsIn( elements ) ) {
|
|
||||||
//TODO: consider is/has
|
|
||||||
String name = method.getSimpleName().toString();
|
|
||||||
|
|
||||||
if ( name.startsWith( "get" ) && name.length() > 3 && method.getParameters()
|
|
||||||
.isEmpty() && method.getReturnType().getKind() != TypeKind.VOID ) {
|
|
||||||
getterMethods.add( method );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return getterMethods;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ExecutableElement> setterMethodsIn(Iterable<? extends Element> elements) {
|
|
||||||
List<ExecutableElement> setterMethods = new LinkedList<ExecutableElement>();
|
|
||||||
|
|
||||||
for ( ExecutableElement method : methodsIn( elements ) ) {
|
|
||||||
//TODO: consider is/has
|
|
||||||
String name = method.getSimpleName().toString();
|
|
||||||
|
|
||||||
if ( name.startsWith( "set" ) && name.length() > 3 && method.getParameters()
|
|
||||||
.size() == 1 && method.getReturnType().getKind() == TypeKind.VOID ) {
|
|
||||||
setterMethods.add( method );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return setterMethods;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
64
processor/src/main/java/org/mapstruct/ap/util/Filters.java
Normal file
64
processor/src/main/java/org/mapstruct/ap/util/Filters.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
import javax.lang.model.type.TypeKind;
|
||||||
|
|
||||||
|
import static javax.lang.model.util.ElementFilter.methodsIn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter methods for working with {@link Element} collections.
|
||||||
|
*
|
||||||
|
* @author Gunnar Morling
|
||||||
|
*/
|
||||||
|
public class Filters {
|
||||||
|
|
||||||
|
public static List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
||||||
|
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
||||||
|
|
||||||
|
for ( ExecutableElement method : methodsIn( elements ) ) {
|
||||||
|
//TODO: consider is/has
|
||||||
|
String name = method.getSimpleName().toString();
|
||||||
|
|
||||||
|
if ( name.startsWith( "get" ) && name.length() > 3 && method.getParameters()
|
||||||
|
.isEmpty() && method.getReturnType().getKind() != TypeKind.VOID ) {
|
||||||
|
getterMethods.add( method );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getterMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<ExecutableElement> setterMethodsIn(Iterable<? extends Element> elements) {
|
||||||
|
List<ExecutableElement> setterMethods = new LinkedList<ExecutableElement>();
|
||||||
|
|
||||||
|
for ( ExecutableElement method : methodsIn( elements ) ) {
|
||||||
|
//TODO: consider is/has
|
||||||
|
String name = method.getSimpleName().toString();
|
||||||
|
|
||||||
|
if ( name.startsWith( "set" ) && name.length() > 3 && method.getParameters()
|
||||||
|
.size() == 1 && method.getReturnType().getKind() == TypeKind.VOID ) {
|
||||||
|
setterMethods.add( method );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return setterMethods;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user