-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMapstructTargetReference.java
More file actions
259 lines (226 loc) · 9.5 KB
/
MapstructTargetReference.java
File metadata and controls
259 lines (226 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* 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.codeinsight.references;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.PsiRecordComponent;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.Mapping;
import org.mapstruct.intellij.util.MapStructVersion;
import org.mapstruct.intellij.util.MapstructUtil;
import org.mapstruct.intellij.util.TargetType;
import org.mapstruct.intellij.util.TargetUtils;
import static org.mapstruct.intellij.util.MapstructAnnotationUtils.findAllDefinedMappingAnnotations;
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
import static org.mapstruct.intellij.util.MapstructUtil.findRecordComponent;
import static org.mapstruct.intellij.util.MapstructUtil.isPublicModifiable;
import static org.mapstruct.intellij.util.MapstructUtil.isPublicNonStatic;
import static org.mapstruct.intellij.util.TargetUtils.getRelevantType;
import static org.mapstruct.intellij.util.TargetUtils.isBuilderEnabled;
import static org.mapstruct.intellij.util.TargetUtils.publicWriteAccessors;
import static org.mapstruct.intellij.util.TargetUtils.resolveBuilderOrSelfClass;
import static org.mapstruct.intellij.util.TypeUtils.firstParameterPsiType;
/**
* Reference for {@link org.mapstruct.Mapping#target()}.
*
* @author Filip Hrisafov
*/
class MapstructTargetReference extends MapstructBaseReference {
private final MapStructVersion mapStructVersion;
/**
* Create a new {@link MapstructTargetReference} with the provided parameters
*
* @param element the element that the reference belongs to
* @param previousReference the previous reference if there is one (in nested properties for example)
* @param rangeInElement the range that the reference represent in the {@code element}
* @param value the matched value (useful when {@code rangeInElement} is empty)
*/
private MapstructTargetReference(PsiElement element, MapstructTargetReference previousReference,
TextRange rangeInElement, String value) {
super( element, previousReference, rangeInElement, value );
mapStructVersion = MapstructUtil.resolveMapStructProjectVersion( element.getContainingFile()
.getOriginalFile() );
}
@Override
PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
boolean builderSupportPresent = mapStructVersion.isBuilderSupported();
Pair<PsiClass, TargetType> pair = resolveBuilderOrSelfClass(
psiType,
builderSupportPresent && isBuilderEnabled( getMappingMethod() )
);
if ( pair == null ) {
return null;
}
PsiClass psiClass = pair.getFirst();
TargetType targetType = pair.getSecond();
PsiType typeToUse = targetType.type();
PsiRecordComponent recordComponent = findRecordComponent( value, psiClass );
if ( recordComponent != null ) {
return recordComponent;
}
if ( mapStructVersion.isConstructorSupported() && !targetType.builder() ) {
PsiMethod constructor = TargetUtils.resolveMappingConstructor( psiClass );
if ( constructor != null && constructor.hasParameters() ) {
for ( PsiParameter parameter : constructor.getParameterList().getParameters() ) {
if ( value.equals( parameter.getName() ) ) {
return parameter;
}
}
}
}
String capitalizedName = MapstructUtil.capitalize( value );
PsiMethod[] methods = psiClass.findMethodsByName( "set" + capitalizedName, true );
if ( methods.length != 0 && isPublicNonStatic( methods[0] ) ) {
return methods[0];
}
// If there is no such setter we need to check if there is a collection getter
methods = psiClass.findMethodsByName( "get" + capitalizedName, true );
if ( methods.length != 0 && isCollectionGetterWriteAccessor( methods[0] ) ) {
return methods[0];
}
if ( builderSupportPresent ) {
for ( PsiMethod method : psiClass.findMethodsByName( value, true ) ) {
if ( method.getParameterList().getParametersCount() == 1 &&
mapstructUtil.isFluentSetter( method, typeToUse ) ) {
return method;
}
}
}
PsiClass selfClass = PsiUtil.resolveClassInType( psiType );
if ( selfClass != null ) {
PsiField field = selfClass.findFieldByName( value, true );
if ( field != null && isPublicModifiable( field ) ) {
return field;
}
}
return null;
}
@Override
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) {
PsiType relevantType = getRelevantType( mappingMethod );
if ( relevantType == null ) {
return null;
}
PsiElement psiElement = resolveInternal( value, relevantType );
if ( psiElement != null ) {
return psiElement;
}
return Stream.of( mappingMethod.getParameterList().getParameters() )
.filter( MapstructUtil::isMappingTarget )
.filter( psiParameter -> Objects.equals( psiParameter.getName(), value ) )
.findAny()
.orElse( null );
}
@NotNull
@Override
Object[] getVariantsInternal(@NotNull PsiType psiType) {
PsiMethod mappingMethod = getMappingMethod();
Map<String, Pair<? extends PsiElement, PsiSubstitutor>> accessors = publicWriteAccessors(
psiType,
mapStructVersion,
mapstructUtil,
mappingMethod
);
if (mappingMethod != null) {
Stream<String> allDefinedMappingTargets = findAllDefinedMappingTargets( mappingMethod );
allDefinedMappingTargets.forEach( accessors::remove );
}
return asLookup(
accessors,
MapstructTargetReference::memberPsiType
);
}
/**
* Find all defined {@link Mapping#target()} for the given method
*
* @param method that needs to be checked
*
* @return see description
*/
private Stream<String> findAllDefinedMappingTargets(@NotNull PsiMethod method) {
return findAllDefinedMappingAnnotations( method, mapStructVersion )
.map( psiAnnotation -> AnnotationUtil.getDeclaredStringAttributeValue( psiAnnotation, "target" ) )
.filter( Objects::nonNull )
.filter( s -> !s.isEmpty() );
}
@NotNull
@Override
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
PsiType targetType = getRelevantType( mappingMethod );
return targetType == null ? LookupElement.EMPTY_ARRAY : getVariantsInternal( targetType );
}
@Nullable
@Override
PsiType resolvedType() {
PsiElement element = resolve();
if ( element instanceof PsiMethod psiMethod ) {
return firstParameterPsiType( psiMethod );
}
else if ( element instanceof PsiParameter psiParameter ) {
return psiParameter.getType();
}
else if ( element instanceof PsiRecordComponent psiRecordComponent ) {
return psiRecordComponent.getType();
}
else if ( element instanceof PsiField psiField ) {
return psiField.getType();
}
return null;
}
/**
* @param psiElement the literal for which references need to be created
*
* @return the references for the given {@code psiLiteral}
*/
static PsiReference[] create(PsiElement psiElement) {
return MapstructBaseReference.create( psiElement, MapstructTargetReference::new, true );
}
private static PsiType memberPsiType(PsiElement psiMember) {
if ( psiMember instanceof PsiMethod psiMemberMethod ) {
return resolveMethodType( psiMemberMethod );
}
else if ( psiMember instanceof PsiVariable psiMemberVariable ) {
return psiMemberVariable.getType();
}
else {
return null;
}
}
private static boolean isCollectionGetterWriteAccessor(@NotNull PsiMethod method) {
if ( !isPublicNonStatic( method ) ) {
return false;
}
PsiParameterList parameterList = method.getParameterList();
if ( parameterList.getParametersCount() > 0 ) {
return false;
}
return TargetUtils.isMethodReturnTypeAssignableToCollectionOrMap( method );
}
private static PsiType resolveMethodType(PsiMethod psiMethod) {
PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
if ( psiParameters.length == 0 ) {
return psiMethod.getReturnType();
}
return psiParameters[0].getType();
}
}