mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#265 adding imports to the Mapper to make expressions more easy.
This commit is contained in:
parent
8a173ba3db
commit
8f56d8dd67
@ -42,6 +42,15 @@ public @interface Mapper {
|
|||||||
*/
|
*/
|
||||||
Class<?>[] uses() default { };
|
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
|
* How unmapped properties of the target type of a mapping should be
|
||||||
* reported. The method overrides an unmappedTargetPolicy set in a central
|
* reported. The method overrides an unmappedTargetPolicy set in a central
|
||||||
|
@ -20,10 +20,10 @@ package org.mapstruct.ap.model;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.TreeSet;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Accessibility;
|
import org.mapstruct.ap.model.common.Accessibility;
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
@ -51,7 +51,8 @@ public class Decorator extends GeneratedType {
|
|||||||
methods,
|
methods,
|
||||||
fields,
|
fields,
|
||||||
suppressGeneratorTimestamp,
|
suppressGeneratorTimestamp,
|
||||||
accessibility
|
accessibility,
|
||||||
|
new TreeSet<Type>()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
private final List<Annotation> annotations;
|
private final List<Annotation> annotations;
|
||||||
private final List<MappingMethod> methods;
|
private final List<MappingMethod> methods;
|
||||||
private final List<? extends ModelElement> fields;
|
private final List<? extends ModelElement> fields;
|
||||||
|
private final SortedSet<Type> extraImportedTypes;
|
||||||
|
|
||||||
private final boolean suppressGeneratorTimestamp;
|
private final boolean suppressGeneratorTimestamp;
|
||||||
private final Accessibility accessibility;
|
private final Accessibility accessibility;
|
||||||
@ -58,11 +59,14 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
String interfaceName,
|
String interfaceName,
|
||||||
List<MappingMethod> methods,
|
List<MappingMethod> methods,
|
||||||
List<? extends ModelElement> fields,
|
List<? extends ModelElement> fields,
|
||||||
boolean suppressGeneratorTimestamp, Accessibility accessibility) {
|
boolean suppressGeneratorTimestamp,
|
||||||
|
Accessibility accessibility,
|
||||||
|
SortedSet<Type> extraImportedTypes) {
|
||||||
this.packageName = packageName;
|
this.packageName = packageName;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.superClassName = superClassName;
|
this.superClassName = superClassName;
|
||||||
this.interfaceName = interfaceName;
|
this.interfaceName = interfaceName;
|
||||||
|
this.extraImportedTypes = extraImportedTypes;
|
||||||
|
|
||||||
this.annotations = new ArrayList<Annotation>();
|
this.annotations = new ArrayList<Annotation>();
|
||||||
this.methods = methods;
|
this.methods = methods;
|
||||||
@ -135,6 +139,10 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
addWithDependents( importedTypes, annotation.getType() );
|
addWithDependents( importedTypes, annotation.getType() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for ( Type extraImport : extraImportedTypes ) {
|
||||||
|
addWithDependents( importedTypes, extraImport );
|
||||||
|
}
|
||||||
|
|
||||||
return importedTypes;
|
return importedTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,11 +19,12 @@
|
|||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.SortedSet;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Accessibility;
|
import org.mapstruct.ap.model.common.Accessibility;
|
||||||
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.common.TypeFactory;
|
import org.mapstruct.ap.model.common.TypeFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,10 +41,11 @@ public class Mapper extends GeneratedType {
|
|||||||
private final List<MapperReference> referencedMappers;
|
private final List<MapperReference> referencedMappers;
|
||||||
private final Decorator decorator;
|
private final Decorator decorator;
|
||||||
|
|
||||||
|
//CHECKSTYLE:OFF
|
||||||
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||||
String interfaceName,
|
String interfaceName, List<MappingMethod> methods, boolean suppressGeneratorTimestamp,
|
||||||
List<MappingMethod> methods, boolean suppressGeneratorTimestamp, Accessibility accessibility,
|
Accessibility accessibility, List<MapperReference> referencedMappers, Decorator decorator,
|
||||||
List<MapperReference> referencedMappers, Decorator decorator) {
|
SortedSet<Type> extraImportedTypes ) {
|
||||||
|
|
||||||
super(
|
super(
|
||||||
typeFactory,
|
typeFactory,
|
||||||
@ -54,7 +56,8 @@ public class Mapper extends GeneratedType {
|
|||||||
methods,
|
methods,
|
||||||
referencedMappers,
|
referencedMappers,
|
||||||
suppressGeneratorTimestamp,
|
suppressGeneratorTimestamp,
|
||||||
accessibility
|
accessibility,
|
||||||
|
extraImportedTypes
|
||||||
);
|
);
|
||||||
|
|
||||||
this.referencedMappers = referencedMappers;
|
this.referencedMappers = referencedMappers;
|
||||||
@ -67,6 +70,7 @@ public class Mapper extends GeneratedType {
|
|||||||
private TypeElement element;
|
private TypeElement element;
|
||||||
private List<MappingMethod> mappingMethods;
|
private List<MappingMethod> mappingMethods;
|
||||||
private List<MapperReference> mapperReferences;
|
private List<MapperReference> mapperReferences;
|
||||||
|
private SortedSet<Type> extraImportedTypes;
|
||||||
|
|
||||||
private Elements elementUtils;
|
private Elements elementUtils;
|
||||||
private boolean suppressGeneratorTimestamp;
|
private boolean suppressGeneratorTimestamp;
|
||||||
@ -107,6 +111,11 @@ public class Mapper extends GeneratedType {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
|
||||||
|
this.extraImportedTypes = extraImportedTypes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Mapper build() {
|
public Mapper build() {
|
||||||
String implementationName = element.getSimpleName()
|
String implementationName = element.getSimpleName()
|
||||||
+ ( decorator == null ? IMPLEMENTATION_SUFFIX : DECORATED_IMPLEMENTATION_SUFFIX );
|
+ ( decorator == null ? IMPLEMENTATION_SUFFIX : DECORATED_IMPLEMENTATION_SUFFIX );
|
||||||
@ -121,7 +130,8 @@ public class Mapper extends GeneratedType {
|
|||||||
suppressGeneratorTimestamp,
|
suppressGeneratorTimestamp,
|
||||||
Accessibility.fromModifiers( element.getModifiers() ),
|
Accessibility.fromModifiers( element.getModifiers() ),
|
||||||
mapperReferences,
|
mapperReferences,
|
||||||
decorator
|
decorator,
|
||||||
|
extraImportedTypes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,8 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeSet;
|
||||||
import javax.annotation.processing.Messager;
|
import javax.annotation.processing.Messager;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
@ -132,6 +134,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
.decorator( getDecorator( element, methods ) )
|
.decorator( getDecorator( element, methods ) )
|
||||||
.typeFactory( typeFactory )
|
.typeFactory( typeFactory )
|
||||||
.elementUtils( elementUtils )
|
.elementUtils( elementUtils )
|
||||||
|
.extraImports( getExtraImports( element ) )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return mapper;
|
return mapper;
|
||||||
@ -263,6 +266,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
return mapperReferences;
|
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,
|
private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<SourceMethod> methods,
|
||||||
TypeElement element) {
|
TypeElement element) {
|
||||||
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
|
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
|
||||||
|
@ -78,6 +78,10 @@ public class MapperConfig {
|
|||||||
return new ArrayList<TypeMirror>( uses );
|
return new ArrayList<TypeMirror>( uses );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TypeMirror> imports() {
|
||||||
|
return mapperPrism.imports();
|
||||||
|
}
|
||||||
|
|
||||||
public String unmappedTargetPolicy() {
|
public String unmappedTargetPolicy() {
|
||||||
if ( !ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() ).equals( ReportingPolicy.DEFAULT ) ) {
|
if ( !ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() ).equals( ReportingPolicy.DEFAULT ) ) {
|
||||||
// it is not the default configuration
|
// it is not the default configuration
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.source.expressions.java;
|
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.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.source.expressions.java;
|
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.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.MappingTarget;
|
import org.mapstruct.MappingTarget;
|
||||||
@ -27,22 +28,20 @@ import org.mapstruct.factory.Mappers;
|
|||||||
/**
|
/**
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper( imports = TimeAndFormat.class )
|
||||||
public interface SourceTargetMapper {
|
public interface SourceTargetMapper {
|
||||||
|
|
||||||
|
|
||||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
||||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
|
||||||
@Mapping( target = "anotherProp", ignore = true )
|
@Mapping( target = "anotherProp", ignore = true )
|
||||||
} )
|
} )
|
||||||
Target sourceToTarget( Source s );
|
Target sourceToTarget( Source s );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))"),
|
||||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
|
||||||
@Mapping( target = "anotherProp", ignore = true )
|
@Mapping( target = "anotherProp", ignore = true )
|
||||||
} )
|
} )
|
||||||
Target sourceToTargetWithMappingTarget(Source s, @MappingTarget Target t);
|
Target sourceToTargetWithMappingTarget(Source s, @MappingTarget Target t);
|
||||||
|
@ -21,20 +21,19 @@ package org.mapstruct.ap.test.source.expressions.java;
|
|||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.Mappings;
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper( imports = TimeAndFormat.class )
|
||||||
public interface SourceTargetMapperSeveralSources {
|
public interface SourceTargetMapperSeveralSources {
|
||||||
|
|
||||||
|
|
||||||
SourceTargetMapperSeveralSources INSTANCE = Mappers.getMapper( SourceTargetMapperSeveralSources.class );
|
SourceTargetMapperSeveralSources INSTANCE = Mappers.getMapper( SourceTargetMapperSeveralSources.class );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
@Mapping( target = "timeAndFormat", expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
||||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
|
||||||
@Mapping( source = "anotherProp", target = "anotherProp" )
|
@Mapping( source = "anotherProp", target = "anotherProp" )
|
||||||
} )
|
} )
|
||||||
Target sourceToTarget( Source s, Source2 s1 );
|
Target sourceToTarget( Source s, Source2 s1 );
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.source.expressions.java;
|
package org.mapstruct.ap.test.source.expressions.java;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
|
@ -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.source.expressions.java;
|
package org.mapstruct.ap.test.source.expressions.java.mapper;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user