2121import org .eclipse .lsp4j .DiagnosticSeverity ;
2222import static io .openliberty .tools .intellij .lsp4jakarta .lsp4ij .di .DependencyInjectionConstants .*;
2323
24- import java .util .*;
24+ import java .util .ArrayList ;
25+ import java .util .HashSet ;
26+ import java .util .List ;
27+ import java .util .Set ;
28+ import java .util .Arrays ;
29+ import java .util .Objects ;
2530import java .util .stream .Collectors ;
2631import java .util .stream .Stream ;
2732
@@ -60,6 +65,7 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
6065 for (PsiClass type : alltypes ) {
6166 PsiField [] allFields = type .getFields ();
6267 boolean isCdiScoped = hasCdiScopeAnnotation (type );
68+ String [] implicitQualifiers = IMPLICIT_QUALIFIERS .toArray (String []::new );
6369 for (PsiField field : allFields ) {
6470 if (containsAnnotation (type , field .getAnnotations (), INJECT_FQ_NAME )) {
6571 if (field .hasModifierProperty (PsiModifier .FINAL )) {
@@ -81,21 +87,10 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
8187 if (isMatchedJavaElement (type , annotation .getQualifiedName (), INJECT_FQ_NAME )){
8288 isInject = true ;
8389 }else {
84- String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (), IMPLICIT_QUALIFIERS .toArray (String []::new ));
85- if (matchedAnnotation !=null ){
86- fqNames .add (matchedAnnotation );
87- }
88- }
89- }
90- if (fqNames .equals (IMPLICIT_QUALIFIERS )) {
91- continue ;
92- }else {
93- //Finding and generating invalid inject qualifier diagnostics for fields in parent class
94- List <PsiAnnotation > qualifiers = getQualifiers (field .getAnnotations (), unit , type );
95- if (isInject && qualifiers .size () > 1 && !isCdiScoped ) {
96- createInvalidInjectQualifierFieldDiagnostics (unit , diagnostics , field );
90+ getMatchedAnnotationFQ (type , annotation , implicitQualifiers , fqNames );
9791 }
9892 }
93+ checkInvalidQualifiersForField (unit , diagnostics , type , field , fqNames , isInject , isCdiScoped );
9994 }
10095
10196 List <PsiMethod > injectedConstructors = new ArrayList <PsiMethod >();
@@ -141,18 +136,7 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
141136 DIAGNOSTIC_CODE_INJECT_INNER_CLASS , method .getReturnType ().getInternalCanonicalText (),
142137 DiagnosticSeverity .Error ));
143138 }
144- if (new HashSet <>(getMatchedJavaElementNames (type , Arrays .stream (param .getAnnotations ())
145- .map (PsiAnnotation ::getQualifiedName )
146- .filter (Objects ::nonNull )
147- .collect (Collectors .toSet ()).toArray (String []::new ), IMPLICIT_QUALIFIERS .toArray (String []::new ))).equals (IMPLICIT_QUALIFIERS )){
148- continue ;
149- }else {
150- //Finding and generating invalid inject qualifier diagnostics for method parameters in parent class
151- List <PsiAnnotation > qualifiers = getQualifiers (param .getAnnotations (), unit , type );
152- if (qualifiers .size () > 1 && !isCdiScoped ) {
153- createInvalidInjectQualifierMethodDiagnostics (unit , diagnostics , method );
154- }
155- }
139+ checkInvalidQualifierMethodDiagnostics (unit , diagnostics , type , method , param , implicitQualifiers , isCdiScoped , true );
156140 }
157141 }
158142 }
@@ -174,43 +158,95 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
174158 if (isMatchedJavaElement (type , annotation .getQualifiedName (), INJECT_FQ_NAME )){
175159 isInjectInner = true ;
176160 }else {
177- String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (), IMPLICIT_QUALIFIERS .toArray (String []::new ));
178- if (matchedAnnotation !=null ){
179- fqNamesInner .add (matchedAnnotation );
180- }
161+ getMatchedAnnotationFQ (type , annotation , implicitQualifiers , fqNamesInner );
181162 }
182163 }
183- if (fqNamesInner .equals (IMPLICIT_QUALIFIERS )) {
184- continue ;
185- }
186- else {
187- List <PsiAnnotation > qualifiers = getQualifiers (innerField .getAnnotations (), unit , type );
188- if (isInjectInner && qualifiers .size () > 1 && !isCdiScoped ) {
189- createInvalidInjectQualifierFieldDiagnostics (unit , diagnostics , innerField );
190- }
191- }
192- }
164+ checkInvalidQualifiersForField (unit , diagnostics , type , innerField , fqNamesInner , isInjectInner , isCdiScoped );
165+ }
193166 for (PsiMethod innerMethod : innerClass .getMethods ()){
194167 if (containsAnnotation (type , innerMethod .getAnnotations (), INJECT_FQ_NAME )) {
195168 for (PsiParameter param : innerMethod .getParameterList ().getParameters ()) {
196- if (new HashSet <>(getMatchedJavaElementNames (type , Arrays .stream (param .getAnnotations ())
197- .map (PsiAnnotation ::getQualifiedName )
198- .filter (Objects ::nonNull )
199- .collect (Collectors .toSet ()).toArray (String []::new ), IMPLICIT_QUALIFIERS .toArray (String []::new ))).equals (IMPLICIT_QUALIFIERS )){
200- continue ;
201- }else {
202- List <PsiAnnotation > qualifiers = getQualifiers (innerMethod .getAnnotations (), unit , type );
203- if (qualifiers .size () > 1 && !isCdiScoped ) {
204- createInvalidInjectQualifierMethodDiagnostics (unit , diagnostics , innerMethod );
205- }
206- }
169+ checkInvalidQualifierMethodDiagnostics (unit , diagnostics , type , innerMethod , param , implicitQualifiers , isCdiScoped , false );
207170 }
208171 }
209172 }
210173 }
211174 }
212175 }
213176
177+ /**
178+ * checkInvalidQualifiersForField
179+ * Method to generate field level diagnostics for invalid qualifiers
180+ *
181+ * @param unit
182+ * @param diagnostics
183+ * @param type
184+ * @param field
185+ * @param fqNames
186+ * @param isInject
187+ * @param isCdiScoped
188+ */
189+ private void checkInvalidQualifiersForField (PsiJavaFile unit , List <Diagnostic > diagnostics , PsiClass type , PsiField field , Set <String > fqNames , boolean isInject , boolean isCdiScoped ) {
190+ if (fqNames .equals (IMPLICIT_QUALIFIERS )) {
191+ return ;
192+ }else {
193+ //Finding and generating invalid inject qualifier diagnostics for fields in parent class
194+ List <PsiAnnotation > qualifiers = getQualifiers (field .getAnnotations (), unit , type );
195+ if (isInject && qualifiers .size () > 1 && !isCdiScoped ) {
196+ createInvalidInjectQualifierFieldDiagnostics (unit , diagnostics , field );
197+ }
198+ }
199+ }
200+
201+ /**
202+ * getMatchedAnnotationFQ
203+ * Gets Fully qualified annotations matching with implicit qualifiers
204+ *
205+ * @param type
206+ * @param annotation
207+ * @param implicitQualifiers
208+ * @param fqNamesInner
209+ */
210+ private static void getMatchedAnnotationFQ (PsiClass type , PsiAnnotation annotation , String [] implicitQualifiers , Set <String > fqNamesInner ) {
211+ String matchedAnnotation = getMatchedJavaElementName (type , annotation .getQualifiedName (), implicitQualifiers );
212+ if (matchedAnnotation !=null ){
213+ fqNamesInner .add (matchedAnnotation );
214+ }
215+ }
216+
217+ /**
218+ * checkInvalidQualifierMethodDiagnostics
219+ * Method checks if invalid qualifier combination is used and throws appropriate diagnostics
220+ *
221+ * @param unit
222+ * @param diagnostics
223+ * @param type
224+ * @param method
225+ * @param param
226+ * @param implicitQualifiers
227+ * @param isCdiScoped
228+ */
229+ private void checkInvalidQualifierMethodDiagnostics (PsiJavaFile unit , List <Diagnostic > diagnostics , PsiClass type , PsiMethod method , PsiParameter param , String [] implicitQualifiers , boolean isCdiScoped , boolean isParent ) {
230+ String [] paramAnnotations = Arrays .stream (param .getAnnotations ())
231+ .map (PsiAnnotation ::getQualifiedName )
232+ .filter (Objects ::nonNull )
233+ .collect (Collectors .toSet ()).toArray (String []::new );
234+ Set paramAnnotationsFQ = new HashSet <>(getMatchedJavaElementNames (type , paramAnnotations , implicitQualifiers ));
235+ if (paramAnnotationsFQ .equals (IMPLICIT_QUALIFIERS )){
236+ return ;
237+ }else {
238+ //Finding and generating invalid inject qualifier diagnostics for method parameters
239+ List <PsiAnnotation > qualifiers = new ArrayList <>();
240+ if (isParent )
241+ qualifiers = getQualifiers (param .getAnnotations (), unit , type );
242+ else
243+ qualifiers = getQualifiers (method .getAnnotations (), unit , type );
244+ if (qualifiers .size () > 1 && !isCdiScoped ) {
245+ createInvalidInjectQualifierMethodDiagnostics (unit , diagnostics , method );
246+ }
247+ }
248+ }
249+
214250 /**
215251 * createInvalidInjectQualifierFieldDiagnostics
216252 * @description Method to create diagnostics invalid inject qualifiers for fields.
0 commit comments