#81, removing FactoryMethod in favour of MethodReference

This commit is contained in:
sjaakd 2014-02-02 17:15:07 +01:00
parent a9d2f788de
commit 9c45c96d8d
5 changed files with 8 additions and 113 deletions

View File

@ -38,9 +38,9 @@ import org.mapstruct.ap.model.source.Method;
public class BeanMappingMethod extends MappingMethod {
private final List<PropertyMapping> propertyMappings;
private final FactoryMethod factoryMethod;
private final MethodReference factoryMethod;
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings, FactoryMethod factoryMethod) {
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings, MethodReference factoryMethod) {
super( method );
this.propertyMappings = propertyMappings;
this.factoryMethod = factoryMethod;
@ -76,7 +76,7 @@ public class BeanMappingMethod extends MappingMethod {
return types;
}
public FactoryMethod getFactoryMethod() {
public MethodReference getFactoryMethod() {
return this.factoryMethod;
}
}

View File

@ -1,81 +0,0 @@
/**
* Copyright 2012-2014 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.model;
import java.util.HashSet;
import java.util.Set;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.ModelElement;
/**
* A factory method
*
* @author Sjaak Derksen
*/
public class FactoryMethod extends ModelElement {
private final String name;
private final boolean hasDeclaringMapper;
private final MapperReference declaringMapper;
private final Type returnType;
public FactoryMethod( Method method, MapperReference declaringMapper ) {
this.name = method.getName();
this.hasDeclaringMapper = method.getDeclaringMapper() != null;
this.declaringMapper = declaringMapper;
this.returnType = method.getReturnType();
}
public String getName() {
return name;
}
public Type getDeclaringMapper() {
return declaringMapper.getMapperType();
}
public String getMapperVariableName() {
return declaringMapper.getVariableName();
}
@Override
public Set<Type> getImportTypes() {
Set<Type> types = new HashSet<Type>();
types.add( returnType );
types.addAll( returnType.getImportTypes() );
return types;
}
/**
* There is no declaring mapper when the factory method is on an abstract
* mapper class. If the declaring mapper is a referenced mapper, then this will
* result into true.
*
* @return true when declaring mapper is a referenced mapper (or factory)
*/
public boolean isHasDeclaringMapper() {
return hasDeclaringMapper;
}
public Object getReturnType() {
return returnType;
}
}

View File

@ -45,7 +45,6 @@ import org.mapstruct.ap.conversion.Conversions;
import org.mapstruct.ap.conversion.DefaultConversionContext;
import org.mapstruct.ap.model.BeanMappingMethod;
import org.mapstruct.ap.model.DefaultMapperReference;
import org.mapstruct.ap.model.FactoryMethod;
import org.mapstruct.ap.model.IterableMappingMethod;
import org.mapstruct.ap.model.MapMappingMethod;
import org.mapstruct.ap.model.Mapper;
@ -209,9 +208,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return mappingMethods;
}
private FactoryMethod getFactoryMethod(List<MapperReference> mapperReferences, List<Method> methods,
private MethodReference getFactoryMethod(List<MapperReference> mapperReferences, List<Method> methods,
Type returnType) {
FactoryMethod result = null;
MethodReference result = null;
for ( Method method : methods ) {
if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping()
&& method.getMappings().isEmpty() && method.getParameters().isEmpty() ) {
@ -225,7 +224,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
}
}
result = new FactoryMethod(method, mapperReference);
result = new MethodReference(method, mapperReference);
}
else {
messager.printMessage(
@ -388,7 +387,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
mappedTargetProperties
);
FactoryMethod factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
MethodReference factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
return new BeanMappingMethod( method, propertyMappings, factoryMethod );
}

View File

@ -24,9 +24,7 @@ public <@includeModel object=returnType/> ${name}(<#list parameters as param><@i
return<#if returnType.name != "void"> null</#if>;
}
<#if !existingInstanceMapping>
${resultType.name} ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod/><#else>new ${resultType.name}()</#if>;
</#if>
<#if !existingInstanceMapping>${resultType.name} ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod/><#else>new ${resultType.name}()</#if>;</#if>
<#if (sourceParameters?size > 1)>
<#list sourceParameters as sourceParam>
if ( ${sourceParam.name} != null ) {

View File

@ -1,21 +0,0 @@
<#--
Copyright 2012-2014 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.
-->
<#if hasDeclaringMapper>${mapperVariableName}.</#if>${name}()