mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#768 Consistently using Type#getImportTypes() for determining imported types
This commit is contained in:
parent
18912727ca
commit
27aa40c0ec
@ -171,7 +171,7 @@ public class Decorator extends GeneratedType {
|
||||
@Override
|
||||
public SortedSet<Type> getImportTypes() {
|
||||
SortedSet<Type> importTypes = super.getImportTypes();
|
||||
addWithDependents( importTypes, decoratorType );
|
||||
addIfImportRequired( importTypes, decoratorType );
|
||||
return importTypes;
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Accessibility;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
@ -159,24 +160,24 @@ public abstract class GeneratedType extends ModelElement {
|
||||
|
||||
for ( MappingMethod mappingMethod : methods ) {
|
||||
for ( Type type : mappingMethod.getImportTypes() ) {
|
||||
addWithDependents( importedTypes, type );
|
||||
addIfImportRequired( importedTypes, type );
|
||||
}
|
||||
}
|
||||
|
||||
for ( Field field : fields ) {
|
||||
if ( field.isTypeRequiresImport() ) {
|
||||
for ( Type type : field.getImportTypes() ) {
|
||||
addWithDependents( importedTypes, type );
|
||||
addIfImportRequired( importedTypes, type );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( Annotation annotation : annotations ) {
|
||||
addWithDependents( importedTypes, annotation.getType() );
|
||||
addIfImportRequired( importedTypes, annotation.getType() );
|
||||
}
|
||||
|
||||
for ( Type extraImport : extraImportedTypes ) {
|
||||
addWithDependents( importedTypes, extraImport );
|
||||
addIfImportRequired( importedTypes, extraImport );
|
||||
}
|
||||
|
||||
return importedTypes;
|
||||
@ -190,22 +191,13 @@ public abstract class GeneratedType extends ModelElement {
|
||||
constructor = null;
|
||||
}
|
||||
|
||||
protected void addWithDependents(Collection<Type> collection, Type typeToAdd) {
|
||||
protected void addIfImportRequired(Collection<Type> collection, Type typeToAdd) {
|
||||
if ( typeToAdd == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( needsImportDeclaration( typeToAdd ) ) {
|
||||
if ( typeToAdd.isArrayType() ) {
|
||||
collection.add( typeToAdd.getComponentType() );
|
||||
}
|
||||
else {
|
||||
collection.add( typeToAdd );
|
||||
}
|
||||
}
|
||||
|
||||
for ( Type type : typeToAdd.getTypeParameters() ) {
|
||||
addWithDependents( collection, type );
|
||||
collection.add( typeToAdd );
|
||||
}
|
||||
}
|
||||
|
||||
@ -214,18 +206,20 @@ public abstract class GeneratedType extends ModelElement {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( typeToAdd.getPackageName() == null ) {
|
||||
if ( typeToAdd.getTypeMirror().getKind() != TypeKind.DECLARED ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( typeToAdd.getPackageName().startsWith( JAVA_LANG_PACKAGE ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( typeToAdd.getPackageName().equals( packageName ) ) {
|
||||
if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) {
|
||||
if ( typeToAdd.getPackageName() != null ) {
|
||||
if ( typeToAdd.getPackageName().startsWith( JAVA_LANG_PACKAGE ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( typeToAdd.getPackageName().equals( packageName ) ) {
|
||||
if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -194,7 +194,6 @@ public class IterableMappingMethod extends MappingMethod {
|
||||
types.addAll( elementAssignment.getImportTypes() );
|
||||
}
|
||||
if ( ( factoryMethod == null ) && ( !isExistingInstanceMapping() ) ) {
|
||||
types.addAll( getReturnType().getImportTypes() );
|
||||
if ( getReturnType().getImplementationType() != null ) {
|
||||
types.addAll( getReturnType().getImplementationType().getImportTypes() );
|
||||
}
|
||||
|
@ -151,11 +151,15 @@ public abstract class MappingMethod extends ModelElement {
|
||||
Set<Type> types = new HashSet<Type>();
|
||||
|
||||
for ( Parameter param : parameters ) {
|
||||
types.add( param.getType() );
|
||||
types.addAll( param.getType().getImportTypes() );
|
||||
}
|
||||
|
||||
types.addAll( getReturnType().getImportTypes() );
|
||||
|
||||
for ( Type type : thrownTypes ) {
|
||||
types.addAll( type.getImportTypes() );
|
||||
}
|
||||
|
||||
types.add( getReturnType() );
|
||||
types.addAll( thrownTypes );
|
||||
return types;
|
||||
}
|
||||
|
||||
|
@ -76,10 +76,16 @@ public class MethodReference extends MappingMethod implements Assignment {
|
||||
super( method );
|
||||
this.declaringMapper = declaringMapper;
|
||||
this.contextParam = null;
|
||||
Set<Type> imported = new HashSet<Type>( method.getThrownTypes() );
|
||||
if ( targetType != null ) {
|
||||
imported.add( targetType );
|
||||
Set<Type> imported = new HashSet<Type>();
|
||||
|
||||
for ( Type type : method.getThrownTypes() ) {
|
||||
imported.addAll( type.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( targetType != null ) {
|
||||
imported.addAll( targetType.getImportTypes() );
|
||||
}
|
||||
|
||||
this.importTypes = Collections.<Type>unmodifiableSet( imported );
|
||||
this.thrownTypes = method.getThrownTypes();
|
||||
this.isUpdateMethod = method.getMappingTargetParameter() != null;
|
||||
|
@ -265,7 +265,9 @@ public class Type extends ModelElement implements Comparable<Type> {
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> result = new HashSet<Type>();
|
||||
|
||||
result.add( this );
|
||||
if ( getTypeMirror().getKind() == TypeKind.DECLARED ) {
|
||||
result.add( this );
|
||||
}
|
||||
|
||||
if ( componentType != null ) {
|
||||
result.addAll( componentType.getImportTypes() );
|
||||
@ -275,6 +277,10 @@ public class Type extends ModelElement implements Comparable<Type> {
|
||||
result.addAll( parameter.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( boundingBase != null ) {
|
||||
result.addAll( boundingBase.getImportTypes() );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.astronautmapper.AstronautMapper;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.dto.AstronautDto;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.dto.SpaceshipDto;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.entity.Astronaut;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.entity.Spaceship;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||
|
||||
/**
|
||||
* Test for generating a mapper which references nested types (static inner classes).
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
@WithClasses({
|
||||
Astronaut.class, Spaceship.class, AstronautDto.class, SpaceshipDto.class, SpaceshipMapper.class,
|
||||
AstronautMapper.class
|
||||
})
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
public class SourceTypeContainsCollectionWithExtendsBoundTest {
|
||||
|
||||
private final GeneratedSource generatedSource = new GeneratedSource();
|
||||
|
||||
@Rule
|
||||
public GeneratedSource getGeneratedSource() {
|
||||
return generatedSource;
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("768")
|
||||
public void generatesImportsForCollectionWithExtendsBoundInSourceType() {
|
||||
Astronaut astronaut = new Astronaut();
|
||||
astronaut.setName( "Bob" );
|
||||
|
||||
Spaceship spaceship = new Spaceship();
|
||||
spaceship.setAstronauts( Collections.singleton( astronaut ) );
|
||||
|
||||
SpaceshipDto spaceshipDto = SpaceshipMapper.INSTANCE.spaceshipToDto( spaceship );
|
||||
|
||||
assertThat( spaceshipDto ).isNotNull();
|
||||
assertThat( spaceshipDto.getAstronauts() ).onProperty( "name" ).containsOnly( "Bob" );
|
||||
|
||||
generatedSource.forMapper( SpaceshipMapper.class ).containsImportFor( Astronaut.class );
|
||||
generatedSource.forMapper( SpaceshipMapper.class ).containsImportFor( Spaceship.class );
|
||||
generatedSource.forMapper( SpaceshipMapper.class ).containsImportFor( AstronautDto.class );
|
||||
generatedSource.forMapper( SpaceshipMapper.class ).containsImportFor( SpaceshipDto.class );
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.astronautmapper.AstronautMapper;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.dto.SpaceshipDto;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.entity.Spaceship;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper(uses = AstronautMapper.class)
|
||||
public interface SpaceshipMapper {
|
||||
|
||||
SpaceshipMapper INSTANCE = Mappers.getMapper( SpaceshipMapper.class );
|
||||
|
||||
SpaceshipDto spaceshipToDto(Spaceship spaceship);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound.astronautmapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.dto.AstronautDto;
|
||||
import org.mapstruct.ap.test.imports.sourcewithextendsbound.entity.Astronaut;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface AstronautMapper {
|
||||
|
||||
AstronautMapper INSTANCE = Mappers.getMapper( AstronautMapper.class );
|
||||
|
||||
AstronautDto astronautToDto(Astronaut astronaut);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound.dto;
|
||||
|
||||
public class AstronautDto {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound.dto;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class SpaceshipDto {
|
||||
|
||||
private Collection<AstronautDto> astronauts;
|
||||
|
||||
public Collection<AstronautDto> getAstronauts() {
|
||||
return astronauts;
|
||||
}
|
||||
|
||||
public void setAstronauts(Collection<AstronautDto> astronauts) {
|
||||
this.astronauts = astronauts;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound.entity;
|
||||
|
||||
public class Astronaut {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright 2012-2016 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.sourcewithextendsbound.entity;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class Spaceship {
|
||||
|
||||
private Collection<? extends Astronaut> astronauts;
|
||||
|
||||
public Collection<? extends Astronaut> getAstronauts() {
|
||||
return astronauts;
|
||||
}
|
||||
|
||||
public void setAstronauts(Collection<? extends Astronaut> astronauts) {
|
||||
this.astronauts = astronauts;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user