Skip to content

Commit

Permalink
Added diagnostics and quickfix for a managed bean class of generic t…
Browse files Browse the repository at this point in the history
…ype (#225)

* Added diagnostics and quickfix for a managed bean class of generic type

* Added integration test for generic managed bean class
  • Loading branch information
KidoVin01 authored Dec 2, 2021
1 parent 4e2b2e4 commit b867f89
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ManagedBeanConstants {
public static final String DISPOSES = "Disposes";
public static final String OBSERVES = "Observes";
public static final String OBSERVES_ASYNC = "ObservesAsync";
public static final String DEPENDENT = "Dependent";


public static final String DIAGNOSTIC_SOURCE = "jakarta-cdi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import java.util.Set;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IAnnotation;
Expand Down Expand Up @@ -248,7 +246,20 @@ else if (!hasParameterizedInjectConstructor)
CONSTRUCTOR_DIAGNOSTIC_CODE);
diagnostics.add(diagnostic);
}

}
}

/**
* 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 -> !annotation.equals("Dependent"));

if (isClassGeneric && !isDependent) {
diagnostics.add(createDiagnostic(type, unit, "Managed bean class of generic type must have scope @Dependent",
DIAGNOSTIC_CODE));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package org.eclipse.lsp4jakarta.jdt.core.di;

import static org.eclipse.lsp4jakarta.jdt.core.di.DependencyInjectionConstants.DIAGNOSTIC_SOURCE;
import static org.eclipse.lsp4jakarta.jdt.core.di.DependencyInjectionConstants.SEVERITY;
import static org.eclipse.lsp4jakarta.jdt.core.di.DependencyInjectionConstants.*;

import java.util.ArrayList;
Expand All @@ -37,7 +35,6 @@
import org.eclipse.lsp4jakarta.jdt.core.JDTUtils;
import org.eclipse.lsp4jakarta.jdt.core.JakartaCorePlugin;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;

/**
*
Expand Down Expand Up @@ -101,7 +98,6 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
alltypes = unit.getAllTypes();
for (IType type : alltypes) {
allAnnotations = type.getAnnotations();

IField[] allFields = type.getFields();
for (IField field : allFields) {
int fieldFlags = field.getFlags();
Expand Down Expand Up @@ -171,7 +167,6 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
for (IType type : alltypes) {
List<IMethod> constructorMethods = Arrays.stream(type.getMethods()).filter(this::isConstructorMethod)
.collect(Collectors.toList());

// there are no constructors
if (constructorMethods.size() == 0)
return;
Expand All @@ -180,6 +175,7 @@ public void collectDiagnostics(ICompilationUnit unit, List<Diagnostic> diagnosti
int numInjectedConstructors = 0;
List<IMethod> injectedConstructors = new ArrayList<IMethod>();
for (IMethod m : constructorMethods) {

hasInjectConstructor = Arrays.stream(m.getAnnotations())
.map(annotation -> annotation.getElementName())
.anyMatch(annotation -> annotation.equals("Inject"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jakarta.enterprise.context.*;

@RequestScoped
public class ManagedBean {
public class ManagedBean<T> {
public int a;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,27 @@ public void managedBeanAnnotations() throws Exception {
diagnosticsParams.setUris(Arrays.asList(uri));

// test expected diagnostic
Diagnostic d = d(6, 12, 13,
Diagnostic d1 = d(6, 12, 13,
"A managed bean with a non-static public field must not declare any scope other than @Dependent",
DiagnosticSeverity.Error, "jakarta-cdi", "InvalidManagedBeanAnnotation");

Diagnostic d2 = d(5, 13, 24,
"Managed bean class of generic type must have scope @Dependent",
DiagnosticSeverity.Error, "jakarta-cdi", "InvalidManagedBeanAnnotation");

assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d);
assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d1, d2);

// test expected quick-fix
JakartaJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
TextEdit te = te(4, 0, 5, 0, "@Dependent\n");
CodeAction ca = ca(uri, "Replace current scope with @Dependent", d, te);
assertJavaCodeAction(codeActionParams, JDT_UTILS, ca);
// Assert for the diagnostic d1
JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d1);
TextEdit te1 = te(4, 0, 5, 0, "@Dependent\n");
CodeAction ca1 = ca(uri, "Replace current scope with @Dependent", d1, te1);
assertJavaCodeAction(codeActionParams1, JDT_UTILS, ca1);

// Assert for the diagnostic d2
JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d2);
TextEdit te2 = te(4, 0, 5, 0, "@Dependent\n");
CodeAction ca2 = ca(uri, "Replace current scope with @Dependent", d2, te2);
assertJavaCodeAction(codeActionParams2, JDT_UTILS, ca2);
}

@Test
Expand Down

0 comments on commit b867f89

Please sign in to comment.