#470 Adding import in case decorator is in another package than mapper interface

This commit is contained in:
Gunnar Morling 2015-03-04 00:19:05 +01:00 committed by Andreas Gudian
parent b02d206de6
commit bcf28c1cc1
7 changed files with 230 additions and 4 deletions

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.model;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
@ -105,7 +106,7 @@ public class Decorator extends GeneratedType {
typeFactory, typeFactory,
elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(), elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(),
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX, mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
decoratorType.getName(), decoratorType,
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null, mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
methods, methods,
Arrays.asList( new Field( typeFactory.getType( mapperElement ), "delegate", true ) ) , 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" ) @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<MappingMethod> methods, List<? extends Field> fields, String interfaceName, List<MappingMethod> methods, List<? extends Field> fields,
Options options, VersionInformation versionInformation, Accessibility accessibility, Options options, VersionInformation versionInformation, Accessibility accessibility,
DecoratorConstructor decoratorConstructor) { DecoratorConstructor decoratorConstructor) {
@ -126,7 +129,7 @@ public class Decorator extends GeneratedType {
typeFactory, typeFactory,
packageName, packageName,
name, name,
superClassName, decoratorType.getName(),
interfaceName, interfaceName,
methods, methods,
fields, fields,
@ -136,6 +139,15 @@ public class Decorator extends GeneratedType {
new TreeSet<Type>(), new TreeSet<Type>(),
decoratorConstructor decoratorConstructor
); );
this.decoratorType = decoratorType;
}
@Override
public SortedSet<Type> getImportTypes() {
SortedSet<Type> importTypes = super.getImportTypes();
addWithDependents( importTypes, decoratorType );
return importTypes;
} }
@Override @Override

View File

@ -176,7 +176,7 @@ public abstract class GeneratedType extends ModelElement {
return constructor; return constructor;
} }
private void addWithDependents(Collection<Type> collection, Type typeToAdd) { protected void addWithDependents(Collection<Type> collection, Type typeToAdd) {
if ( typeToAdd == null ) { if ( typeToAdd == null ) {
return; return;
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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;
}
}