mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#410 use static custom mapper method instead of instance method call
This commit is contained in:
parent
151f24d306
commit
d2796d7bf6
@ -44,6 +44,7 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
private final Parameter targetParameter;
|
private final Parameter targetParameter;
|
||||||
private final Accessibility accessibility;
|
private final Accessibility accessibility;
|
||||||
private final List<Type> thrownTypes;
|
private final List<Type> thrownTypes;
|
||||||
|
private final boolean isStatic;
|
||||||
|
|
||||||
protected MappingMethod(Method method) {
|
protected MappingMethod(Method method) {
|
||||||
this.name = method.getName();
|
this.name = method.getName();
|
||||||
@ -52,6 +53,7 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
this.targetParameter = method.getTargetParameter();
|
this.targetParameter = method.getTargetParameter();
|
||||||
this.accessibility = method.getAccessibility();
|
this.accessibility = method.getAccessibility();
|
||||||
this.thrownTypes = method.getThrownTypes();
|
this.thrownTypes = method.getThrownTypes();
|
||||||
|
this.isStatic = method.isStatic();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -102,6 +104,10 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
return targetParameter != null;
|
return targetParameter != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isStatic() {
|
||||||
|
return isStatic;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> types = new HashSet<Type>();
|
Set<Type> types = new HashSet<Type>();
|
||||||
|
@ -162,4 +162,10 @@ public class ForgedMethod implements Method {
|
|||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isStatic() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -130,4 +130,11 @@ public interface Method {
|
|||||||
boolean overridesMethod();
|
boolean overridesMethod();
|
||||||
|
|
||||||
ExecutableElement getExecutable();
|
ExecutableElement getExecutable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this method is static or an instance method
|
||||||
|
*
|
||||||
|
* @return true when static.
|
||||||
|
*/
|
||||||
|
boolean isStatic();
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
private void increase(Type key, Map<Type, Integer> test) {
|
||||||
Integer count = test.get( key );
|
Integer count = test.get( key );
|
||||||
count++;
|
count++;
|
||||||
|
@ -222,4 +222,10 @@ public abstract class BuiltInMethod implements Method {
|
|||||||
public ExecutableElement getExecutable() {
|
public ExecutableElement getExecutable() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isStatic() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
-->
|
-->
|
||||||
<@compress single_line=true>
|
<@compress single_line=true>
|
||||||
<#-- method is either internal to the mapper class, or external (via uses) declaringMapper!=null -->
|
<#-- 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>
|
<#macro arguments>
|
||||||
<#list parameters as param>
|
<#list parameters as param>
|
||||||
<#if param.targetType>
|
<#if param.targetType>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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?
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user