mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2021 Generate compilable code when Decorator is nested within the Mapper
We need to treat the import of the decoratorType specially when it is nested. Calling addIfImportRequired is not the most correct approach since it would lead to checking if the type is to be imported and that would be false since the Decorator is a nested class within the Mapper. However, when generating the Decorator this is not needed, because the Decorator is a top level class itself In a nutshell creating the Decorator should have its own ProcessorContext, but it doesn't
This commit is contained in:
parent
b7d5e557c1
commit
4f76208c62
@ -125,7 +125,22 @@ public class Decorator extends GeneratedType {
|
||||
@Override
|
||||
public SortedSet<Type> getImportTypes() {
|
||||
SortedSet<Type> importTypes = super.getImportTypes();
|
||||
addIfImportRequired( importTypes, decoratorType );
|
||||
// DecoratorType needs special handling in case it is nested
|
||||
// calling addIfImportRequired is not the most correct approach since it would
|
||||
// lead to checking if the type is to be imported and that would be false
|
||||
// since the Decorator is a nested class within the Mapper.
|
||||
// However, when generating the Decorator this is not needed,
|
||||
// because the Decorator is a top level class itself
|
||||
// In a nutshell creating the Decorator should have its own ProcessorContext, but it doesn't
|
||||
if ( decoratorType.getPackageName().equalsIgnoreCase( getPackageName() ) ) {
|
||||
if ( decoratorType.getTypeElement() != null &&
|
||||
decoratorType.getTypeElement().getNestingKind().isNested() ) {
|
||||
importTypes.add( decoratorType );
|
||||
}
|
||||
}
|
||||
else {
|
||||
importTypes.add( decoratorType );
|
||||
}
|
||||
return importTypes;
|
||||
}
|
||||
|
||||
|
@ -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._2021;
|
||||
|
||||
import org.mapstruct.DecoratedWith;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper
|
||||
@DecoratedWith(Issue2021Mapper.Decorator.class)
|
||||
public interface Issue2021Mapper {
|
||||
|
||||
abstract class Decorator implements Issue2021Mapper {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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._2021;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@IssueKey("2021")
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@WithClasses({
|
||||
Issue2021Mapper.class
|
||||
})
|
||||
public class Issue2021Test {
|
||||
|
||||
@Test
|
||||
public void shouldCompile() {
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user