-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathBaseValueMappingReference.java
More file actions
114 lines (94 loc) · 3.12 KB
/
BaseValueMappingReference.java
File metadata and controls
114 lines (94 loc) · 3.12 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
/*
* 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 com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Base Reference for {@link org.mapstruct.ValueMapping}(s).
*
* @author Filip Hrisafov
*/
public abstract class BaseValueMappingReference extends BaseReference {
/**
* @param element the element for which a reference should be found
*/
BaseValueMappingReference(@NotNull PsiElement element) {
super( element );
}
@Nullable
@Override
public final PsiElement resolve() {
String value = getValue();
if ( value.isEmpty() ) {
return null;
}
PsiClass enumClass = getEnumClass();
if ( enumClass == null ) {
return null;
}
return resolveInternal( value, enumClass );
}
@Override
@Nullable
PsiMethod getMappingMethod() {
PsiMethod mappingMethod = super.getMappingMethod();
if ( isNotValueMapping( mappingMethod ) ) {
return null;
}
return mappingMethod;
}
public PsiClass getEnumClass() {
PsiMethod mappingMethod = getMappingMethod();
if ( mappingMethod == null ) {
return null;
}
PsiClass enumClass = getEnumClass( mappingMethod );
if ( enumClass == null || !enumClass.isEnum() ) {
return null;
}
return enumClass;
}
@Nullable
PsiElement resolveInternal(@NotNull String value, @NotNull PsiClass enumClass) {
PsiField field = enumClass.findFieldByName( value, false );
if ( field instanceof PsiEnumConstant ) {
return field;
}
return null;
}
PsiClass getEnumClass(@NotNull PsiMethod mappingMethod) {
PsiClass enumClass = determineEnumClass( mappingMethod );
if ( enumClass == null || !enumClass.isEnum() ) {
return null;
}
return enumClass;
}
abstract PsiClass determineEnumClass(@NotNull PsiMethod mappingMethod);
@NotNull
@Override
public final Object[] getVariants() {
PsiMethod mappingMethod = getMappingMethod();
if ( mappingMethod == null ) {
return LookupElement.EMPTY_ARRAY;
}
PsiClass enumClass = getEnumClass( mappingMethod );
if ( enumClass == null ) {
return LookupElement.EMPTY_ARRAY;
}
return getVariantsInternal( mappingMethod, enumClass );
}
@NotNull
abstract Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod, @NotNull PsiClass enumClass);
private static boolean isNotValueMapping(@Nullable PsiMethod mappingMethod) {
return mappingMethod == null || mappingMethod.getParameterList().getParametersCount() != 1;
}
}