|
| 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.codeinsight.references; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 14 | +import com.intellij.openapi.util.TextRange; |
| 15 | +import com.intellij.openapi.util.text.StringUtil; |
| 16 | +import com.intellij.psi.PsiAnnotation; |
| 17 | +import com.intellij.psi.PsiClass; |
| 18 | +import com.intellij.psi.PsiElement; |
| 19 | +import com.intellij.psi.PsiMethod; |
| 20 | +import com.intellij.psi.PsiParameter; |
| 21 | +import com.intellij.psi.PsiReference; |
| 22 | +import com.intellij.psi.PsiType; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | +import org.mapstruct.intellij.util.MapstructUtil; |
| 26 | + |
| 27 | +import static com.intellij.codeInsight.AnnotationUtil.findAnnotation; |
| 28 | +import static com.intellij.codeInsight.AnnotationUtil.getStringAttributeValue; |
| 29 | +import static org.mapstruct.intellij.util.MapstructAnnotationUtils.findReferencedMapperClasses; |
| 30 | +import static org.mapstruct.intellij.util.MapstructUtil.asLookupWithRepresentableText; |
| 31 | + |
| 32 | +/** |
| 33 | + * Reference for {@link org.mapstruct.Mapping#qualifiedByName()}. |
| 34 | + * |
| 35 | + * @author Oliver Erhart |
| 36 | + */ |
| 37 | +class MapstructMappingQualifiedByNameReference extends MapstructBaseReference { |
| 38 | + |
| 39 | + /** |
| 40 | + * Create a new {@link MapstructMappingQualifiedByNameReference} with the provided parameters |
| 41 | + * |
| 42 | + * @param element the element that the reference belongs to |
| 43 | + * @param previousReference the previous reference if there is one (in nested properties for example) |
| 44 | + * @param rangeInElement the range that the reference represent in the {@code element} |
| 45 | + * @param value the matched value (useful when {@code rangeInElement} is empty) |
| 46 | + */ |
| 47 | + private MapstructMappingQualifiedByNameReference(PsiElement element, |
| 48 | + MapstructMappingQualifiedByNameReference previousReference, |
| 49 | + TextRange rangeInElement, String value) { |
| 50 | + super( element, previousReference, rangeInElement, value ); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) { |
| 55 | + return null; // not needed |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) { |
| 60 | + |
| 61 | + return findAllNamedMethodsFromThisAndReferencedMappers( mappingMethod ) |
| 62 | + .filter( a -> Objects.equals( getNamedValue( a ), value ) ) |
| 63 | + .findAny() |
| 64 | + .orElse( null ); |
| 65 | + } |
| 66 | + |
| 67 | + @Nullable |
| 68 | + private String getNamedValue(PsiMethod method) { |
| 69 | + |
| 70 | + PsiAnnotation annotation = findAnnotation( method, true, MapstructUtil.NAMED_ANNOTATION_FQN ); |
| 71 | + |
| 72 | + if ( annotation == null ) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return getStringAttributeValue( annotation, "value" ); |
| 77 | + } |
| 78 | + |
| 79 | + @NotNull |
| 80 | + @Override |
| 81 | + Object[] getVariantsInternal(@NotNull PsiType psiType) { |
| 82 | + return LookupElement.EMPTY_ARRAY; // not needed |
| 83 | + } |
| 84 | + |
| 85 | + @NotNull |
| 86 | + @Override |
| 87 | + Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) { |
| 88 | + |
| 89 | + return findAllNamedMethodsFromThisAndReferencedMappers( mappingMethod ) |
| 90 | + .map( this::methodAsLookup ) |
| 91 | + .filter( Objects::nonNull ) |
| 92 | + .toArray(); |
| 93 | + } |
| 94 | + |
| 95 | + private boolean methodHasReturnType(@NotNull PsiMethod psiMethod) { |
| 96 | + return !PsiType.VOID.equals( psiMethod.getReturnType() ); |
| 97 | + } |
| 98 | + |
| 99 | + @NotNull |
| 100 | + private Stream<PsiMethod> findAllNamedMethodsFromThisAndReferencedMappers(@NotNull PsiMethod mappingMethod) { |
| 101 | + |
| 102 | + PsiClass containingClass = mappingMethod.getContainingClass(); |
| 103 | + if ( containingClass == null ) { |
| 104 | + return Stream.empty(); |
| 105 | + } |
| 106 | + |
| 107 | + Stream<PsiMethod> internalMethods = Stream.of( containingClass.getMethods() ) |
| 108 | + .filter( MapstructUtil::isNamedMethod ); |
| 109 | + |
| 110 | + Stream<PsiMethod> externalMethods = findNamedMethodsInUsedMappers( containingClass ); |
| 111 | + |
| 112 | + return Stream.concat( internalMethods, externalMethods ) |
| 113 | + .filter( this::methodHasReturnType ); |
| 114 | + } |
| 115 | + |
| 116 | + @NotNull |
| 117 | + private Stream<PsiMethod> findNamedMethodsInUsedMappers(@Nullable PsiClass containingClass) { |
| 118 | + |
| 119 | + PsiAnnotation mapperAnnotation = findAnnotation( |
| 120 | + containingClass, |
| 121 | + MapstructUtil.MAPPER_ANNOTATION_FQN |
| 122 | + ); |
| 123 | + |
| 124 | + if ( mapperAnnotation == null ) { |
| 125 | + return Stream.empty(); |
| 126 | + } |
| 127 | + |
| 128 | + return findReferencedMapperClasses( mapperAnnotation ) |
| 129 | + .flatMap( psiClass -> Arrays.stream( psiClass.getMethods() ) ) |
| 130 | + .filter( MapstructUtil::isNamedMethod ); |
| 131 | + } |
| 132 | + |
| 133 | + private LookupElement methodAsLookup(@NotNull PsiMethod method) { |
| 134 | + String lookupString = getNamedValue( method ); |
| 135 | + if ( StringUtil.isEmpty( lookupString ) ) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + return asLookupWithRepresentableText( |
| 140 | + method, |
| 141 | + lookupString, |
| 142 | + lookupString, |
| 143 | + String.format( |
| 144 | + " %s#%s(%s)", |
| 145 | + Objects.requireNonNull( method.getContainingClass() ).getName(), |
| 146 | + method.getName(), |
| 147 | + formatParameters( method ) |
| 148 | + ) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + @NotNull |
| 153 | + private static String formatParameters(@NotNull PsiMethod method) { |
| 154 | + return Arrays.stream( method.getParameterList().getParameters() ) |
| 155 | + .map( PsiParameter::getType ) |
| 156 | + .map( PsiType::getPresentableText ) |
| 157 | + .collect( Collectors.joining( ", " ) ); |
| 158 | + } |
| 159 | + |
| 160 | + @Nullable |
| 161 | + @Override |
| 162 | + PsiType resolvedType() { |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param psiElement the literal for which references need to be created |
| 168 | + * @return the references for the given {@code psiLiteral} |
| 169 | + */ |
| 170 | + static PsiReference[] create(PsiElement psiElement) { |
| 171 | + return MapstructBaseReference.create( psiElement, MapstructMappingQualifiedByNameReference::new, false ); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments