|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.intellij.util; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Optional; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import com.intellij.psi.PsiAnnotation; |
| 17 | +import com.intellij.psi.PsiClass; |
| 18 | +import com.intellij.psi.PsiMethod; |
| 19 | +import com.intellij.psi.PsiModifierListOwner; |
| 20 | +import com.intellij.psi.PsiParameter; |
| 21 | +import com.intellij.psi.PsiParameterList; |
| 22 | +import com.intellij.psi.PsiType; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | + |
| 26 | +import static com.intellij.codeInsight.AnnotationUtil.getStringAttributeValue; |
| 27 | +import static org.mapstruct.intellij.util.MapstructAnnotationUtils.findMapperConfigReference; |
| 28 | +import static org.mapstruct.intellij.util.MapstructAnnotationUtils.findReferencedMapperClasses; |
| 29 | + |
| 30 | +/** |
| 31 | + * Utils for working with inherited configurations based on {@link org.mapstruct.InheritConfiguration} |
| 32 | + */ |
| 33 | +public class InheritConfigurationUtils { |
| 34 | + |
| 35 | + private InheritConfigurationUtils() { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Find all mapping methods (regardless of their type and parameter) that are in the scope to be inherited from: |
| 40 | + * <ul> |
| 41 | + * <li>local methods</li> |
| 42 | + * <li>methods of mappers extending from</li> |
| 43 | + * <li>methods in referenced mappers by {@link org.mapstruct.Mapper#uses()}</li> |
| 44 | + * <li>methods from {@link org.mapstruct.MapperConfig}</li> |
| 45 | + * <li>methods from referenced mappers from {@link org.mapstruct.MapperConfig#uses()}</li> |
| 46 | + * </ul> |
| 47 | + * |
| 48 | + * @param containingClass containing class of the mapping method to check |
| 49 | + * @param mapperAnnotation the mapper annotation of the containing class |
| 50 | + * @return possible mapping methods to be inherited from |
| 51 | + */ |
| 52 | + public static Stream<PsiMethod> findMappingMethodsFromInheritScope(@NotNull PsiClass containingClass, |
| 53 | + @NotNull PsiAnnotation mapperAnnotation) { |
| 54 | + |
| 55 | + Stream<PsiMethod> localAndParentMethods = Arrays.stream( containingClass.getAllMethods() ); |
| 56 | + |
| 57 | + Stream<PsiMethod> referencedMethods = findReferencedMapperClasses( mapperAnnotation ) |
| 58 | + .flatMap( c -> Arrays.stream( c.getMethods() ) ); |
| 59 | + |
| 60 | + Stream<PsiMethod> mapperConfigMethods = findMapperConfigMethods( mapperAnnotation ); |
| 61 | + |
| 62 | + return Stream.concat( Stream.concat( localAndParentMethods, referencedMethods ), mapperConfigMethods ); |
| 63 | + } |
| 64 | + |
| 65 | + private static Stream<PsiMethod> findMapperConfigMethods(@NotNull PsiAnnotation mapperAnnotation) { |
| 66 | + |
| 67 | + PsiModifierListOwner mapperConfigReference = findMapperConfigReference( mapperAnnotation ); |
| 68 | + |
| 69 | + if ( !( mapperConfigReference instanceof PsiClass ) ) { |
| 70 | + return Stream.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + return Arrays.stream( ( (PsiClass) mapperConfigReference ).getMethods() ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Find a candidate that this mapping method can inherit from, only if it is the only one possible. |
| 78 | + * |
| 79 | + * @param mappingMethod the mapping method to find a belonging method |
| 80 | + * @param candidates mapping methods that possibly hold the method to inherit from |
| 81 | + * @param inheritConfigurationAnnotation the {@link org.mapstruct.InheritConfiguration} annotation |
| 82 | + * @return a mapping method to inherit from, only if it is the only possible one found. |
| 83 | + */ |
| 84 | + public static Optional<PsiMethod> findSingleMatchingInheritMappingMethod( |
| 85 | + @NotNull PsiMethod mappingMethod, |
| 86 | + @NotNull Stream<PsiMethod> candidates, |
| 87 | + @NotNull PsiAnnotation inheritConfigurationAnnotation) { |
| 88 | + |
| 89 | + String inheritConfigurationName = getStringAttributeValue( inheritConfigurationAnnotation, "name" ); |
| 90 | + |
| 91 | + PsiType targetType = findTargetTypeOfMappingMethod( mappingMethod ); |
| 92 | + |
| 93 | + List<PsiMethod> matchingCandidates = candidates |
| 94 | + .filter( candidate -> isNotTheSameMethod( mappingMethod, candidate ) ) |
| 95 | + .filter( candidate -> matchesNameWhenNameIsDefined( inheritConfigurationName, candidate ) ) |
| 96 | + .filter( candidate -> canInheritFrom( mappingMethod, targetType, candidate ) ) |
| 97 | + .collect( Collectors.toList() ); |
| 98 | + |
| 99 | + if ( matchingCandidates.size() == 1 ) { |
| 100 | + return Optional.of( matchingCandidates.get( 0 ) ); |
| 101 | + } |
| 102 | + |
| 103 | + return Optional.empty(); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + private static boolean isNotTheSameMethod(PsiMethod mappingMethod, PsiMethod candidate) { |
| 108 | + |
| 109 | + return !( mappingMethod.equals( candidate ) ); |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean matchesNameWhenNameIsDefined(String inheritConfigurationName, PsiMethod candidate) { |
| 113 | + |
| 114 | + if ( inheritConfigurationName == null || inheritConfigurationName.isEmpty() ) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + return candidate.getName().equals( inheritConfigurationName ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * simplified version of <code>org.mapstruct.ap.internal.model.source.SourceMethod#canInheritFrom</code> |
| 123 | + */ |
| 124 | + public static boolean canInheritFrom(PsiMethod mappingMethod, PsiType targetType, PsiMethod candidate) { |
| 125 | + |
| 126 | + PsiType targetTypeOfCandidate = findTargetTypeOfMappingMethod( candidate ); |
| 127 | + |
| 128 | + return targetType != null |
| 129 | + && candidate.getBody() == null |
| 130 | + && targetTypeOfCandidate != null |
| 131 | + && targetTypeOfCandidate.isAssignableFrom( targetType ) |
| 132 | + && allParametersAreAssignable( mappingMethod.getParameterList(), candidate.getParameterList() ); |
| 133 | + } |
| 134 | + |
| 135 | + @Nullable |
| 136 | + private static PsiType findTargetTypeOfMappingMethod(PsiMethod mappingMethod) { |
| 137 | + |
| 138 | + PsiType targetType = mappingMethod.getReturnType(); |
| 139 | + |
| 140 | + if ( !PsiType.VOID.equals( targetType ) ) { |
| 141 | + return targetType; |
| 142 | + } |
| 143 | + |
| 144 | + return Stream.of( mappingMethod.getParameterList().getParameters() ) |
| 145 | + .filter( MapstructUtil::isMappingTarget ) |
| 146 | + .findFirst() |
| 147 | + .map( PsiParameter::getType ) |
| 148 | + .orElse( null ); |
| 149 | + } |
| 150 | + |
| 151 | + private static boolean allParametersAreAssignable(PsiParameterList inheritParameters, |
| 152 | + PsiParameterList candidateParameters) { |
| 153 | + |
| 154 | + if ( inheritParameters == null || candidateParameters == null || inheritParameters.isEmpty() || |
| 155 | + candidateParameters.isEmpty() ) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + List<PsiParameter> fromParams = Arrays.stream( inheritParameters.getParameters() ) |
| 160 | + .filter( MapstructUtil::isValidSourceParameter ) |
| 161 | + .collect( Collectors.toList() ); |
| 162 | + |
| 163 | + List<PsiParameter> toParams = Arrays.stream( candidateParameters.getParameters() ) |
| 164 | + .filter( MapstructUtil::isValidSourceParameter ) |
| 165 | + .collect( Collectors.toList() ); |
| 166 | + |
| 167 | + return allParametersAreAssignable( fromParams, toParams ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * psi-modified copy of <code>org.mapstruct.ap.internal.model.source.SourceMethod#allParametersAreAssignable</code> |
| 172 | + */ |
| 173 | + private static boolean allParametersAreAssignable(List<PsiParameter> fromParams, List<PsiParameter> toParams) { |
| 174 | + if ( fromParams.size() == toParams.size() ) { |
| 175 | + Set<PsiParameter> unaccountedToParams = new HashSet<>( toParams ); |
| 176 | + |
| 177 | + for ( PsiParameter fromParam : fromParams ) { |
| 178 | + // each fromParam needs at least one match, and all toParam need to be accounted for at the end |
| 179 | + boolean hasMatch = false; |
| 180 | + for ( PsiParameter toParam : toParams ) { |
| 181 | + if ( toParam.getType().isAssignableFrom( fromParam.getType() ) ) { |
| 182 | + unaccountedToParams.remove( toParam ); |
| 183 | + hasMatch = true; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if ( !hasMatch ) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return unaccountedToParams.isEmpty(); |
| 193 | + } |
| 194 | + |
| 195 | + return false; |
| 196 | + } |
| 197 | +} |
0 commit comments