11/*******************************************************************************
2- * Copyright (c) 2020, 2023 IBM Corporation, Ankush Sharma and others.
2+ * Copyright (c) 2020, 2024 IBM Corporation, Ankush Sharma and others.
33 *
44 * This program and the accompanying materials are made available under the
55 * terms of the Eclipse Public License v. 2.0 which is available at
1818import io .openliberty .tools .intellij .lsp4jakarta .lsp4ij .Messages ;
1919import org .eclipse .lsp4j .Diagnostic ;
2020import org .eclipse .lsp4j .DiagnosticSeverity ;
21+ import org .slf4j .Logger ;
22+ import org .slf4j .LoggerFactory ;
2123
2224import java .util .ArrayList ;
2325import java .util .Arrays ;
2426import java .util .List ;
2527
2628public class PersistenceMapKeyDiagnosticsCollector extends AbstractDiagnosticsCollector {
2729
30+ private static final Logger log = LoggerFactory .getLogger (PersistenceMapKeyDiagnosticsCollector .class );
31+
2832 public PersistenceMapKeyDiagnosticsCollector () {
2933 super ();
3034 }
@@ -37,83 +41,58 @@ protected String getDiagnosticSource() {
3741 @ Override
3842 public void collectDiagnostics (PsiJavaFile unit , List <Diagnostic > diagnostics ) {
3943 if (unit != null ) {
40- PsiClass [] alltypes ;
41- PsiAnnotation [] allAnnotations ;
42-
43- alltypes = unit .getClasses ();
44+ PsiClass [] alltypes = unit .getClasses ();
4445 PsiMethod [] methods ;
4546 PsiField [] fields ;
4647
4748 for (PsiClass type : alltypes ) {
4849 methods = type .getMethods ();
4950 for (PsiMethod method : methods ) {
50- List <PsiAnnotation > mapKeyJoinCols = new ArrayList <PsiAnnotation >();
51- boolean hasMapKeyAnnotation = false ;
52- boolean hasMapKeyClassAnnotation = false ;
53- allAnnotations = method .getAnnotations ();
54- for (PsiAnnotation annotation : allAnnotations ) {
55- String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (),
56- PersistenceConstants .SET_OF_PERSISTENCE_ANNOTATIONS );
57- if (matchedAnnotation != null ) {
58- if (PersistenceConstants .MAPKEY .equals (matchedAnnotation ))
59- hasMapKeyAnnotation = true ;
60- else if (PersistenceConstants .MAPKEYCLASS .equals (matchedAnnotation ))
61- hasMapKeyClassAnnotation = true ;
62- else if (PersistenceConstants .MAPKEYJOINCOLUMN .equals (matchedAnnotation )) {
63- mapKeyJoinCols .add (annotation );
64- }
65- }
66- }
67- if (hasMapKeyAnnotation && hasMapKeyClassAnnotation ) {
68- // A single field cannot have the same
69- diagnostics .add (createDiagnostic (method , unit ,
70- Messages .getMessage ("MapKeyAnnotationsNotOnSameField" ),
71- PersistenceConstants .DIAGNOSTIC_CODE_INVALID_ANNOTATION , null ,
72- DiagnosticSeverity .Error ));
73- }
74- // If we have multiple MapKeyJoinColumn annotations on a single method we must
75- // ensure each has a name and referencedColumnName
76- if (mapKeyJoinCols .size () > 1 ) {
77- validateMapKeyJoinColumnAnnotations (mapKeyJoinCols , method , unit , diagnostics );
78- }
51+ collectDiagnostics (unit , diagnostics , type , method );
7952 }
80-
8153 // Go through each field to ensure they do not have both MapKey and MapKeyColumn
8254 // Annotations
8355 fields = type .getFields ();
8456 for (PsiField field : fields ) {
85- List <PsiAnnotation > mapKeyJoinCols = new ArrayList <PsiAnnotation >();
86- boolean hasMapKeyAnnotation = false ;
87- boolean hasMapKeyClassAnnotation = false ;
88- allAnnotations = field .getAnnotations ();
89- for (PsiAnnotation annotation : allAnnotations ) {
90- String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (),
91- PersistenceConstants .SET_OF_PERSISTENCE_ANNOTATIONS );
92- if (matchedAnnotation != null ) {
93- if (PersistenceConstants .MAPKEY .equals (matchedAnnotation ))
94- hasMapKeyAnnotation = true ;
95- else if (PersistenceConstants .MAPKEYCLASS .equals (matchedAnnotation ))
96- hasMapKeyClassAnnotation = true ;
97- else if (PersistenceConstants .MAPKEYJOINCOLUMN .equals (matchedAnnotation )) {
98- mapKeyJoinCols .add (annotation );
99- }
100- }
101- }
102- if (hasMapKeyAnnotation && hasMapKeyClassAnnotation ) {
103- // A single field cannot have the same
104- diagnostics .add (createDiagnostic (field , unit ,
105- Messages .getMessage ("MapKeyAnnotationsNotOnSameField" ),
106- PersistenceConstants .DIAGNOSTIC_CODE_INVALID_ANNOTATION , null ,
107- DiagnosticSeverity .Error ));
108- }
109- if (mapKeyJoinCols .size () > 1 ) {
110- validateMapKeyJoinColumnAnnotations (mapKeyJoinCols , field , unit , diagnostics );
111- }
57+ collectDiagnostics (unit , diagnostics , type , field );
11258 }
11359 }
11460 }
11561 }
11662
63+ private void collectDiagnostics (PsiJavaFile unit , List <Diagnostic > diagnostics ,
64+ PsiClass type , PsiJvmModifiersOwner fieldOrProperty ) {
65+ List <PsiAnnotation > mapKeyJoinCols = new ArrayList <PsiAnnotation >();
66+ boolean hasMapKeyAnnotation = false ;
67+ boolean hasMapKeyClassAnnotation = false ;
68+ PsiAnnotation [] allAnnotations = fieldOrProperty .getAnnotations ();
69+ for (PsiAnnotation annotation : allAnnotations ) {
70+ String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (),
71+ PersistenceConstants .SET_OF_PERSISTENCE_ANNOTATIONS );
72+ if (matchedAnnotation != null ) {
73+ if (PersistenceConstants .MAPKEY .equals (matchedAnnotation ))
74+ hasMapKeyAnnotation = true ;
75+ else if (PersistenceConstants .MAPKEYCLASS .equals (matchedAnnotation ))
76+ hasMapKeyClassAnnotation = true ;
77+ else if (PersistenceConstants .MAPKEYJOINCOLUMN .equals (matchedAnnotation )) {
78+ mapKeyJoinCols .add (annotation );
79+ }
80+ }
81+ }
82+ if (hasMapKeyAnnotation && hasMapKeyClassAnnotation ) {
83+ // A single field or property cannot have the same
84+ diagnostics .add (createDiagnostic (fieldOrProperty , unit ,
85+ Messages .getMessage ("MapKeyAnnotationsNotOnSameField" ),
86+ PersistenceConstants .DIAGNOSTIC_CODE_INVALID_ANNOTATION , null ,
87+ DiagnosticSeverity .Error ));
88+ }
89+ // If we have multiple MapKeyJoinColumn annotations on a single method or property we must
90+ // ensure each has a name and referencedColumnName
91+ if (mapKeyJoinCols .size () > 1 ) {
92+ validateMapKeyJoinColumnAnnotations (mapKeyJoinCols , fieldOrProperty , unit , diagnostics );
93+ }
94+ }
95+
11796 private void validateMapKeyJoinColumnAnnotations (List <PsiAnnotation > annotations , PsiElement element ,
11897 PsiJavaFile unit , List <Diagnostic > diagnostics ) {
11998 String message = (element instanceof PsiMethod ) ?
@@ -131,4 +110,5 @@ private void validateMapKeyJoinColumnAnnotations(List<PsiAnnotation> annotations
131110 PersistenceConstants .DIAGNOSTIC_CODE_MISSING_ATTRIBUTES , null , DiagnosticSeverity .Error ));
132111 }
133112 });
134- }}
113+ }
114+ }
0 commit comments