|
| 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.codeinsight.references; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import com.intellij.openapi.util.Pair; |
| 12 | +import com.intellij.openapi.util.TextRange; |
| 13 | +import com.intellij.psi.PsiClass; |
| 14 | +import com.intellij.psi.PsiElement; |
| 15 | +import com.intellij.psi.PsiField; |
| 16 | +import com.intellij.psi.PsiMethod; |
| 17 | +import com.intellij.psi.PsiParameter; |
| 18 | +import com.intellij.psi.PsiParameterList; |
| 19 | +import com.intellij.psi.PsiRecordComponent; |
| 20 | +import com.intellij.psi.PsiSubstitutor; |
| 21 | +import com.intellij.psi.PsiType; |
| 22 | +import com.intellij.psi.PsiVariable; |
| 23 | +import com.intellij.psi.util.PsiUtil; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | +import org.jetbrains.annotations.Nullable; |
| 26 | +import org.mapstruct.intellij.util.MapStructVersion; |
| 27 | +import org.mapstruct.intellij.util.MapstructUtil; |
| 28 | +import org.mapstruct.intellij.util.TargetType; |
| 29 | +import org.mapstruct.intellij.util.TargetUtils; |
| 30 | + |
| 31 | +import static org.mapstruct.intellij.util.MapstructUtil.asLookup; |
| 32 | +import static org.mapstruct.intellij.util.MapstructUtil.findRecordComponent; |
| 33 | +import static org.mapstruct.intellij.util.MapstructUtil.isPublicModifiable; |
| 34 | +import static org.mapstruct.intellij.util.MapstructUtil.isPublicNonStatic; |
| 35 | +import static org.mapstruct.intellij.util.TargetUtils.findAllDefinedMappingTargets; |
| 36 | +import static org.mapstruct.intellij.util.TargetUtils.findAllIgnoredTargets; |
| 37 | +import static org.mapstruct.intellij.util.TargetUtils.isBuilderEnabled; |
| 38 | +import static org.mapstruct.intellij.util.TargetUtils.publicWriteAccessors; |
| 39 | +import static org.mapstruct.intellij.util.TargetUtils.resolveBuilderOrSelfClass; |
| 40 | +import static org.mapstruct.intellij.util.TypeUtils.firstParameterPsiType; |
| 41 | + |
| 42 | +/** |
| 43 | + * Base class for target references ({@link MapstructTargetReference} and {@link MapstructIgnoredTargetReference}). |
| 44 | + * Provides shared implementations for resolving the type of target elements. |
| 45 | + * |
| 46 | + * @author Filip Hrisafov |
| 47 | + */ |
| 48 | +abstract class BaseTargetReference extends BaseMappingReference { |
| 49 | + |
| 50 | + protected final MapStructVersion mapStructVersion; |
| 51 | + |
| 52 | + BaseTargetReference(@NotNull PsiElement element, @Nullable MapstructBaseReference previousReference, |
| 53 | + TextRange rangeInElement, String value) { |
| 54 | + super( element, previousReference, rangeInElement, value ); |
| 55 | + mapStructVersion = MapstructUtil.resolveMapStructProjectVersion( element.getContainingFile() |
| 56 | + .getOriginalFile() ); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) { |
| 61 | + return resolveTargetElement( value, psiType, getMappingMethod() ); |
| 62 | + } |
| 63 | + |
| 64 | + PsiElement resolveTargetElement(@NotNull String value, @NotNull PsiType psiType, |
| 65 | + @Nullable PsiMethod mappingMethod) { |
| 66 | + boolean builderSupportPresent = mapStructVersion.isBuilderSupported(); |
| 67 | + Pair<PsiClass, TargetType> pair = resolveBuilderOrSelfClass( |
| 68 | + psiType, |
| 69 | + builderSupportPresent && isBuilderEnabled( mappingMethod ) |
| 70 | + ); |
| 71 | + if ( pair == null ) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + PsiClass psiClass = pair.getFirst(); |
| 76 | + TargetType targetType = pair.getSecond(); |
| 77 | + PsiType typeToUse = targetType.type(); |
| 78 | + |
| 79 | + PsiRecordComponent recordComponent = findRecordComponent( value, psiClass ); |
| 80 | + if ( recordComponent != null ) { |
| 81 | + return recordComponent; |
| 82 | + } |
| 83 | + |
| 84 | + if ( mapStructVersion.isConstructorSupported() && !targetType.builder() ) { |
| 85 | + PsiMethod constructor = TargetUtils.resolveMappingConstructor( psiClass ); |
| 86 | + if ( constructor != null && constructor.hasParameters() ) { |
| 87 | + for ( PsiParameter parameter : constructor.getParameterList().getParameters() ) { |
| 88 | + if ( value.equals( parameter.getName() ) ) { |
| 89 | + return parameter; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + String capitalizedName = MapstructUtil.capitalize( value ); |
| 96 | + PsiMethod[] methods = psiClass.findMethodsByName( "set" + capitalizedName, true ); |
| 97 | + if ( methods.length != 0 && isPublicNonStatic( methods[0] ) ) { |
| 98 | + return methods[0]; |
| 99 | + } |
| 100 | + |
| 101 | + // If there is no such setter we need to check if there is a collection getter |
| 102 | + methods = psiClass.findMethodsByName( "get" + capitalizedName, true ); |
| 103 | + if ( methods.length != 0 && isCollectionGetterWriteAccessor( methods[0] ) ) { |
| 104 | + return methods[0]; |
| 105 | + } |
| 106 | + |
| 107 | + if ( builderSupportPresent ) { |
| 108 | + for ( Pair<PsiMethod, PsiSubstitutor> builderPair : psiClass.findMethodsAndTheirSubstitutorsByName( |
| 109 | + value, |
| 110 | + true |
| 111 | + ) ) { |
| 112 | + PsiMethod method = builderPair.getFirst(); |
| 113 | + if ( method.getParameterList().getParametersCount() == 1 && |
| 114 | + mapstructUtil.isFluentSetter( method, typeToUse, builderPair.getSecond() ) ) { |
| 115 | + return method; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + PsiClass selfClass = PsiUtil.resolveClassInType( psiType ); |
| 121 | + if ( selfClass != null ) { |
| 122 | + PsiField field = selfClass.findFieldByName( value, true ); |
| 123 | + if ( field != null && isPublicModifiable( field ) ) { |
| 124 | + return field; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + @NotNull |
| 132 | + @Override |
| 133 | + Object[] getVariantsInternal(@NotNull PsiType psiType) { |
| 134 | + |
| 135 | + PsiMethod mappingMethod = getMappingMethod(); |
| 136 | + |
| 137 | + Map<String, Pair<? extends PsiElement, PsiSubstitutor>> accessors = publicWriteAccessors( |
| 138 | + psiType, |
| 139 | + mapStructVersion, |
| 140 | + mapstructUtil, |
| 141 | + mappingMethod |
| 142 | + ); |
| 143 | + |
| 144 | + if ( mappingMethod != null ) { |
| 145 | + findAllDefinedTargets( mappingMethod ).forEach( accessors::remove ); |
| 146 | + } |
| 147 | + |
| 148 | + return asLookup( |
| 149 | + accessors, |
| 150 | + BaseTargetReference::memberPsiType |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + protected Stream<String> findAllDefinedTargets(PsiMethod mappingMethod) { |
| 155 | + return Stream.concat( |
| 156 | + findAllDefinedMappingTargets( mappingMethod, mapStructVersion ), |
| 157 | + findAllIgnoredTargets( mappingMethod ) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + @Nullable |
| 162 | + @Override |
| 163 | + PsiType resolvedType() { |
| 164 | + PsiElement element = resolve(); |
| 165 | + if ( element instanceof PsiMethod psiMethod ) { |
| 166 | + return firstParameterPsiType( psiMethod ); |
| 167 | + } |
| 168 | + else if ( element instanceof PsiParameter psiParameter ) { |
| 169 | + return psiParameter.getType(); |
| 170 | + } |
| 171 | + else if ( element instanceof PsiRecordComponent psiRecordComponent ) { |
| 172 | + return psiRecordComponent.getType(); |
| 173 | + } |
| 174 | + else if ( element instanceof PsiField psiField ) { |
| 175 | + return psiField.getType(); |
| 176 | + } |
| 177 | + |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
| 181 | + static PsiType memberPsiType(PsiElement psiMember) { |
| 182 | + if ( psiMember instanceof PsiMethod psiMemberMethod ) { |
| 183 | + return resolveMethodType( psiMemberMethod ); |
| 184 | + } |
| 185 | + else if ( psiMember instanceof PsiVariable psiMemberVariable ) { |
| 186 | + return psiMemberVariable.getType(); |
| 187 | + } |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + static PsiType resolveMethodType(PsiMethod psiMethod) { |
| 192 | + PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters(); |
| 193 | + if ( psiParameters.length == 0 ) { |
| 194 | + return psiMethod.getReturnType(); |
| 195 | + } |
| 196 | + return psiParameters[0].getType(); |
| 197 | + } |
| 198 | + |
| 199 | + private static boolean isCollectionGetterWriteAccessor(@NotNull PsiMethod method) { |
| 200 | + if ( !isPublicNonStatic( method ) ) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + PsiParameterList parameterList = method.getParameterList(); |
| 204 | + if ( parameterList.getParametersCount() > 0 ) { |
| 205 | + return false; |
| 206 | + } |
| 207 | + return TargetUtils.isMethodReturnTypeAssignableToCollectionOrMap( method ); |
| 208 | + } |
| 209 | +} |
0 commit comments