Skip to content

Commit

Permalink
Properly handle a non-static field with the @dependent annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipse-lsp4jakarta-bot committed Aug 14, 2023
1 parent 110b11c commit 70f167e
Showing 1 changed file with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
package org.eclipse.lsp4jakarta.jdt.core.cdi;

import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.CONSTRUCTOR_DIAGNOSTIC_CODE;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DEPENDENT;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DEPENDENT_FQ_NAME;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DIAGNOSTIC_CODE;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DIAGNOSTIC_CODE_SCOPEDECL;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DIAGNOSTIC_SOURCE;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DISPOSES;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.DISPOSES_FQ_NAME;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.INJECT;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.INJECT_FQ_NAME;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.INVALID_INJECT_PARAMS_FQ;
import static org.eclipse.lsp4jakarta.jdt.core.cdi.ManagedBeanConstants.OBSERVES_ASYNC_FQ_NAME;
Expand Down Expand Up @@ -74,8 +71,8 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
IType[] types = unit.getAllTypes();
String[] scopeFQNames = SCOPE_FQ_NAMES.toArray(String[]::new);
for (IType type : types) {
List<String> managedBeanAnnotations = getMatchedJavaElementNames(type, Stream.of(type.getAnnotations())
.map(annotation -> annotation.getElementName()).toArray(String[]::new),
List<String> managedBeanAnnotations = getMatchedJavaElementNames(type,
Stream.of(type.getAnnotations()).map(annotation -> annotation.getElementName()).toArray(String[]::new),
scopeFQNames);
boolean isManagedBean = managedBeanAnnotations.size() > 0;

Expand All @@ -93,9 +90,6 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
IField fields[] = type.getFields();
for (IField field : fields) {
int fieldFlags = field.getFlags();
String[] annotationNames = Stream.of(field.getAnnotations())
.map(annotation -> annotation.getElementName()).toArray(String[]::new);
List<String> fieldScopes = getMatchedJavaElementNames(type, annotationNames, scopeFQNames);

/**
* If a managed bean has a non-static public field, it must have
Expand All @@ -106,11 +100,9 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
* https://jakarta.ee/specifications/cdi/3.0/jakarta-cdi-spec-3.0.html#managed_beans
*/
if (isManagedBean && Flags.isPublic(fieldFlags) && !Flags.isStatic(fieldFlags)
&& (fieldScopes.size() != 1 || !fieldScopes.get(0).equals(DEPENDENT_FQ_NAME))) {
&& !managedBeanAnnotations.contains(DEPENDENT_FQ_NAME)) {
diagnostics.add(createDiagnostic(field, unit,
Messages.getMessage("ManagedBeanWithNonStaticPublicField"),
DIAGNOSTIC_CODE, null,
DiagnosticSeverity.Error));
Messages.getMessage("ManagedBeanWithNonStaticPublicField"), DIAGNOSTIC_CODE, null, DiagnosticSeverity.Error));
}

/**
Expand All @@ -122,6 +114,9 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
*
* Here we only look at the fields.
*/
String[] annotationNames = Stream.of(field.getAnnotations())
.map(annotation -> annotation.getElementName()).toArray(String[]::new);
List<String> fieldScopes = getMatchedJavaElementNames(type, annotationNames, scopeFQNames);
List<String> fieldInjects = getMatchedJavaElementNames(type, annotationNames, injectAnnotations);
boolean isProducerField = false, isInjectField = false;
for (String annotation : fieldInjects) {
Expand Down Expand Up @@ -257,15 +252,15 @@ else if (INJECT_FQ_NAME.equals(annotation))
CONSTRUCTOR_DIAGNOSTIC_CODE, null, DiagnosticSeverity.Error));
}
}

/**
* If a managed bean class is of generic type, it must be annotated with @Dependent
*/
if (isManagedBean) {
boolean isClassGeneric = type.getTypeParameters().length != 0;
boolean isDependent = managedBeanAnnotations.stream()
.anyMatch(annotation -> DEPENDENT_FQ_NAME.equals(annotation));

if (isClassGeneric && !isDependent) {
diagnostics.add(createDiagnostic(type, unit, Messages.getMessage("ManagedBeanGenericType"),
DIAGNOSTIC_CODE, null, DiagnosticSeverity.Error));
Expand Down Expand Up @@ -307,7 +302,7 @@ else if (INJECT_FQ_NAME.equals(annotation))
int numDisposes = 0;
Set<String> invalidAnnotations = new TreeSet<>();
ILocalVariable[] params = method.getParameters();

for (ILocalVariable param : params) {
IAnnotation[] annotations = param.getAnnotations();
for (IAnnotation annotation : annotations) {
Expand All @@ -321,18 +316,20 @@ else if (INJECT_FQ_NAME.equals(annotation))
}
}
}

if(numDisposes == 0) continue;
if(numDisposes > 1) {

if (numDisposes == 0) {
continue;
}

if (numDisposes > 1) {
diagnostics.add(createDiagnostic(method, unit,
Messages.getMessage("ManagedBeanDisposeOneParameter"),
ManagedBeanConstants.DIAGNOSTIC_CODE_REDUNDANT_DISPOSES, null,
DiagnosticSeverity.Error));
}

if(!invalidAnnotations.isEmpty()) {
diagnostics.add(createDiagnostic(method, unit,
createInvalidDisposesLabel(invalidAnnotations),

if (!invalidAnnotations.isEmpty()) {
diagnostics.add(createDiagnostic(method, unit, createInvalidDisposesLabel(invalidAnnotations),
ManagedBeanConstants.DIAGNOSTIC_CODE_INVALID_DISPOSES_PARAM, null,
DiagnosticSeverity.Error));
}
Expand All @@ -341,12 +338,12 @@ else if (INJECT_FQ_NAME.equals(annotation))
}

} catch (JavaModelException e) {
JakartaCorePlugin.logException("Cannot calculate diagnostics", e);
JakartaCorePlugin.logException("Cannot calculate diagnostics", e);
}
}

private void invalidParamsCheck(ICompilationUnit unit, List<Diagnostic> diagnostics, IType type, String target,
String diagnosticCode) throws JavaModelException {
private void invalidParamsCheck(ICompilationUnit unit, List<Diagnostic> diagnostics, IType type, String target, String diagnosticCode)
throws JavaModelException {
for (IMethod method : type.getMethods()) {
IAnnotation targetAnnotation = null;

Expand Down

0 comments on commit 70f167e

Please sign in to comment.