forked from projectlombok/lombok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractProcessor.java
123 lines (107 loc) · 4.62 KB
/
AbstractProcessor.java
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
package de.plushnikov.intellij.plugin.processor;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.util.ArrayUtil;
import de.plushnikov.intellij.plugin.lombokconfig.ConfigDiscovery;
import de.plushnikov.intellij.plugin.lombokconfig.ConfigKey;
import de.plushnikov.intellij.plugin.util.LombokProcessorUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationSearchUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import lombok.experimental.Tolerate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Base lombok processor class
*
* @author Plushnikov Michail
*/
public abstract class AbstractProcessor implements Processor {
/**
* Annotation classes this processor supports
*/
private final Class<? extends Annotation>[] supportedAnnotationClasses;
/**
* Kind of output elements this processor supports
*/
private final Class<? extends PsiElement> supportedClass;
/**
* Constructor for all Lombok-Processors
*
* @param supportedClass kind of output elements this processor supports
* @param supportedAnnotationClass annotation this processor supports
*/
protected AbstractProcessor(@NotNull Class<? extends PsiElement> supportedClass,
@NotNull Class<? extends Annotation> supportedAnnotationClass) {
this.supportedClass = supportedClass;
this.supportedAnnotationClasses = new Class[]{supportedAnnotationClass};
}
/**
* Constructor for all Lombok-Processors
*
* @param supportedClass kind of output elements this processor supports
* @param supportedAnnotationClass annotation this processor supports
* @param equivalentAnnotationClasses any other equivalent annotations
*/
protected AbstractProcessor(@NotNull Class<? extends PsiElement> supportedClass,
@NotNull Class<? extends Annotation> supportedAnnotationClass,
@NotNull Class<? extends Annotation>... equivalentAnnotationClasses) {
this.supportedClass = supportedClass;
this.supportedAnnotationClasses = ArrayUtil.prepend(supportedAnnotationClass, equivalentAnnotationClasses);
}
@NotNull
public final Class<? extends Annotation>[] getSupportedAnnotationClasses() {
return supportedAnnotationClasses;
}
@NotNull
@Override
public final Class<? extends PsiElement> getSupportedClass() {
return supportedClass;
}
@Override
public boolean isEnabled(@NotNull PropertiesComponent propertiesComponent) {
return true;
}
@NotNull
public List<? super PsiElement> process(@NotNull PsiClass psiClass) {
return Collections.emptyList();
}
@NotNull
public abstract Collection<PsiAnnotation> collectProcessedAnnotations(@NotNull PsiClass psiClass);
protected void filterToleratedElements(@NotNull Collection<? extends PsiModifierListOwner> definedMethods) {
definedMethods.removeIf(definedMethod -> PsiAnnotationSearchUtil.isAnnotatedWith(definedMethod, Tolerate.class));
}
protected static boolean readAnnotationOrConfigProperty(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass,
@NotNull String annotationParameter, @NotNull ConfigKey configKey) {
final boolean result;
final Boolean declaredAnnotationValue = PsiAnnotationUtil.getDeclaredBooleanAnnotationValue(psiAnnotation, annotationParameter);
if (null == declaredAnnotationValue) {
result = ConfigDiscovery.getInstance().getBooleanLombokConfigProperty(configKey, psiClass);
} else {
result = declaredAnnotationValue;
}
return result;
}
protected static void addOnXAnnotations(@Nullable PsiAnnotation processedAnnotation,
@NotNull PsiModifierList modifierList,
@NotNull String onXParameterName) {
if (processedAnnotation == null) {
return;
}
Collection<String> annotationsToAdd = LombokProcessorUtil.getOnX(processedAnnotation, onXParameterName);
for (String annotation : annotationsToAdd) {
modifierList.addAnnotation(annotation);
}
}
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
return LombokPsiElementUsage.NONE;
}
}