#410 use static custom mapper method instead of instance method call

This commit is contained in:
sjaakd 2015-01-19 19:57:32 +01:00
parent 151f24d306
commit d2796d7bf6
12 changed files with 263 additions and 1 deletions

View File

@ -44,6 +44,7 @@ public abstract class MappingMethod extends ModelElement {
private final Parameter targetParameter;
private final Accessibility accessibility;
private final List<Type> thrownTypes;
private final boolean isStatic;
protected MappingMethod(Method method) {
this.name = method.getName();
@ -52,6 +53,7 @@ public abstract class MappingMethod extends ModelElement {
this.targetParameter = method.getTargetParameter();
this.accessibility = method.getAccessibility();
this.thrownTypes = method.getThrownTypes();
this.isStatic = method.isStatic();
}
public String getName() {
@ -102,6 +104,10 @@ public abstract class MappingMethod extends ModelElement {
return targetParameter != null;
}
public boolean isStatic() {
return isStatic;
}
@Override
public Set<Type> getImportTypes() {
Set<Type> types = new HashSet<Type>();

View File

@ -162,4 +162,10 @@ public class ForgedMethod implements Method {
return sb.toString();
}
@Override
public boolean isStatic() {
return false;
}
}

View File

@ -130,4 +130,11 @@ public interface Method {
boolean overridesMethod();
ExecutableElement getExecutable();
/**
* Whether this method is static or an instance method
*
* @return true when static.
*/
boolean isStatic();
}

View File

@ -496,6 +496,11 @@ public class SourceMethod implements Method {
}
}
@Override
public boolean isStatic() {
return executable.getModifiers().contains( Modifier.STATIC );
}
private void increase(Type key, Map<Type, Integer> test) {
Integer count = test.get( key );
count++;

View File

@ -222,4 +222,10 @@ public abstract class BuiltInMethod implements Method {
public ExecutableElement getExecutable() {
return null;
}
@Override
public boolean isStatic() {
return false;
}
}

View File

@ -20,7 +20,7 @@
-->
<@compress single_line=true>
<#-- method is either internal to the mapper class, or external (via uses) declaringMapper!=null -->
<#if declaringMapper??>${mapperVariableName}.</#if>${name}<#if (parameters?size > 0)>( <@arguments/> )<#else>()</#if>
<#if declaringMapper??><#if static><@includeModel object=declaringMapper.type/><#else>${mapperVariableName}</#if>.</#if>${name}<#if (parameters?size > 0)>( <@arguments/> )<#else>()</#if>
<#macro arguments>
<#list parameters as param>
<#if param.targetType>

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2015 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.test.references.statics;
/**
*
* @author Sjaak Derksen
*/
public class Beer {
private float percentage;
public float getPercentage() {
return percentage;
}
public void setPercentage(float percentage) {
this.percentage = percentage;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2015 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.test.references.statics;
/**
*
* @author Sjaak Derksen
*/
public class BeerDto {
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright 2012-2015 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.test.references.statics;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper(uses = CustomMapper.class)
public interface BeerMapper {
BeerMapper INSTANCE = Mappers.getMapper( BeerMapper.class );
@Mapping( target = "category", source = "percentage")
BeerDto mapBeer(Beer beer);
}

View File

@ -0,0 +1,32 @@
/**
* Copyright 2012-2015 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.test.references.statics;
/**
*
* @author Sjaak Derksen
*/
public enum Category {
LIGHT,
LAGER,
STRONG,
BARLEY_WINE
}

View File

@ -0,0 +1,43 @@
/**
* Copyright 2012-2015 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.test.references.statics;
/**
*
* @author Sjaak Derksen
*/
//@CHECKSTYLE:OFF
public class CustomMapper {
public static Category toCategory(float in) {
if ( in < 2.5 ) {
return Category.LIGHT;
}
else if ( in < 5.5 ) {
return Category.LAGER;
}
else if ( in < 10 ) {
return Category.STRONG;
}
else {
return Category.BARLEY_WINE;
}
}
}

View File

@ -0,0 +1,47 @@
/**
* Copyright 2012-2015 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.test.references.statics;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;
/**
*
* @author Sjaak Derksen
*/
@IssueKey( "410" )
@WithClasses( { Beer.class, BeerDto.class, BeerMapper.class, Category.class, CustomMapper.class } )
@RunWith(AnnotationProcessorTestRunner.class)
public class StaticsTest {
@Test
public void shouldUseStaticMethod() {
Beer beer = new Beer(); // what the heck, open another one..
beer.setPercentage( 7 );
BeerDto result = BeerMapper.INSTANCE.mapBeer( beer );
assertThat( result ).isNotNull();
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // why settle for less?
}
}