Skip to content

Commit 3e3c97c

Browse files
authored
Merge pull request #1069 from mrglavas/1068#reduce-code-duplication
Reduce code duplication in AnnotationDiagnosticsCollector.collectDiagnostics().
2 parents 658a78a + 20f3cfe commit 3e3c97c

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/annotations/AnnotationDiagnosticsCollector.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
*/
4747
public class AnnotationDiagnosticsCollector extends AbstractDiagnosticsCollector {
4848

49+
private static final String[] VALID_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME };
50+
private static final String[] VALID_TYPE_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME,
51+
AnnotationConstants.RESOURCE_FQ_NAME };
52+
private static final String[] VALID_METHOD_ANNOTATIONS = { AnnotationConstants.GENERATED_FQ_NAME,
53+
AnnotationConstants.POST_CONSTRUCT_FQ_NAME, AnnotationConstants.PRE_DESTROY_FQ_NAME,
54+
AnnotationConstants.RESOURCE_FQ_NAME };
55+
4956
public AnnotationDiagnosticsCollector() {
5057
super();
5158
}
@@ -59,57 +66,31 @@ protected String getDiagnosticSource() {
5966
public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
6067
if (unit != null) {
6168
ArrayList<Tuple.Two<PsiAnnotation, PsiElement>> annotatables = new ArrayList<Tuple.Two<PsiAnnotation, PsiElement>>();
62-
String[] validAnnotations = { AnnotationConstants.GENERATED_FQ_NAME };
63-
String[] validTypeAnnotations = { AnnotationConstants.GENERATED_FQ_NAME,
64-
AnnotationConstants.RESOURCE_FQ_NAME };
65-
String[] validMethodAnnotations = { AnnotationConstants.GENERATED_FQ_NAME,
66-
AnnotationConstants.POST_CONSTRUCT_FQ_NAME, AnnotationConstants.PRE_DESTROY_FQ_NAME,
67-
AnnotationConstants.RESOURCE_FQ_NAME };
6869

6970
PsiPackage psiPackage = JavaPsiFacade.getInstance(unit.getProject())
7071
.findPackage(unit.getPackageName());
7172
if (psiPackage != null) {
72-
PsiAnnotation[] pkgAnnotations = psiPackage.getAnnotations();
73-
for (PsiAnnotation annotation : pkgAnnotations) {
74-
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
75-
annotatables.add(new Tuple.Two<>(annotation, psiPackage));
76-
}
73+
processAnnotations(psiPackage, annotatables, VALID_ANNOTATIONS);
7774
}
7875

7976
PsiClass[] types = unit.getClasses();
8077
for (PsiClass type : types) {
8178
// Type
82-
PsiAnnotation[] annotations = type.getAnnotations();
83-
for (PsiAnnotation annotation : annotations) {
84-
if (isValidAnnotation(annotation.getQualifiedName(), validTypeAnnotations))
85-
annotatables.add(new Tuple.Two<>(annotation, type));
86-
}
79+
processAnnotations(type, annotatables, VALID_TYPE_ANNOTATIONS);
8780
// Method
8881
PsiMethod[] methods = type.getMethods();
8982
for (PsiMethod method : methods) {
90-
annotations = method.getAnnotations();
91-
for (PsiAnnotation annotation : annotations) {
92-
if (isValidAnnotation(annotation.getQualifiedName(), validMethodAnnotations))
93-
annotatables.add(new Tuple.Two<>(annotation, method));
94-
}
83+
processAnnotations(method, annotatables, VALID_METHOD_ANNOTATIONS);
9584
// method parameters
9685
PsiParameter[] parameters = method.getParameterList().getParameters();
9786
for (PsiParameter parameter : parameters) {
98-
annotations = parameter.getAnnotations();
99-
for (PsiAnnotation annotation : annotations) {
100-
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
101-
annotatables.add(new Tuple.Two<>(annotation, parameter));
102-
}
87+
processAnnotations(parameter, annotatables, VALID_ANNOTATIONS);
10388
}
10489
}
10590
// Field
10691
PsiField[] fields = type.getFields();
10792
for (PsiField field : fields) {
108-
annotations = field.getAnnotations();
109-
for (PsiAnnotation annotation : annotations) {
110-
if (isValidAnnotation(annotation.getQualifiedName(), validTypeAnnotations))
111-
annotatables.add(new Tuple.Two<>(annotation, field));
112-
}
93+
processAnnotations(field, annotatables, VALID_TYPE_ANNOTATIONS);
11394
}
11495
}
11596

@@ -224,6 +205,16 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
224205
}
225206
}
226207

208+
private void processAnnotations(PsiJvmModifiersOwner psiModifierOwner,
209+
ArrayList<Tuple.Two<PsiAnnotation, PsiElement>> annotatables,
210+
String[] validAnnotations) {
211+
PsiAnnotation[] annotations = psiModifierOwner.getAnnotations();
212+
for (PsiAnnotation annotation : annotations) {
213+
if (isValidAnnotation(annotation.getQualifiedName(), validAnnotations))
214+
annotatables.add(new Tuple.Two<>(annotation, psiModifierOwner));
215+
}
216+
}
217+
227218
private static boolean isValidAnnotation(String annotationName, String[] validAnnotations) {
228219
if (annotationName != null) {
229220
for (String fqName : validAnnotations) {

0 commit comments

Comments
 (0)