mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2897 Always import types defined in Mapper#imports
This commit is contained in:
parent
febed7ea1c
commit
082b36a50a
@ -193,7 +193,24 @@ public class TypeFactory {
|
|||||||
return getType( mirror, false );
|
return getType( mirror, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a type that is always going to be imported.
|
||||||
|
* This is useful when using it in {@code Mapper#imports}
|
||||||
|
* for types that should be used in expressions.
|
||||||
|
*
|
||||||
|
* @param mirror the type mirror for which we need a type
|
||||||
|
*
|
||||||
|
* @return the type
|
||||||
|
*/
|
||||||
|
public Type getAlwaysImportedType(TypeMirror mirror) {
|
||||||
|
return getType( mirror, false, true );
|
||||||
|
}
|
||||||
|
|
||||||
private Type getType(TypeMirror mirror, boolean isLiteral) {
|
private Type getType(TypeMirror mirror, boolean isLiteral) {
|
||||||
|
return getType( mirror, isLiteral, null );
|
||||||
|
}
|
||||||
|
|
||||||
|
private Type getType(TypeMirror mirror, boolean isLiteral, Boolean alwaysImport) {
|
||||||
if ( !canBeProcessed( mirror ) ) {
|
if ( !canBeProcessed( mirror ) ) {
|
||||||
throw new TypeHierarchyErroneousException( mirror );
|
throw new TypeHierarchyErroneousException( mirror );
|
||||||
}
|
}
|
||||||
@ -212,7 +229,7 @@ public class TypeFactory {
|
|||||||
String qualifiedName;
|
String qualifiedName;
|
||||||
TypeElement typeElement;
|
TypeElement typeElement;
|
||||||
Type componentType;
|
Type componentType;
|
||||||
Boolean toBeImported = null;
|
Boolean toBeImported = alwaysImport;
|
||||||
|
|
||||||
if ( mirror.getKind() == TypeKind.DECLARED ) {
|
if ( mirror.getKind() == TypeKind.DECLARED ) {
|
||||||
DeclaredType declaredType = (DeclaredType) mirror;
|
DeclaredType declaredType = (DeclaredType) mirror;
|
||||||
|
@ -301,7 +301,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
|
|
||||||
|
|
||||||
for ( TypeMirror extraImport : mapperOptions.imports() ) {
|
for ( TypeMirror extraImport : mapperOptions.imports() ) {
|
||||||
Type type = typeFactory.getType( extraImport );
|
Type type = typeFactory.getAlwaysImportedType( extraImport );
|
||||||
extraImports.add( type );
|
extraImports.add( type );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2897;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.ap.test.bugs._2897.util.Util;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(imports = Util.Factory.class)
|
||||||
|
public interface Issue2897Mapper {
|
||||||
|
|
||||||
|
Issue2897Mapper INSTANCE = Mappers.getMapper( Issue2897Mapper.class );
|
||||||
|
|
||||||
|
@Mapping( target = "value", expression = "java(Factory.parse( source ))")
|
||||||
|
Target map(Source source);
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2897;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.test.bugs._2897.util.Util;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2897")
|
||||||
|
@WithClasses({
|
||||||
|
Util.class,
|
||||||
|
Issue2897Mapper.class,
|
||||||
|
Source.class,
|
||||||
|
Target.class,
|
||||||
|
})
|
||||||
|
class Issue2897Test {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
void shouldImportNestedClassInMapperImports() {
|
||||||
|
Target target = Issue2897Mapper.INSTANCE.map( new Source( "test" ) );
|
||||||
|
|
||||||
|
assertThat( target.getValue() ).isEqualTo( "parsed(test)" );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2897;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Source {
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public Source(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2897;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Target {
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public Target(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2897.util;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.test.bugs._2897.Source;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Util {
|
||||||
|
|
||||||
|
public static class Factory {
|
||||||
|
|
||||||
|
public static String parse(Source source) {
|
||||||
|
return source == null ? null : "parsed(" + source.getValue() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user