From bcf28c1cc178438d0004f25b6b399e92abff3116 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Wed, 4 Mar 2015 00:19:05 +0100 Subject: [PATCH] #470 Adding import in case decorator is in another package than mapper interface --- .../org/mapstruct/ap/model/Decorator.java | 18 +++++- .../org/mapstruct/ap/model/GeneratedType.java | 2 +- .../ap/test/imports/decorator/Actor.java | 35 ++++++++++++ .../ap/test/imports/decorator/ActorDto.java | 44 +++++++++++++++ .../test/imports/decorator/ActorMapper.java | 38 +++++++++++++ .../DecoratorInAnotherPackageTest.java | 55 +++++++++++++++++++ .../decorator/other/ActorMapperDecorator.java | 42 ++++++++++++++ 7 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/decorator/Actor.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/decorator/DecoratorInAnotherPackageTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/decorator/other/ActorMapperDecorator.java diff --git a/processor/src/main/java/org/mapstruct/ap/model/Decorator.java b/processor/src/main/java/org/mapstruct/ap/model/Decorator.java index 50af2b58b..52c84759a 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/Decorator.java +++ b/processor/src/main/java/org/mapstruct/ap/model/Decorator.java @@ -20,6 +20,7 @@ package org.mapstruct.ap.model; import java.util.Arrays; import java.util.List; +import java.util.SortedSet; import java.util.TreeSet; import javax.lang.model.element.ElementKind; @@ -105,7 +106,7 @@ public class Decorator extends GeneratedType { typeFactory, elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(), mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX, - decoratorType.getName(), + decoratorType, mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null, methods, Arrays.asList( new Field( typeFactory.getType( mapperElement ), "delegate", true ) ) , @@ -117,8 +118,10 @@ public class Decorator extends GeneratedType { } } + private final Type decoratorType; + @SuppressWarnings( "checkstyle:parameternumber" ) - private Decorator(TypeFactory typeFactory, String packageName, String name, String superClassName, + private Decorator(TypeFactory typeFactory, String packageName, String name, Type decoratorType, String interfaceName, List methods, List fields, Options options, VersionInformation versionInformation, Accessibility accessibility, DecoratorConstructor decoratorConstructor) { @@ -126,7 +129,7 @@ public class Decorator extends GeneratedType { typeFactory, packageName, name, - superClassName, + decoratorType.getName(), interfaceName, methods, fields, @@ -136,6 +139,15 @@ public class Decorator extends GeneratedType { new TreeSet(), decoratorConstructor ); + + this.decoratorType = decoratorType; + } + + @Override + public SortedSet getImportTypes() { + SortedSet importTypes = super.getImportTypes(); + addWithDependents( importTypes, decoratorType ); + return importTypes; } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java b/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java index 244e6082c..03ceeef46 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java +++ b/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java @@ -176,7 +176,7 @@ public abstract class GeneratedType extends ModelElement { return constructor; } - private void addWithDependents(Collection collection, Type typeToAdd) { + protected void addWithDependents(Collection collection, Type typeToAdd) { if ( typeToAdd == null ) { return; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/Actor.java b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/Actor.java new file mode 100644 index 000000000..10a898b2d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/Actor.java @@ -0,0 +1,35 @@ +/** + * 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.imports.decorator; + +/** + * @author Gunnar Morling + */ +public class Actor { + + private int awards; + + public int getAwards() { + return awards; + } + + public void setAwards(int awards) { + this.awards = awards; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorDto.java b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorDto.java new file mode 100644 index 000000000..2576e3599 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorDto.java @@ -0,0 +1,44 @@ +/** + * 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.imports.decorator; + +/** + * @author Gunnar Morling + */ +public class ActorDto { + + private int awards; + private boolean famous; + + public int getAwards() { + return awards; + } + + public void setAwards(int awards) { + this.awards = awards; + } + + public boolean isFamous() { + return famous; + } + + public void setFamous(boolean famous) { + this.famous = famous; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorMapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorMapper.java new file mode 100644 index 000000000..e12ae8363 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/ActorMapper.java @@ -0,0 +1,38 @@ +/** + * 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.imports.decorator; + +import org.mapstruct.DecoratedWith; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.ap.test.imports.decorator.other.ActorMapperDecorator; +import org.mapstruct.factory.Mappers; + +/** + * @author Gunnar Morling + */ +@Mapper +@DecoratedWith( ActorMapperDecorator.class ) +public interface ActorMapper { + + ActorMapper INSTANCE = Mappers.getMapper( ActorMapper.class ); + + @Mapping( target = "famous", ignore = true ) + ActorDto actorToDto(Actor actor); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/DecoratorInAnotherPackageTest.java b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/DecoratorInAnotherPackageTest.java new file mode 100644 index 000000000..a976ba919 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/DecoratorInAnotherPackageTest.java @@ -0,0 +1,55 @@ +/** + * 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.imports.decorator; + +import static org.fest.assertions.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.test.imports.decorator.other.ActorMapperDecorator; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +/** + * Test for having a decorator in another package than the mapper interface. + * + * @author Gunnar Morling + */ +@IssueKey("470") +@WithClasses({ + Actor.class, + ActorDto.class, + ActorMapper.class, + ActorMapperDecorator.class +}) +@RunWith(AnnotationProcessorTestRunner.class) +public class DecoratorInAnotherPackageTest { + + @Test + public void shouldApplyDecoratorFromAnotherPackage() { + Actor actor = new Actor(); + actor.setAwards( 3 ); + + ActorDto dto = ActorMapper.INSTANCE.actorToDto( actor ); + assertThat( dto ).isNotNull(); + assertThat( dto.getAwards() ).isEqualTo( 3 ); + assertThat( dto.isFamous() ).isTrue(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/other/ActorMapperDecorator.java b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/other/ActorMapperDecorator.java new file mode 100644 index 000000000..b93f74329 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/decorator/other/ActorMapperDecorator.java @@ -0,0 +1,42 @@ +/** + * 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.imports.decorator.other; + +import org.mapstruct.ap.test.imports.decorator.Actor; +import org.mapstruct.ap.test.imports.decorator.ActorDto; +import org.mapstruct.ap.test.imports.decorator.ActorMapper; + +/** + * @author Gunnar Morling + */ +public class ActorMapperDecorator implements ActorMapper { + + private final ActorMapper delegate; + + public ActorMapperDecorator(ActorMapper delegate) { + this.delegate = delegate; + } + + @Override + public ActorDto actorToDto(Actor actor) { + ActorDto dto = delegate.actorToDto( actor ); + dto.setFamous( actor.getAwards() >= 3 ); + return dto; + } +}