#2 Generating import statements for used types; Restructuring "complex" test case to make use of imports

This commit is contained in:
Gunnar Morling 2013-05-14 23:32:14 +02:00
parent d88cc1086a
commit 289286cdda
11 changed files with 71 additions and 14 deletions

View File

@ -18,7 +18,11 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.Generated;
public class Mapper { public class Mapper {
@ -28,6 +32,7 @@ public class Mapper {
private final List<BeanMapping> beanMappings; private final List<BeanMapping> beanMappings;
private final List<Type> usedMapperTypes; private final List<Type> usedMapperTypes;
private final Options options; private final Options options;
private final SortedSet<Type> importedTypes;
public Mapper(String packageName, String interfaceName, public Mapper(String packageName, String interfaceName,
String implementationName, List<BeanMapping> beanMappings, List<Type> usedMapperTypes, Options options) { String implementationName, List<BeanMapping> beanMappings, List<Type> usedMapperTypes, Options options) {
@ -37,6 +42,40 @@ public class Mapper {
this.beanMappings = beanMappings; this.beanMappings = beanMappings;
this.usedMapperTypes = usedMapperTypes; this.usedMapperTypes = usedMapperTypes;
this.options = options; this.options = options;
this.importedTypes = determineImportedTypes();
}
private SortedSet<Type> determineImportedTypes() {
SortedSet<Type> importedTypes = new TreeSet<Type>();
importedTypes.add( Type.forClass( Generated.class ) );
for ( BeanMapping beanMapping : beanMappings ) {
addWithDependents( importedTypes, beanMapping.getSourceType() );
addWithDependents( importedTypes, beanMapping.getTargetType() );
for ( PropertyMapping propertyMapping : beanMapping.getPropertyMappings() ) {
addWithDependents( importedTypes, propertyMapping.getSourceType() );
addWithDependents( importedTypes, propertyMapping.getTargetType() );
}
}
return importedTypes;
}
private void addWithDependents(Collection<Type> collection, Type typeToAdd) {
if ( typeToAdd == null ) {
return;
}
if ( typeToAdd.getPackageName() != null &&
!typeToAdd.getPackageName().equals( packageName ) &&
!typeToAdd.getPackageName().startsWith( "java.lang" ) ) {
collection.add( typeToAdd );
}
addWithDependents( collection, typeToAdd.getCollectionImplementationType() );
addWithDependents( collection, typeToAdd.getIterableImplementationType() );
addWithDependents( collection, typeToAdd.getElementType() );
} }
@Override @Override
@ -81,4 +120,8 @@ public class Mapper {
public Options getOptions() { public Options getOptions() {
return options; return options;
} }
public SortedSet<Type> getImportedTypes() {
return importedTypes;
}
} }

View File

@ -32,7 +32,7 @@ import java.util.concurrent.ConcurrentMap;
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
public class Type { public class Type implements Comparable<Type> {
private final static Set<String> primitiveTypeNames = new HashSet<String>( private final static Set<String> primitiveTypeNames = new HashSet<String>(
Arrays.asList( "boolean", "char", "byte", "short", "int", "long", "float", "double" ) Arrays.asList( "boolean", "char", "byte", "short", "int", "long", "float", "double" )
@ -140,6 +140,10 @@ public class Type {
return isIterableType; return isIterableType;
} }
public String getFullyQualifiedName() {
return packageName == null ? name : packageName + "." + name;
}
@Override @Override
public String toString() { public String toString() {
if ( packageName == null ) { if ( packageName == null ) {
@ -203,4 +207,9 @@ public class Type {
} }
return true; return true;
} }
@Override
public int compareTo(Type o) {
return getFullyQualifiedName().compareTo( o.getFullyQualifiedName() );
}
} }

View File

@ -20,13 +20,9 @@
--> -->
package ${packageName}; package ${packageName};
import java.util.ArrayList; <#list importedTypes as importedType>
import java.util.Collection; import ${importedType.fullyQualifiedName};
import java.util.HashSet; </#list>
import java.util.List;
import java.util.Set;
import javax.annotation.Generated;
import java.util.Date;
@Generated( @Generated(
value = "org.mapstruct.ap.MappingProcessor"<#if options.suppressGeneratorTimestamp == false>, value = "org.mapstruct.ap.MappingProcessor"<#if options.suppressGeneratorTimestamp == false>,

View File

@ -37,7 +37,6 @@ import static org.fest.assertions.Assertions.assertThat;
}) })
public class DefaultCollectionImplementationTest extends MapperTestBase { public class DefaultCollectionImplementationTest extends MapperTestBase {
@Test @Test
@IssueKey("6") @IssueKey("6")
public void shouldUseDefaultImplementationForList() { public void shouldUseDefaultImplementationForList() {

View File

@ -24,6 +24,10 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mappers; import org.mapstruct.Mappers;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.ap.test.complex.source.Car;
import org.mapstruct.ap.test.complex.source.Person;
import org.mapstruct.ap.test.complex.target.CarDto;
import org.mapstruct.ap.test.complex.target.PersonDto;
@Mapper(uses = DateMapper.class) @Mapper(uses = DateMapper.class)
public interface CarMapper { public interface CarMapper {

View File

@ -23,6 +23,11 @@ import java.util.Arrays;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List; import java.util.List;
import org.mapstruct.ap.test.complex.source.Car;
import org.mapstruct.ap.test.complex.source.Category;
import org.mapstruct.ap.test.complex.source.Person;
import org.mapstruct.ap.test.complex.target.CarDto;
import org.mapstruct.ap.test.complex.target.PersonDto;
import org.mapstruct.ap.testutil.MapperTestBase; import org.mapstruct.ap.testutil.MapperTestBase;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.testng.annotations.Test; import org.testng.annotations.Test;

View File

@ -16,11 +16,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.test.complex; package org.mapstruct.ap.test.complex.source;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class Car { public class Car {
private String make; private String make;

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.test.complex; package org.mapstruct.ap.test.complex.source;
public enum Category { public enum Category {
SEDAN, CONVERTIBLE, TRUCK; SEDAN, CONVERTIBLE, TRUCK;

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.test.complex; package org.mapstruct.ap.test.complex.source;
public class Person { public class Person {

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.test.complex; package org.mapstruct.ap.test.complex.target;
import java.util.List; import java.util.List;

View File

@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.test.complex; package org.mapstruct.ap.test.complex.target;
public class PersonDto { public class PersonDto {