diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java index 2d25273d5..6be230211 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java @@ -44,6 +44,7 @@ public abstract class MappingMethod extends ModelElement { private final Parameter targetParameter; private final Accessibility accessibility; private final List 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 getImportTypes() { Set types = new HashSet(); diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java index eb8bce6d5..7179c025a 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java @@ -162,4 +162,10 @@ public class ForgedMethod implements Method { return sb.toString(); } + + @Override + public boolean isStatic() { + return false; + } + } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java index 033dca819..b2e13d06c 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java @@ -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(); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java index ce4404149..d5b2e4771 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java @@ -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 test) { Integer count = test.get( key ); count++; diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/BuiltInMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/BuiltInMethod.java index c5105d700..d1d68b2fb 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/builtin/BuiltInMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/builtin/BuiltInMethod.java @@ -222,4 +222,10 @@ public abstract class BuiltInMethod implements Method { public ExecutableElement getExecutable() { return null; } + + @Override + public boolean isStatic() { + return false; + } + } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl index 789147f7e..99f42cb54 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl @@ -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}.${name}<#if (parameters?size > 0)>( <@arguments/> )<#else>() + <#if declaringMapper??><#if static><@includeModel object=declaringMapper.type/><#else>${mapperVariableName}.${name}<#if (parameters?size > 0)>( <@arguments/> )<#else>() <#macro arguments> <#list parameters as param> <#if param.targetType> diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/Beer.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/Beer.java new file mode 100644 index 000000000..859391cb9 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/Beer.java @@ -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; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerDto.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerDto.java new file mode 100644 index 000000000..1385df18c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerDto.java @@ -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; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerMapper.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerMapper.java new file mode 100644 index 000000000..462c1d8a2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/BeerMapper.java @@ -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); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/Category.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/Category.java new file mode 100644 index 000000000..b1c2dc7e5 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/Category.java @@ -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 + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/CustomMapper.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/CustomMapper.java new file mode 100644 index 000000000..fdb94d087 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/CustomMapper.java @@ -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; + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/statics/StaticsTest.java b/processor/src/test/java/org/mapstruct/ap/test/references/statics/StaticsTest.java new file mode 100644 index 000000000..820320a33 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/statics/StaticsTest.java @@ -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? + } +}