Skip to content

Commit

Permalink
Used fully qualified name instead of simple name.
Browse files Browse the repository at this point in the history
Enabled tests for ManagedBean and JsonbDiagnosticsCollector
  • Loading branch information
aparnamichael committed Mar 7, 2024
1 parent ef2d7d0 commit 737cad9
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation.
* Copyright (c) 2021, 2024 IBM Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -56,12 +56,9 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
boolean isManagedBean = managedBeanAnnotations.size() > 0;

if (managedBeanAnnotations.size() > 1) {
// convert to simple name
List<String> diagnosticData = managedBeanAnnotations.stream()
.map(annotation -> getSimpleName(annotation)).collect(Collectors.toList());
diagnostics.add(createDiagnostic(type, unit,
Messages.getMessage("ScopeTypeAnnotationsManagedBean"),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(diagnosticData)),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(managedBeanAnnotations)),
DiagnosticSeverity.Error));
}

Expand Down Expand Up @@ -108,12 +105,10 @@ else if (INJECT_FQ_NAME.equals(annotation))
isInjectField = true;
}
if (isProducerField && fieldScopes.size() > 1) {
List<String> diagnosticData = fieldScopes.stream().map(annotation -> getSimpleName(annotation))
.collect(Collectors.toList()); // convert to simple name
diagnosticData.add(PRODUCES);
fieldScopes.add(PRODUCES);
diagnostics.add(createDiagnostic(field, unit,
Messages.getMessage("ScopeTypeAnnotationsProducerField"),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(diagnosticData)),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(fieldScopes)),
DiagnosticSeverity.Error));
}

Expand Down Expand Up @@ -167,12 +162,10 @@ else if (INJECT_FQ_NAME.equals(annotation))
}

if (isProducerMethod && methodScopes.size() > 1) {
List<String> diagnosticData = methodScopes.stream().map(annotation -> getSimpleName(annotation))
.collect(Collectors.toList()); // convert to simple name
diagnosticData.add(PRODUCES);
methodScopes.add(PRODUCES);
diagnostics.add(createDiagnostic(method, unit,
Messages.getMessage("ScopeTypeAnnotationsProducerMethod"),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(diagnosticData)),
DIAGNOSTIC_CODE_SCOPEDECL, (JsonArray) (new Gson().toJsonTree(methodScopes)),
DiagnosticSeverity.Error));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,13 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,

if (parentType != null) {

// Convert the short annotation names to their fully qualified equivalents.
List<String> fqAnnotations = new ArrayList<>();
for (String annotation : annotations) {
fqAnnotations.addAll(getFQAnnotationNames(parentType.getProject(), annotation));
}

List<CodeAction> codeActions = new ArrayList<>();
/**
* for each annotation, choose the current annotation to keep and remove the
* rest since we can have at most one scope annotation.
*/
for (String annotation : fqAnnotations) {
List<String> resultingAnnotations = new ArrayList<>(fqAnnotations);
for (String annotation : annotations) {
List<String> resultingAnnotations = new ArrayList<>(annotations);
resultingAnnotations.remove(annotation);
removeAnnotation(diagnostic, context, codeActions,
resultingAnnotations.toArray(new String[] {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ private boolean createJsonbTransientDiagnostic(PsiJavaFile unit, List<Diagnostic
diagnosticErrorMessage = Messages.getMessage("ErrorMessageJsonbTransientOnField");
else if (code.equals(JsonbConstants.DIAGNOSTIC_CODE_ANNOTATION_TRANSIENT_ACCESSOR))
diagnosticErrorMessage = Messages.getMessage("ErrorMessageJsonbTransientOnAccessor");
// convert to simple name for current tests
List<String> diagnosticData = jsonbAnnotations.stream().map(annotation -> getSimpleName(annotation))
.collect(Collectors.toList());

diagnostics.add(createDiagnostic(member, unit, diagnosticErrorMessage, code,
(JsonArray) (new Gson().toJsonTree(diagnosticData)), DiagnosticSeverity.Error));
(JsonArray) (new Gson().toJsonTree(jsonbAnnotations)), DiagnosticSeverity.Error));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -35,20 +36,15 @@ public class JsonbTransientAnnotationQuickFix extends RemoveMultipleAnnotations
protected List<List<String>> getMultipleRemoveAnnotations(Project project, List<String> annotations) {
List<List<String>> annotationsListsToRemove = new ArrayList<List<String>>();

if (annotations.contains(JsonbConstants.JSONB_TRANSIENT)) {
if (annotations.contains(JsonbConstants.JSONB_TRANSIENT_FQ_NAME)) {
// Provide as one option: Remove JsonbTransient
annotationsListsToRemove.add(Arrays.asList(JsonbConstants.JSONB_TRANSIENT_FQ_NAME));
}

// Provide as another option: Remove all other JsonbAnnotations
annotations.remove(JsonbConstants.JSONB_TRANSIENT);
annotations.remove(JsonbConstants.JSONB_TRANSIENT_FQ_NAME);
if (annotations.size() > 0) {
// Convert the short annotation names to their fully qualified equivalents.
List<String> fqAnnotations = new ArrayList<>();
for (String annotation : annotations) {
fqAnnotations.addAll(getFQAnnotationNames(project, annotation));
}
annotationsListsToRemove.add(fqAnnotations);
annotationsListsToRemove.addAll(Collections.singleton(annotations));
}

return annotationsListsToRemove;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,46 +104,92 @@ public void scopeDeclaration() throws Exception {
Diagnostic d1 = d(12, 16, 17,
"Scope type annotations must be specified by a producer field at most once.",
DiagnosticSeverity.Error, "jakarta-cdi", "InvalidScopeDecl");
d1.setData(new Gson().toJsonTree(Arrays.asList("Dependent", "ApplicationScoped", "Produces")));
d1.setData(new Gson().toJsonTree(Arrays.asList("jakarta.enterprise.context.Dependent",
"jakarta.enterprise.context.ApplicationScoped", "Produces")));

Diagnostic d2 = d(15, 25, 41, "Scope type annotations must be specified by a producer method at most once.",
DiagnosticSeverity.Error, "jakarta-cdi", "InvalidScopeDecl");
d2.setData(new Gson().toJsonTree(Arrays.asList("ApplicationScoped", "RequestScoped", "Produces")));

Diagnostic d3 = d(10, 13, 29, "Scope type annotations must be specified by a managed bean class at most once.",
DiagnosticSeverity.Error, "jakarta-cdi", "InvalidScopeDecl");
d3.setData(new Gson().toJsonTree(Arrays.asList("ApplicationScoped", "RequestScoped")));
Diagnostic d2 = d(15, 25, 41, "Scope type annotations must be specified " +
"by a producer method at most once.", DiagnosticSeverity.Error, "jakarta-cdi",
"InvalidScopeDecl");
d2.setData(new Gson().toJsonTree(Arrays.asList("jakarta.enterprise.context.ApplicationScoped",
"jakarta.enterprise.context.RequestScoped", "Produces")));

Diagnostic d3 = d(10, 13, 29, "Scope type annotations must be " +
"specified by a managed bean class at most once.", DiagnosticSeverity.Error,
"jakarta-cdi", "InvalidScopeDecl");
d3.setData(new Gson().toJsonTree(Arrays.asList("jakarta.enterprise.context.ApplicationScoped",
"jakarta.enterprise.context.RequestScoped")));

assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3);

if (CHECK_CODE_ACTIONS) {
// Assert for the diagnostic d1
JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d1);
TextEdit te1 = te(11, 33, 12, 4, "");
TextEdit te2 = te(11, 14, 11, 33, "");
CodeAction ca1 = ca(uri, "Remove @ApplicationScoped", d1, te2);
CodeAction ca2 = ca(uri, "Remove @Dependent", d1, te1);

assertJavaCodeAction(codeActionParams1, utils, ca1, ca2);

// Assert for the diagnostic d2
JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d2);
TextEdit te3 = te(14, 33, 15, 4, "");
TextEdit te4 = te(14, 14, 14, 33, "");
CodeAction ca3 = ca(uri, "Remove @RequestScoped", d2, te3);
CodeAction ca4 = ca(uri, "Remove @ApplicationScoped", d2, te4);

assertJavaCodeAction(codeActionParams2, utils, ca3, ca4);

// Assert for the diagnostic d3
JakartaJavaCodeActionParams codeActionParams3 = createCodeActionParams(uri, d3);
TextEdit te5 = te(9, 19, 10, 0, "");
TextEdit te6 = te(9, 0, 9, 19, "");
CodeAction ca5 = ca(uri, "Remove @RequestScoped", d3, te5);
CodeAction ca6 = ca(uri, "Remove @ApplicationScoped", d3, te6);

assertJavaCodeAction(codeActionParams3, utils, ca5, ca6);
}
//TODO: Uncomment this line of code once all the quickfix changes are done.
// if (CHECK_CODE_ACTIONS) {
// Assert for the diagnostic d1
JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d1);
String newText = "package io.openliberty.sample.jakarta.cdi;\n\nimport java.util.Collections;" +
"\nimport java.util.List;\n\nimport jakarta.enterprise.inject.Produces;\n\n" +
"import jakarta.enterprise.context.*;\n\n@ApplicationScoped @RequestScoped\n" +
"public class ScopeDeclaration {\n @Produces @Dependent\n private int n;\n " +
" \n @Produces @ApplicationScoped @RequestScoped\n public List<Integer> getAllProductIds() " +
"{\n return Collections.emptyList();\n }\n}";

String newText1 = "package io.openliberty.sample.jakarta.cdi;\n\n" +
"import java.util.Collections;\nimport java.util.List;\n\n" +
"import jakarta.enterprise.inject.Produces;\n\nimport jakarta.enterprise.context.*;" +
"\n\n@ApplicationScoped @RequestScoped\npublic class ScopeDeclaration {\n " +
" @Produces @ApplicationScoped\n private int n;\n \n @Produces @ApplicationScoped" +
" @RequestScoped\n public List<Integer> getAllProductIds() {\n " +
"return Collections.emptyList();\n }\n}";

TextEdit te1 = te(0, 0, 18, 1, newText);
TextEdit te2 = te(0, 0, 18, 1, newText1);
CodeAction ca1 = ca(uri, "Remove @ApplicationScoped", d1, te1);
CodeAction ca2 = ca(uri, "Remove @Dependent", d1, te2);
assertJavaCodeAction(codeActionParams1, utils, ca1,ca2);

// Assert for the diagnostic d2
JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d2);
newText = "package io.openliberty.sample.jakarta.cdi;\n\nimport java.util.Collections;\n" +
"import java.util.List;\n\nimport jakarta.enterprise.inject.Produces;\n\n" +
"import jakarta.enterprise.context.*;\n\n@ApplicationScoped @RequestScoped\n" +
"public class ScopeDeclaration {\n @Produces @ApplicationScoped @Dependent\n " +
" private int n;\n \n @Produces @RequestScoped\n public List<Integer> getAllProductIds()" +
" {\n return Collections.emptyList();\n }\n}";

newText1 = "package io.openliberty.sample.jakarta.cdi;\n\nimport java.util.Collections;\nimport " +
"java.util.List;\n\nimport jakarta.enterprise.inject.Produces;\n\nimport " +
"jakarta.enterprise.context.*;\n\n@ApplicationScoped @RequestScoped\npublic class ScopeDeclaration" +
" {\n @Produces @ApplicationScoped @Dependent\n private int n;\n \n " +
" @Produces @ApplicationScoped\n public List<Integer> getAllProductIds() {\n " +
" return Collections.emptyList();\n }\n}";

TextEdit te3 = te(0, 0, 18, 1, newText1);
TextEdit te4 = te(0, 0, 18, 1, newText);
CodeAction ca3 = ca(uri, "Remove @RequestScoped", d2, te3);
CodeAction ca4 = ca(uri, "Remove @ApplicationScoped", d2, te4);
assertJavaCodeAction(codeActionParams2, utils, ca4, ca3);

// Assert for the diagnostic d3
JakartaJavaCodeActionParams codeActionParams3 = createCodeActionParams(uri, d3);
newText = "package io.openliberty.sample.jakarta.cdi;\n\nimport java.util.Collections;\n" +
"import java.util.List;\n\nimport jakarta.enterprise.inject.Produces;\n\n" +
"import jakarta.enterprise.context.*;\n\n@RequestScoped\npublic class ScopeDeclaration {\n " +
" @Produces @ApplicationScoped @Dependent\n private int n;\n \n " +
" @Produces @ApplicationScoped @RequestScoped\n public List<Integer> getAllProductIds() {\n " +
" return Collections.emptyList();\n }\n}";

newText1 = "package io.openliberty.sample.jakarta.cdi;\n\nimport java.util.Collections;" +
"\nimport java.util.List;\n\nimport jakarta.enterprise.inject.Produces;\n\n" +
"import jakarta.enterprise.context.*;\n\n@ApplicationScoped\npublic class ScopeDeclaration {\n " +
" @Produces @ApplicationScoped @Dependent\n private int n;\n \n " +
"@Produces @ApplicationScoped @RequestScoped\n public List<Integer> getAllProductIds() {\n " +
" return Collections.emptyList();\n }\n}";

TextEdit te5 = te(0, 0, 18, 1, newText1);
TextEdit te6 = te(0, 0, 18, 1, newText);
CodeAction ca5 = ca(uri, "Remove @RequestScoped", d3, te5);
CodeAction ca6 = ca(uri, "Remove @ApplicationScoped", d3, te6);
assertJavaCodeAction(codeActionParams3, utils, ca6, ca5);
// }
}

@Test
Expand Down
Loading

0 comments on commit 737cad9

Please sign in to comment.