Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find disabled builder in @MapperConfig #97

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/org/mapstruct/intellij/util/TargetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassObjectAccessExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiMember;
Expand Down Expand Up @@ -169,6 +170,23 @@ private static Optional<Boolean> findDisableBuilder(@Nullable PsiModifierListOwn
return Optional.ofNullable( disableBuilder );
}
}
else {
PsiNameValuePair configAttribute =
AnnotationUtil.findDeclaredAttribute( requestedAnnotation, "config" );
if ( configAttribute != null ) {
PsiAnnotationMemberValue configValue = configAttribute.getValue();

if ( configValue != null ) {
PsiJavaCodeReferenceElement referenceElement =
((PsiClassObjectAccessExpression) configValue).getOperand()
.getInnermostComponentReferenceElement();
if (referenceElement != null) {
return findDisableBuilder( (PsiModifierListOwner) referenceElement.resolve(),
"org.mapstruct.MapperConfig" );
}
}
}
}
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public void testUnmappedTargetPropertiesWithBuilder() {
"Add unmapped target property: 'targetTestName'",

"Ignore unmapped target property: 'builderTestName'",
"Add unmapped target property: 'builderTestName'"
"Add unmapped target property: 'builderTestName'",

"Ignore unmapped target property: 'targetTestName'",
"Add unmapped target property: 'targetTestName'"
);

allQuickFixes.forEach( myFixture::launchAction );
Expand Down
12 changes: 12 additions & 0 deletions testData/inspection/UnmappedTargetPropertiesWithBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.mapstruct.BeanMapping;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping;
import org.example.data.UnmappedTargetPropertiesData.Target;

Expand Down Expand Up @@ -35,3 +36,14 @@ interface MapperDisabledBuilderBeanMappingEnabledBuilder {
@BeanMapping(builder = @Builder(disableBuilder = false))
Target <warning descr="Unmapped target property: builderTestName">map</warning>(String source);
}

@MapperConfig(builder = @Builder(disableBuilder = true))
class DoNotUseBuilderMapperConfig {

}

@Mapper(config = DoNotUseBuilderMapperConfig.class)
interface MapperConfigDisabledBuilder {

Target <warning descr="Unmapped target property: targetTestName">map</warning>(String source);
}
14 changes: 14 additions & 0 deletions testData/inspection/UnmappedTargetPropertiesWithBuilder_after.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.mapstruct.BeanMapping;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping;
import org.example.data.UnmappedTargetPropertiesData.Target;

Expand Down Expand Up @@ -43,3 +44,16 @@ interface MapperDisabledBuilderBeanMappingEnabledBuilder {
@BeanMapping(builder = @Builder(disableBuilder = false))
Target map(String source);
}

@MapperConfig(builder = @Builder(disableBuilder = true))
class DoNotUseBuilderMapperConfig {

}

@Mapper(config = DoNotUseBuilderMapperConfig.class)
interface MapperConfigDisabledBuilder {

@Mapping(target = "targetTestName", source = "")
@Mapping(target = "targetTestName", ignore = true)
Target map(String source);
}