#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:
Filip Hrisafov 2020-03-07 18:16:38 +01:00
parent b7d5e557c1
commit 4f76208c62
3 changed files with 66 additions and 1 deletions

View File

@ -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;
}

View File

@ -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 {
}
}

View File

@ -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() {
}
}