Skip to content

Commit

Permalink
Refatoring FilterImplementationQuickFix and ListenerImplmentationQuic…
Browse files Browse the repository at this point in the history
…kFix and wrote test cases for same
  • Loading branch information
dessina-devasia committed Oct 12, 2023
1 parent 686172b commit 8476a93
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
public class FilterImplementationQuickFix implements IJavaCodeActionParticipant {
private static final Logger LOGGER = Logger.getLogger(FilterImplementationQuickFix.class.getName());

private final static String TITLE_MESSAGE = Messages.getMessage("LetClassImplement");
@Override
public String getParticipantId() {
return FilterImplementationQuickFix.class.getName();
Expand All @@ -60,8 +59,12 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
List<CodeAction> codeActions = new ArrayList<>();
PsiElement node = context.getCoveredNode();
PsiClass parentType = getBinding(node);

if (parentType != null) {
codeActions.add(createCodeAction(context, diagnostic));
String title = Messages.getMessage("LetClassImplement",
parentType.getName(),
ServletConstants.FILTER);
codeActions.add(createCodeAction(context, diagnostic, title));
}
return codeActions;
}
Expand All @@ -72,9 +75,9 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiClass parentType = getBinding(node);
final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class);
//final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class);

assert parentMethod != null;
assert parentType != null;
ChangeCorrectionProposal proposal = new ImplementInterfaceProposal(
null, parentType, context.getASTRoot(),
"jakarta.servlet.Filter", 0, context.getSource().getCompilationUnit());
Expand All @@ -87,8 +90,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
return toResolve;
}

private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE);
private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(title);
codeAction.setRelevance(0);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setKind(CodeActionKind.QuickFix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.CodeActionResolveData;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -54,14 +53,15 @@ public class ListenerImplementationQuickFix implements IJavaCodeActionParticipan

private static final Logger LOGGER = Logger.getLogger(ListenerImplementationQuickFix.class.getName());

private final static String TITLE_MESSAGE = Messages.getMessage("LetClassImplement");
@Override
public String getParticipantId() {
return ListenerImplementationQuickFix.class.getName();
}

public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) {
List<CodeAction> codeActions = new ArrayList<>();
PsiElement node = context.getCoveredNode();
PsiClass parentType = getBinding(node);

String[] listenerConstants = {
ServletConstants.SERVLET_CONTEXT_LISTENER,
Expand All @@ -77,8 +77,8 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
String httpExt = (constant.contains("HTTP")) ? "http." : "";
String interfaceType = "jakarta.servlet." + httpExt + constant;
context.put("interface", interfaceType);

codeActions.add(createCodeAction(context, diagnostic));
String title = Messages.getMessage("LetClassImplement", parentType.getName(), constant);
codeActions.add(createCodeAction(context, diagnostic, title));
}

return codeActions;
Expand All @@ -89,10 +89,10 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiClass parentType = getBinding(node);
final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class);
//final PsiMethod parentMethod = PsiTreeUtil.getParentOfType(node, PsiMethod.class);
String interfaceType = (String) context.get("interface");

assert parentMethod != null;
assert parentType != null;
ChangeCorrectionProposal proposal = new ImplementInterfaceProposal(
null, parentType, context.getASTRoot(), interfaceType, 0,
context.getCompilationUnit());
Expand All @@ -109,8 +109,8 @@ private PsiClass getBinding(PsiElement node) {
return PsiTreeUtil.getParentOfType(node, PsiClass.class);
}

private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE);
private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(title);
codeAction.setRelevance(0);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setKind(CodeActionKind.QuickFix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,6 @@ public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
ServletConstants.DIAGNOSTIC_CODE_DUPLICATE_ATTRIBUTES, null,
DiagnosticSeverity.Error));
}


/* Filter implementation diagnostic check */
PsiClass filterInterface = facade.findClass(ServletConstants.FILTER_FQ_NAME,
GlobalSearchScope.allScope(type.getProject()));
if (!type.isInheritor(filterInterface, true)) {
diagnostics.add(createDiagnostic(type, unit,
Messages.getMessage("WebServletMustImplementFilter"),
ServletConstants.DIAGNOSTIC_CODE_FILTER, null, DiagnosticSeverity.Error));
}

/* Listener implementation diagnostic check */
/*PsiClass listenerInterface = facade.findClass(ServletConstants.LISTENER_FQ_NAME,
GlobalSearchScope.allScope(type.getProject()));
if (!type.isInheritor(filterInterface, true)) {
diagnostics.add(createDiagnostic(type, unit,
Messages.getMessage("WebServletMustImplementListener"),
ServletConstants.DIAGNOSTIC_CODE_LISTENER, null, DiagnosticSeverity.Error));
}*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ WebServletMustExtend = Annotated classes with @WebServlet must extend the HttpSe
WebServletShouldExtend = Annotated classes with @WebServlet should extend the HttpServlet class.
WebServletMustDefine = The @WebServlet annotation must define the attribute 'urlPatterns' or 'value'.
WebServletCannotHaveBoth = The @WebServlet annotation cannot have both 'value' and 'urlPatterns' attributes specified at once.
WebServletMustImplementFilter = Annotated classes with @WebServlet must implement the Filter interface.
WebServletMustImplementListener = Annotated classes with @WebServlet must implement the Listener interface.

# WebSocketDiagnosticsCollector
WebSocketParamType = Invalid parameter type. When using {0}, parameter must be of type: \n- {1}\n- annotated with @PathParams and of type String or any Java primitive type or boxed version thereof.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ public void implementFilter() throws Exception {
JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams();
diagnosticsParams.setUris(Arrays.asList(uri));

Diagnostic d = JakartaForJavaAssert.d(5, 13, 34, "Annotated classes with @WebServlet must implement the Filter interface.",
Diagnostic d = JakartaForJavaAssert.d(5, 13, 32, "Annotated classes with @WebFilter must implement the Filter interface.",
DiagnosticSeverity.Error, "jakarta-servlet", "ImplementFilter");

JakartaForJavaAssert.assertJavaDiagnostics(diagnosticsParams, utils, d);

if (CHECK_CODE_ACTIONS) {
// test associated quick-fix code action
JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d);
TextEdit te = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Filter");
TextEdit te = JakartaForJavaAssert.te(5, 32, 5, 32, " implements Filter");
CodeAction ca = JakartaForJavaAssert.ca(uri, "Let 'DontImplementFilter' implement 'Filter'", d, te);
JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca);
}
Expand All @@ -133,17 +133,37 @@ public void implementListener() throws Exception {
JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams();
diagnosticsParams.setUris(Arrays.asList(uri));

Diagnostic d = JakartaForJavaAssert.d(5, 13, 34, "Annotated classes with @WebServlet must implement the Listener interface.",
Diagnostic d = JakartaForJavaAssert.d(5, 13, 34, "Annotated classes with @WebListener must implement one or more of the following interfaces: ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener, HttpSessionListener, HttpSessionAttributeListener, or HttpSessionIdListener.",
DiagnosticSeverity.Error, "jakarta-servlet", "ImplementListener");

JakartaForJavaAssert.assertJavaDiagnostics(diagnosticsParams, utils, d);

if (CHECK_CODE_ACTIONS) {
// test associated quick-fix code action
JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d);
TextEdit te = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'Listener'", d, te);
JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca);
// test associated quick-fix code action
JakartaJavaCodeActionParams codeActionParams = JakartaForJavaAssert.createCodeActionParams(uri, d);

TextEdit te1 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca1 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'ServletContextListener'", d, te1);

TextEdit te2 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca2 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'ServletContextAttributeListener'", d, te2);

TextEdit te3 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca3 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'ServletRequestListener'", d, te3);

TextEdit te4 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca4 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'ServletRequestAttributeListener'", d, te4);

TextEdit te5 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca5 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'HttpSessionListener'", d, te5);

TextEdit te6 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca6 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'HttpSessionAttributeListener'", d, te6);

TextEdit te7 = JakartaForJavaAssert.te(5, 34, 5, 34, " implements Listener");
CodeAction ca7 = JakartaForJavaAssert.ca(uri, "Let 'DontImplementListener' implement 'HttpSessionIdListener'", d, te7);

JakartaForJavaAssert.assertJavaCodeAction(codeActionParams, utils, ca1, ca2, ca3, ca4, ca5, ca6, ca7);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.openliberty.sample.jakarta.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.annotation.WebFilter;

@WebServlet(name = "filterdemo", urlPatterns = { "/filter" })
@WebFilter(urlPatterns = { "/filter" })
public class DontImplementFilter {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.openliberty.sample.jakarta.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.annotation.WebListener;

@WebServlet(name = "listenerdemo", urlPatterns = { "/listener" })
@WebListener
public class DontImplementListener {

}

0 comments on commit 8476a93

Please sign in to comment.