|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.intellij.inspection; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.LocalQuickFixOnPsiElement; |
| 9 | +import com.intellij.codeInspection.ProblemsHolder; |
| 10 | +import com.intellij.codeInspection.util.IntentionFamilyName; |
| 11 | +import com.intellij.codeInspection.util.IntentionName; |
| 12 | +import com.intellij.openapi.project.Project; |
| 13 | +import com.intellij.psi.JavaElementVisitor; |
| 14 | +import com.intellij.psi.PsiElement; |
| 15 | +import com.intellij.psi.PsiElementVisitor; |
| 16 | +import com.intellij.psi.PsiFile; |
| 17 | +import com.intellij.psi.PsiMethod; |
| 18 | +import com.intellij.psi.PsiParameter; |
| 19 | +import com.intellij.psi.PsiType; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | +import org.mapstruct.intellij.MapStructBundle; |
| 23 | +import org.mapstruct.intellij.util.MapStructVersion; |
| 24 | +import org.mapstruct.intellij.util.MapstructUtil; |
| 25 | + |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import static com.intellij.psi.PsiElementFactory.getInstance; |
| 29 | +import static org.mapstruct.intellij.util.MapstructUtil.getSourceParameters; |
| 30 | +import static org.mapstruct.intellij.util.SourceUtils.findAllDefinedMappingSources; |
| 31 | +import static org.mapstruct.intellij.util.SourceUtils.getGenericTypes; |
| 32 | +import static org.mapstruct.intellij.util.TargetUtils.findAllTargetProperties; |
| 33 | +import static org.mapstruct.intellij.util.TargetUtils.getTargetType; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author hduelme |
| 37 | + */ |
| 38 | +public class FromMapMappingMapTypeInspection extends InspectionBase { |
| 39 | + |
| 40 | + @NotNull |
| 41 | + @Override |
| 42 | + PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 43 | + return new MyJavaElementVisitor( holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ) ); |
| 44 | + } |
| 45 | + |
| 46 | + private static class MyJavaElementVisitor extends JavaElementVisitor { |
| 47 | + private final ProblemsHolder holder; |
| 48 | + private final MapStructVersion mapStructVersion; |
| 49 | + |
| 50 | + private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion) { |
| 51 | + this.holder = holder; |
| 52 | + this.mapStructVersion = mapStructVersion; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void visitMethod(@NotNull PsiMethod method) { |
| 57 | + super.visitMethod( method ); |
| 58 | + |
| 59 | + if (!MapstructUtil.isMapper( method.getContainingClass() ) ) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + PsiType targetType = getTargetType( method ); |
| 64 | + if (targetType == null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + PsiParameter fromMapMappingParameter = getFromMapMappingParameter( method ); |
| 69 | + if (fromMapMappingParameter == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + PsiType[] parameters = getGenericTypes( fromMapMappingParameter ); |
| 73 | + if (parameters == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion, method ); |
| 77 | + if ( allTargetProperties.contains( fromMapMappingParameter.getName() ) ) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if ( findAllDefinedMappingSources( method, mapStructVersion ) |
| 81 | + .anyMatch( source -> fromMapMappingParameter.getName().equals( source ) ) ) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (parameters.length == 0) { |
| 85 | + // handle raw type |
| 86 | + holder.registerProblem( fromMapMappingParameter, |
| 87 | + MapStructBundle.message( "inspection.wrong.map.mapping.map.type.raw" ), |
| 88 | + new ReplaceByStringStringMapTypeFix( fromMapMappingParameter ) ); |
| 89 | + } |
| 90 | + else if (parameters.length == 2) { |
| 91 | + // only if both parameters of the map are set |
| 92 | + PsiType keyParameter = parameters[0]; |
| 93 | + if ( !keyParameter.equalsToText( "java.lang.String" ) ) { |
| 94 | + // handle wrong map key type |
| 95 | + holder.registerProblem( fromMapMappingParameter, |
| 96 | + MapStructBundle.message( "inspection.wrong.map.mapping.map.key" ), |
| 97 | + new ReplaceMapKeyByStringTypeFix( fromMapMappingParameter ) ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Nullable |
| 103 | + private static PsiParameter getFromMapMappingParameter(@NotNull PsiMethod method) { |
| 104 | + PsiParameter[] sourceParameters = getSourceParameters( method ); |
| 105 | + if (sourceParameters.length == 1) { |
| 106 | + PsiParameter parameter = sourceParameters[0]; |
| 107 | + if (parameter != null && PsiType.getTypeByName( "java.util.Map", method.getProject(), |
| 108 | + method.getResolveScope() ).isAssignableFrom( parameter.getType() ) ) { |
| 109 | + return parameter; |
| 110 | + } |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + private static class ReplaceByStringStringMapTypeFix extends LocalQuickFixOnPsiElement { |
| 116 | + |
| 117 | + private final String text; |
| 118 | + |
| 119 | + private ReplaceByStringStringMapTypeFix(@NotNull PsiParameter element) { |
| 120 | + super( element ); |
| 121 | + this.text = MapStructBundle.message( "inspection.wrong.map.mapping.map.type.raw.set.default", |
| 122 | + element.getType().getPresentableText() ); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public @IntentionName @NotNull String getText() { |
| 127 | + return text; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, |
| 132 | + @NotNull PsiElement startElement, @NotNull PsiElement endElement) { |
| 133 | + if (!super.isAvailable( project, file, startElement, endElement ) ) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + if (startElement instanceof PsiParameter) { |
| 137 | + PsiParameter parameter = (PsiParameter) startElement; |
| 138 | + PsiType[] parameters = getGenericTypes( parameter ); |
| 139 | + return parameters != null && parameters.length == 0; |
| 140 | + } |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, |
| 146 | + @NotNull PsiElement psiElement1) { |
| 147 | + if (psiElement instanceof PsiParameter) { |
| 148 | + String mapText = psiElement.getText(); |
| 149 | + String prefix = mapText.substring( 0, mapText.indexOf( ' ' ) ); |
| 150 | + String end = mapText.substring( mapText.lastIndexOf( ' ' ) ); |
| 151 | + String result = prefix + "<String, String>" + end; |
| 152 | + psiElement.replace( getInstance( project ).createParameterFromText( result, psiElement ) ); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 158 | + return MapStructBundle.message( "intention.wrong.map.mapping.map.type.raw" ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static class ReplaceMapKeyByStringTypeFix extends LocalQuickFixOnPsiElement { |
| 163 | + |
| 164 | + private final String text; |
| 165 | + |
| 166 | + private ReplaceMapKeyByStringTypeFix(@NotNull PsiParameter element) { |
| 167 | + super( element ); |
| 168 | + this.text = MapStructBundle.message( "inspection.wrong.map.mapping.map.key.change.to.string" ); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public @IntentionName @NotNull String getText() { |
| 173 | + return text; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, |
| 178 | + @NotNull PsiElement startElement, @NotNull PsiElement endElement) { |
| 179 | + if (!super.isAvailable( project, file, startElement, endElement ) ) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + if (startElement instanceof PsiParameter) { |
| 183 | + PsiParameter parameter = (PsiParameter) startElement; |
| 184 | + PsiType[] parameters = getGenericTypes( parameter ); |
| 185 | + if (parameters == null || parameters.length != 2) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + return !parameters[0].equalsToText( "java.lang.String" ); |
| 189 | + } |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, |
| 195 | + @NotNull PsiElement psiElement1) { |
| 196 | + if (psiElement instanceof PsiParameter) { |
| 197 | + String mapText = psiElement.getText(); |
| 198 | + String prefix = mapText.substring( 0, mapText.indexOf( '<' ) + 1 ); |
| 199 | + String end = mapText.substring( mapText.indexOf( ',' ) ); |
| 200 | + String result = prefix + "String" + end; |
| 201 | + psiElement.replace( getInstance( project ).createParameterFromText( result, psiElement ) ); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 207 | + return MapStructBundle.message( "intention.wrong.map.mapping.map.key" ); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments