|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2020, 2024 IBM Corporation, Reza Akhavan and others. |
| 2 | + * Copyright (c) 2020, 2025 IBM Corporation, Reza Akhavan and others. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0 which is available at |
|
14 | 14 | package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.beanvalidation; |
15 | 15 |
|
16 | 16 | import com.intellij.psi.*; |
| 17 | +import com.intellij.psi.util.PsiUtil; |
17 | 18 | import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.AbstractDiagnosticsCollector; |
18 | 19 | import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages; |
19 | 20 | import org.eclipse.lsp4j.Diagnostic; |
20 | 21 | import org.eclipse.lsp4j.DiagnosticSeverity; |
21 | 22 |
|
| 23 | +import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.DiagnosticsUtils.inheritsFrom; |
22 | 24 | import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils.getSimpleName; |
23 | 25 | import static io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.beanvalidation.BeanValidationConstants.*; |
24 | 26 |
|
@@ -159,40 +161,40 @@ private void validAnnotation(PsiElement element, PsiAnnotation annotation, Strin |
159 | 161 | diagnostics.add(createDiagnostic(element, (PsiJavaFile) element.getContainingFile(), |
160 | 162 | source, DIAGNOSTIC_CODE_INVALID_TYPE, annotationName, DiagnosticSeverity.Error)); |
161 | 163 | } |
| 164 | + } else if (matchedAnnotation.equals(NOT_EMPTY) || matchedAnnotation.equals(SIZE)) { |
| 165 | + if (!(isSizeOrNonEmptyAllowed(type))) { |
| 166 | + String source = isMethod ? |
| 167 | + Messages.getMessage("SizeOrNonEmptyAnnotationsMethod") : Messages.getMessage( |
| 168 | + "SizeOrNonEmptyAnnotationsField"); |
| 169 | + diagnostics.add(createDiagnostic(element, (PsiJavaFile) element.getContainingFile(), |
| 170 | + source, DIAGNOSTIC_CODE_INVALID_TYPE, annotationName, DiagnosticSeverity.Error)); |
| 171 | + } |
162 | 172 | } |
163 | | - |
164 | | - // These ones contains check on all collection types which requires resolving |
165 | | - // the String of the type somehow |
166 | | - // This will also require us to check if the field type was a custom collection |
167 | | - // subtype which means we |
168 | | - // have to resolve it and get the super interfaces and check to see if |
169 | | - // Collection, Map or Array was implemented |
170 | | - // for that custom type (which could as well be a user made subtype) |
171 | | - |
172 | | -// else if (annotation.getElementName().equals(NOT_EMPTY) || annotation.getElementName().equals(SIZE)) { |
173 | | -// |
174 | | -// System.out.println("--Field name: " + Signature.getTypeSignatureKind(fieldType)); |
175 | | -// System.out.println("--Field name: " + Signature.getParameterTypes(fieldType)); |
176 | | -// if ( !fieldType.equals(getSignatureFormatOfType(CHAR_SEQUENCE)) && |
177 | | -// !fieldType.contains("List") && |
178 | | -// !fieldType.contains("Set") && |
179 | | -// !fieldType.contains("Collection") && |
180 | | -// !fieldType.contains("Array") && |
181 | | -// !fieldType.contains("Vector") && |
182 | | -// !fieldType.contains("Stack") && |
183 | | -// !fieldType.contains("Queue") && |
184 | | -// !fieldType.contains("Deque") && |
185 | | -// !fieldType.contains("Map")) { |
186 | | -// |
187 | | -// diagnostics.add(new Diagnostic(fieldAnnotationrange, |
188 | | -// "This annotation can only be used on CharSequence, Collection, Array, " |
189 | | -// + "Map type fields.")); |
190 | | -// } |
191 | | -// } |
192 | 173 | } |
193 | 174 | } |
194 | 175 | } |
195 | 176 |
|
| 177 | + /** |
| 178 | + * isSizeOrNonEmptyAllowed |
| 179 | + * This method checks whether the supported types for the Size and NotEmpty annotations are CharSequence, Collection, Map, or array. |
| 180 | + * |
| 181 | + * @param childType |
| 182 | + * @return |
| 183 | + */ |
| 184 | + public static boolean isSizeOrNonEmptyAllowed(PsiType childType) { |
| 185 | + |
| 186 | + if (childType instanceof PsiArrayType) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + if (childType instanceof PsiPrimitiveType) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + PsiClass resolvedClass = PsiUtil.resolveClassInClassTypeOnly(childType); |
| 193 | + return resolvedClass != null && (inheritsFrom(resolvedClass, CHAR_SEQUENCE) |
| 194 | + || inheritsFrom(resolvedClass, COLLECTION_FQ) |
| 195 | + || inheritsFrom(resolvedClass, MAP_FQ)); |
| 196 | + } |
| 197 | + |
196 | 198 | private void checkStringOnly(PsiElement element, List<Diagnostic> diagnostics, String annotationName, boolean isMethod, PsiType type) { |
197 | 199 | if (!type.getCanonicalText().equals(STRING) |
198 | 200 | && !type.getCanonicalText().equals(CHAR_SEQUENCE)) { |
|
0 commit comments