#265 adding imports to the Mapper to make expressions more easy.

This commit is contained in:
sjaakd 2014-08-19 19:42:25 +02:00
parent 8a173ba3db
commit 8f56d8dd67
11 changed files with 73 additions and 23 deletions

View File

@ -42,6 +42,15 @@ public @interface Mapper {
*/
Class<?>[] uses() default { };
/**
* Additional types for which an import statement is to be added to the generated mapper implementation class.
* This allows to refer to those types from within mapping expressions given via {@link #expression()} using
* their simple name rather than their fully-qualified name.
*
* @return classes to add in the imports of the generated implementation.
*/
Class<?>[] imports() default { };
/**
* How unmapped properties of the target type of a mapping should be
* reported. The method overrides an unmappedTargetPolicy set in a central

View File

@ -20,10 +20,10 @@ package org.mapstruct.ap.model;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import org.mapstruct.ap.model.common.Accessibility;
import org.mapstruct.ap.model.common.ModelElement;
import org.mapstruct.ap.model.common.Type;
@ -41,7 +41,7 @@ public class Decorator extends GeneratedType {
private Decorator(TypeFactory typeFactory, String packageName, String name, String superClassName,
String interfaceName, List<MappingMethod> methods, List<? extends ModelElement> fields,
boolean suppressGeneratorTimestamp, Accessibility accessibility) {
boolean suppressGeneratorTimestamp, Accessibility accessibility ) {
super(
typeFactory,
packageName,
@ -51,7 +51,8 @@ public class Decorator extends GeneratedType {
methods,
fields,
suppressGeneratorTimestamp,
accessibility
accessibility,
new TreeSet<Type>()
);
}

View File

@ -45,6 +45,7 @@ public abstract class GeneratedType extends ModelElement {
private final List<Annotation> annotations;
private final List<MappingMethod> methods;
private final List<? extends ModelElement> fields;
private final SortedSet<Type> extraImportedTypes;
private final boolean suppressGeneratorTimestamp;
private final Accessibility accessibility;
@ -58,11 +59,14 @@ public abstract class GeneratedType extends ModelElement {
String interfaceName,
List<MappingMethod> methods,
List<? extends ModelElement> fields,
boolean suppressGeneratorTimestamp, Accessibility accessibility) {
boolean suppressGeneratorTimestamp,
Accessibility accessibility,
SortedSet<Type> extraImportedTypes) {
this.packageName = packageName;
this.name = name;
this.superClassName = superClassName;
this.interfaceName = interfaceName;
this.extraImportedTypes = extraImportedTypes;
this.annotations = new ArrayList<Annotation>();
this.methods = methods;
@ -135,6 +139,10 @@ public abstract class GeneratedType extends ModelElement {
addWithDependents( importedTypes, annotation.getType() );
}
for ( Type extraImport : extraImportedTypes ) {
addWithDependents( importedTypes, extraImport );
}
return importedTypes;
}

View File

@ -19,11 +19,12 @@
package org.mapstruct.ap.model;
import java.util.List;
import java.util.SortedSet;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import org.mapstruct.ap.model.common.Accessibility;
import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
/**
@ -40,10 +41,11 @@ public class Mapper extends GeneratedType {
private final List<MapperReference> referencedMappers;
private final Decorator decorator;
//CHECKSTYLE:OFF
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
String interfaceName,
List<MappingMethod> methods, boolean suppressGeneratorTimestamp, Accessibility accessibility,
List<MapperReference> referencedMappers, Decorator decorator) {
String interfaceName, List<MappingMethod> methods, boolean suppressGeneratorTimestamp,
Accessibility accessibility, List<MapperReference> referencedMappers, Decorator decorator,
SortedSet<Type> extraImportedTypes ) {
super(
typeFactory,
@ -54,7 +56,8 @@ public class Mapper extends GeneratedType {
methods,
referencedMappers,
suppressGeneratorTimestamp,
accessibility
accessibility,
extraImportedTypes
);
this.referencedMappers = referencedMappers;
@ -67,6 +70,7 @@ public class Mapper extends GeneratedType {
private TypeElement element;
private List<MappingMethod> mappingMethods;
private List<MapperReference> mapperReferences;
private SortedSet<Type> extraImportedTypes;
private Elements elementUtils;
private boolean suppressGeneratorTimestamp;
@ -107,6 +111,11 @@ public class Mapper extends GeneratedType {
return this;
}
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
this.extraImportedTypes = extraImportedTypes;
return this;
}
public Mapper build() {
String implementationName = element.getSimpleName()
+ ( decorator == null ? IMPLEMENTATION_SUFFIX : DECORATED_IMPLEMENTATION_SUFFIX );
@ -121,7 +130,8 @@ public class Mapper extends GeneratedType {
suppressGeneratorTimestamp,
Accessibility.fromModifiers( element.getModifiers() ),
mapperReferences,
decorator
decorator,
extraImportedTypes
);
}
}

View File

@ -26,6 +26,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
@ -132,6 +134,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
.decorator( getDecorator( element, methods ) )
.typeFactory( typeFactory )
.elementUtils( elementUtils )
.extraImports( getExtraImports( element ) )
.build();
return mapper;
@ -242,7 +245,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
);
}
private List<MapperReference> getReferencedMappers(TypeElement element) {
private List<MapperReference> getReferencedMappers(TypeElement element) {
List<MapperReference> mapperReferences = new LinkedList<MapperReference>();
List<String> variableNames = new LinkedList<String>();
@ -263,6 +266,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
return mapperReferences;
}
private SortedSet<Type> getExtraImports(TypeElement element) {
SortedSet<Type> extraImports = new TreeSet<Type>();
MapperConfig mapperPrism = MapperConfig.getInstanceOn( element );
for ( TypeMirror extraImport : mapperPrism.imports() ) {
Type type = typeFactory.getType( extraImport );
extraImports.add( type );
}
return extraImports;
}
private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<SourceMethod> methods,
TypeElement element) {
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();

View File

@ -78,6 +78,10 @@ public class MapperConfig {
return new ArrayList<TypeMirror>( uses );
}
public List<TypeMirror> imports() {
return mapperPrism.imports();
}
public String unmappedTargetPolicy() {
if ( !ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() ).equals( ReportingPolicy.DEFAULT ) ) {
// it is not the default configuration

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.test.source.expressions.java;
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.test.source.expressions.java;
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
@ -27,22 +28,20 @@ import org.mapstruct.factory.Mappers;
/**
* @author Sjaak Derksen
*/
@Mapper
@Mapper( imports = TimeAndFormat.class )
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mappings( {
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( target = "anotherProp", ignore = true )
} )
Target sourceToTarget(Source s);
Target sourceToTarget( Source s );
@Mappings( {
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))"),
@Mapping( target = "anotherProp", ignore = true )
} )
Target sourceToTargetWithMappingTarget(Source s, @MappingTarget Target t);

View File

@ -21,21 +21,20 @@ package org.mapstruct.ap.test.source.expressions.java;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
import org.mapstruct.factory.Mappers;
/**
* @author Sjaak Derksen
*/
@Mapper
@Mapper( imports = TimeAndFormat.class )
public interface SourceTargetMapperSeveralSources {
SourceTargetMapperSeveralSources INSTANCE = Mappers.getMapper( SourceTargetMapperSeveralSources.class );
@Mappings( {
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( source = "anotherProp", target = "anotherProp" )
} )
Target sourceToTarget(Source s, Source2 s1);
Target sourceToTarget( Source s, Source2 s1 );
}

View File

@ -18,6 +18,8 @@
*/
package org.mapstruct.ap.test.source.expressions.java;
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
/**
* @author Sjaak Derksen
*/

View File

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