Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class BaseReference extends PsiReferenceBase<PsiElement> impleme
* @return The mapping method that this reference belongs to
*/
@Nullable
PsiMethod getMappingMethod() {
public PsiMethod getMappingMethod() {
PsiElement element = getElement();
UExpression expression = UastContextKt.toUElement( element, UExpression.class );
if ( expression != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final PsiElement resolve() {

@Override
@Nullable
PsiMethod getMappingMethod() {
public PsiMethod getMappingMethod() {
PsiMethod mappingMethod = super.getMappingMethod();
if ( isNotValueMapping( mappingMethod ) ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public final PsiElement resolve() {
}

if ( previous != null ) {
PsiType psiType = canDescendIntoType( previous.resolvedType() ) ? previous.resolvedType() : null;
PsiType previousResolvedType = previous.resolvedType();
PsiType psiType = supportsNestedReferenceForType( previousResolvedType ) ? previousResolvedType : null;
return psiType == null ? null : resolveInternal( value, psiType );
}

Expand All @@ -78,6 +79,10 @@ public final PsiElement resolve() {
return mappingMethod == null ? null : resolveInternal( value, mappingMethod );
}

protected boolean supportsNestedReferenceForType(@Nullable PsiType psiType) {
return canDescendIntoType( psiType );
}

/**
* Resolved the reference from the {@code value} for the reference {@code psiClass}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
return null;
}

if ( MapstructUtil.isMapWithStringKeyType( psiType ) ) {
return psiClass;
}

PsiRecordComponent recordComponent = findRecordComponent( value, psiClass );
if ( recordComponent != null ) {
return recordComponent;
Expand All @@ -78,6 +82,11 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
return null;
}

@Override
protected boolean supportsNestedReferenceForType(@Nullable PsiType psiType) {
return super.supportsNestedReferenceForType( psiType ) || MapstructUtil.isMapWithStringKeyType( psiType );
}

@Override
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) {
PsiParameter[] sourceParameters = MapstructUtil.getSourceParameters( mappingMethod );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.intellij.codeinsight.references.BaseReference;
import org.mapstruct.intellij.codeinsight.references.BaseValueMappingReference;
import org.mapstruct.intellij.util.MapstructUtil;

/**
* Inspection that checks if mapstruct references can be resolved.
* @see BaseReference
*
* @author hduelme
* @see BaseReference
*/
public class MapstructReferenceInspection extends InspectionBase {

@Override
@NotNull PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new MapstructReferenceVisitor(holder);
return new MapstructReferenceVisitor( holder );
}

private static class MapstructReferenceVisitor extends PsiElementVisitor {
Expand All @@ -50,8 +56,10 @@ public void visitElement(@NotNull PsiElement element) {
TextRange range = psiReference.getRangeInElement();
if ( range.isEmpty() && range.getStartOffset() == 1 && "\"\"".equals( element.getText() ) ) {
String message = ProblemsHolder.unresolvedReferenceMessage( baseReference );
holder.registerProblem( element, message, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
TextRange.create( 0, 2 ) );
holder.registerProblem(
element, message, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
TextRange.create( 0, 2 )
);
}
else if ( shouldRegisterProblem( baseReference ) ) {
holder.registerProblem( psiReference );
Expand All @@ -67,9 +75,28 @@ private boolean shouldRegisterProblem(BaseReference reference) {
return valueMappingReference.getEnumClass() != null;
}

if ( hasSingleStringKeyMapSourceParameter( reference.getMappingMethod() ) ) {
// MapStruct allows source values as map keys, even if they are not resolvable as Java properties.
// Therefore, we don't report an unresolved reference problem here.
return false;
}

return !containingClassIsAnnotationType( reference.getElement() );
}

private boolean hasSingleStringKeyMapSourceParameter(@Nullable PsiMethod mappingMethod) {

if ( mappingMethod != null ) {
PsiParameter[] parameters = MapstructUtil.getSourceParameters( mappingMethod );
if ( parameters.length == 1 ) {
PsiType parameterType = parameters[0].getType();
return MapstructUtil.isMapWithStringKeyType( parameterType );
}
}

return false;
}

private boolean containingClassIsAnnotationType(PsiElement element) {

PsiClass containingClass = PsiTreeUtil.getParentOfType( element, PsiClass.class );
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/mapstruct/intellij/util/MapstructUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,33 @@ public static boolean isInheritInverseConfiguration(PsiMethod method) {
return isAnnotated( method, INHERIT_INVERSE_CONFIGURATION_FQN, AnnotationUtil.CHECK_TYPE );
}

/**
* Checks if the given type is a {@link java.util.Map} with a {@link String} key type.
*
* @param type to be checked
* @return {@code true} if the {@code type} is a {@link java.util.Map} with a {@link String} key type,
* {@code false} otherwise
*/
public static boolean isMapWithStringKeyType(@Nullable PsiType type) {
if ( type == null ) {
return false;
}

PsiClass psiClass = PsiUtil.resolveClassInType( type );
if ( psiClass == null ||
!CommonClassNames.JAVA_UTIL_MAP.equals( psiClass.getQualifiedName() ) ) {
return false;
}

if ( !( type instanceof PsiClassType ct ) ) {
return false;
}

PsiType[] parameters = ct.getParameters();
if ( parameters.length != 2 ) {
return false;
}

return parameters[0].equalsToText( CommonClassNames.JAVA_LANG_STRING );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.intellij.bugs._243;

import org.jetbrains.annotations.NotNull;
import org.mapstruct.intellij.inspection.BaseInspectionTest;
import org.mapstruct.intellij.inspection.MapstructReferenceInspection;

/**
* @author Oliver Erhart
*/
public class DisableSourcePropertyInspectionOnMapTest extends BaseInspectionTest {

@Override
protected String getTestDataPath() {
return "testData/bugs/_243";
}

@NotNull
@Override
protected Class<MapstructReferenceInspection> getInspection() {
return MapstructReferenceInspection.class;
}

public void testDisableSourcePropertyInspectionOnMap() {
doTest();
}

public void testDisableSourcePropertyInspectionOnMapErroneous() {
doTest();
}
}
38 changes: 38 additions & 0 deletions testData/bugs/_243/DisableSourcePropertyInspectionOnMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/

import java.time.LocalDate;
import java.util.Map;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper
abstract class Issue243Mapper {

@Mapping(source = "exDate", target = "exDate")
@Mapping(source = "payDate", target = "payDate")
public abstract CorporateAction mapWithStringKeyMap(Map<String, String> rowValues);

@Mapping(source = "exDate", target = "exDate")
public abstract void updateWithStringKeyMap(Map<String, String> rowValues, @MappingTarget CorporateAction target);

@Mapping(source = "exDate", target = "exDate")
public abstract CorporateAction mapWithObjectValueMap(Map<String, Object> rowValues);

@Mapping(source = "rowValues.exDate", target = "exDate")
@Mapping(source = "payDate", target = "payDate")
public abstract CorporateAction mapWithMultipleSourcesAndMapName(
Map<String, String> rowValues,
LocalDate payDate
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test looks correct.

I think it would be cleaner to split this into separate custom tests. Typically, erroneous and non-erroneous cases aren’t mixed together in the same test.

}

class CorporateAction {
public LocalDate exDate;
public LocalDate payDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/

import java.time.LocalDate;
import java.util.Map;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper
abstract class Issue243ErroneousMapper {

@Mapping(source = "<error descr="Unknown property 'exDate'">exDate</error>", target = "exDate")
public abstract CorporateAction mapWithIntegerKeyMap(Map<Integer, String> rowValues);

@Mapping(source = "<error descr="Unknown property 'exDate'">exDate</error>", target = "exDate")
public abstract void updateWithIntegerKeyMap(
Map<Integer, String> rowValues,
@MappingTarget CorporateAction target
);

@Mapping(source = "<error descr="Unknown property 'exDate'">exDate</error>", target = "exDate")
@Mapping(source = "payDate", target = "payDate")
public abstract CorporateAction mapWithMultipleSources(
Map<String, String> rowValues,
LocalDate payDate
);
}

class CorporateAction {
public LocalDate exDate;
public LocalDate payDate;
}
Loading