From 4c276548a0ade92fdd0c0b83e948643e252bffcd Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Wed, 23 Mar 2022 22:08:16 -0700 Subject: [PATCH 01/11] Add Relevant Constants --- .../core/websocket/WebSocketConstants.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 62c13a35..1658de35 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -47,7 +47,7 @@ public class WebSocketConstants { /* Diagnostic codes */ public static final String DIAGNOSTIC_CODE_ON_OPEN_INVALID_PARAMS = "OnOpenChangeInvalidParam"; public static final String DIAGNOSTIC_CODE_ON_CLOSE_INVALID_PARAMS = "OnCloseChangeInvalidParam"; - + public static final String DIAGNOSTIC_CODE_ON_MESSAGE_INVALID_PARAMS = "OnMessageChangeInvalidParam"; /* https://jakarta.ee/specifications/websocket/2.0/websocket-spec-2.0.html#applications */ // Class Level Annotations @@ -63,19 +63,33 @@ public class WebSocketConstants { /* Annotations */ public static final String ON_OPEN = "OnOpen"; public static final String ON_CLOSE = "OnClose"; + public static final String ON_MESSAGE = "OnMessage"; public static final String IS_ANNOTATION = "isAnnotation"; /* Types */ public static final String PATH_PARAM_ANNOTATION = "PathParam"; - // For OnOpen annotation + /* For OnOpen annotation */ public static final Set ON_OPEN_PARAM_OPT_TYPES= new HashSet<>(Arrays.asList("jakarta.websocket.EndpointConfig", "jakarta.websocket.Session")); public static final Set RAW_ON_OPEN_PARAM_OPT_TYPES= new HashSet<>(Arrays.asList("EndpointConfig", "Session")); - + /* For OnClose annotation */ public static final Set ON_CLOSE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.CloseReason", "jakarta.websocket.Session")); public static final Set RAW_ON_CLOSE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("CloseReason", "Session")); + /* For OnMessage annotation */ + public static final Set ON_MESSAGE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.Session")); + public static final Set RAW_ON_MESSAGE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("Session")); + /* For OnMessage (Text) annotation */ + public static final Set ON_MESSAGE_TEXT_TYPES = new HashSet<>(Arrays.asList("java.lang.String", "java.io.Reader")); + public static final Set RAW_ON_MESSAGE_TEXT_TYPES = new HashSet<>(Arrays.asList("String", "Reader")); + /* For OnMessage (Text) annotation */ + public static final Set ON_MESSAGE_BINARY_TYPES = new HashSet<>(Arrays.asList("java.nio.ByteBuffer", "java.io.InputStream")); + public static final Set RAW_ON_MESSAGE_BINARY_TYPES = new HashSet<>(Arrays.asList("ByteBuffer", "InputStream")); + /* For OnMessage (Text) annotation */ + public static final Set ON_MESSAGE_PONG_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.PongMessage")); + public static final Set RAW_ON_MESSAGE_PONG_TYPES = new HashSet<>(Arrays.asList("PongMessage")); + /* Wrapper Objects */ public static final Set RAW_WRAPPER_OBJS = new HashSet<>(Arrays.asList("String", "Boolean", "Integer", "Long", "Double", "Float")); public static final Set WRAPPER_OBJS = RAW_WRAPPER_OBJS.stream().map(raw -> "java.lang.".concat(raw)).collect(Collectors.toSet()); From 3b832f7df18d370efb5fed3a4a33cf7194ca3170 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Thu, 24 Mar 2022 02:20:48 -0700 Subject: [PATCH 02/11] Add Initial Implementation Detail --- .../core/websocket/WebSocketConstants.java | 1 + .../WebSocketDiagnosticsCollector.java | 86 ++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 1658de35..7b1408bc 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -36,6 +36,7 @@ public class WebSocketConstants { public static final String PATHPARAM_DIAGNOSTIC_CODE = "ChangePathParamValue"; public static final String ANNOTATION_VALUE = "value"; + public static final String ANNOTATION_DECODER = "decoders"; public static final String URI_SEPARATOR = "/"; public static final String CURLY_BRACE_START = "{"; diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java index a2ef4445..ecdb4bc0 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java @@ -92,7 +92,8 @@ public void collectDiagnostics(ICompilationUnit unit, List diagnosti WebSocketConstants.RAW_ON_OPEN_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_OPEN_INVALID_PARAMS, unit, diagnostics); invalidParamsCheck(type, WebSocketConstants.ON_CLOSE, WebSocketConstants.ON_CLOSE_PARAM_OPT_TYPES, WebSocketConstants.RAW_ON_CLOSE_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_CLOSE_INVALID_PARAMS, unit, diagnostics); - + onMessageParamsCheck(type, WebSocketConstants.ON_MESSAGE, WebSocketConstants.ON_MESSAGE_PARAM_OPT_TYPES, + WebSocketConstants.RAW_ON_MESSAGE_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_MESSAGE_INVALID_PARAMS, unit, diagnostics); // PathParam URI Mismatch Warning Diagnostic uriMismatchWarningCheck(type, diagnostics, unit); } @@ -159,6 +160,89 @@ private void invalidParamsCheck(IType type, String methodAnnotTarget, Set specialParamTypes, Set rawSpecialParamTypes, String diagnosticCode, ICompilationUnit unit, + List diagnostics) throws JavaModelException { + + boolean decoderIncluded = checkEndpointHasDecoder(type); + + IMethod[] allMethods = type.getMethods(); + + for (IMethod method : allMethods) { + IAnnotation[] allAnnotations = method.getAnnotations(); + + for (IAnnotation annotation : allAnnotations) { + if (annotation.getElementName().equals(methodAnnotTarget)) { + ILocalVariable[] allParams = method.getParameters(); + + for (ILocalVariable param : allParams) { + String signature = param.getTypeSignature(); + String formatSignature = signature.replace("/", "."); + String resolvedTypeName = JavaModelUtil.getResolvedTypeName(formatSignature, type); + + boolean isPrimitive = JavaModelUtil.isPrimitive(formatSignature); + boolean isSpecialType; + boolean isPrimWrapped; + + if (resolvedTypeName != null) { + isSpecialType = specialParamTypes.contains(resolvedTypeName); + isPrimWrapped = isWrapper(resolvedTypeName); + } else { + String simpleParamType = Signature.getSignatureSimpleName(signature); + isSpecialType = rawSpecialParamTypes.contains(simpleParamType); + isPrimWrapped = isWrapper(simpleParamType); + } + + // check parameters valid types + if (!(isSpecialType || isPrimWrapped || isPrimitive)) { + Diagnostic diagnostic = createDiagnostic(param, unit, + createParamTypeDiagMsg(specialParamTypes, methodAnnotTarget), + diagnosticCode); + diagnostics.add(diagnostic); + continue; + } + + if (!isSpecialType) { + // check that if parameter is not a specialType, it has a @PathParam annotation + IAnnotation[] param_annotations = param.getAnnotations(); + boolean hasPathParamAnnot = Arrays.asList(param_annotations).stream().anyMatch( + annot -> annot.getElementName().equals(WebSocketConstants.PATH_PARAM_ANNOTATION)); + + if (!hasPathParamAnnot) { + Diagnostic diagnostic = createDiagnostic(param, unit, + WebSocketConstants.DIAGNOSTIC_PATH_PARAMS_ANNOT_MISSING, + WebSocketConstants.DIAGNOSTIC_CODE_PATH_PARMS_ANNOT); + diagnostics.add(diagnostic); + } + } + } + } + } + } + } + + /** + * Checks if a WebSocket EndPoint annotation contains custom decoders + * + * @param type representing the class + * @return boolean to represent if decoders are present + * @throws JavaModelException + */ + private boolean checkEndpointHasDecoder(IType type) throws JavaModelException { + IAnnotation[] endpointAnnotations = type.getAnnotations(); + for (IAnnotation annotation : endpointAnnotations) { + if (annotation.getElementName().equals(WebSocketConstants.SERVER_ENDPOINT_ANNOTATION) + || annotation.getElementName().equals(WebSocketConstants.CLIENT_ENDPOINT_ANNOTATION)) { + IMemberValuePair[] valuePairs = annotation.getMemberValuePairs(); + for (IMemberValuePair pair : valuePairs) { + if (pair.getMemberName().equals(WebSocketConstants.ANNOTATION_DECODER)) { + return true; + } + } + } + } + return false; + } /** * Creates a warning diagnostic if a PathParam annotation does not match any From d626cf8a156fccc58ec3f8879d9d21020cc1fac5 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 22:54:56 -0700 Subject: [PATCH 03/11] Updated MAP/SET Implementation --- .../core/websocket/WebSocketConstants.java | 30 ++++ .../WebSocketDiagnosticsCollector.java | 150 +++++++++++++----- .../websocket/InvalidParamTypeBinary.java | 32 ++++ .../websocket/InvalidParamTypePong.java | 32 ++++ .../websocket/InvalidParamTypeText.java | 32 ++++ ...amType.java => OnMessageInvalidParam.java} | 2 +- .../jdt/websocket/JakartaWebSocketTest.java | 58 +++++++ 7 files changed, 298 insertions(+), 38 deletions(-) create mode 100644 jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java create mode 100644 jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java create mode 100644 jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java rename jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/{InvalidParamType.java => OnMessageInvalidParam.java} (96%) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 7b1408bc..7c5117a2 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -94,6 +94,36 @@ public class WebSocketConstants { public static final Set RAW_WRAPPER_OBJS = new HashSet<>(Arrays.asList("String", "Boolean", "Integer", "Long", "Double", "Float")); public static final Set WRAPPER_OBJS = RAW_WRAPPER_OBJS.stream().map(raw -> "java.lang.".concat(raw)).collect(Collectors.toSet()); + public static final String RAW_STRING_TYPE = "String"; + public static final String STRING_OBJ = "java.lang.String"; + + public static final String RAW_BOOLEAN_TYPE = "boolean"; + public static final String BOOLEAN_OBJ = "java.lang.Boolean"; + + public static final String RAW_BYTEBUFFER_OBJ = "ByteBuffer"; + public static final String BYTEBUFFER_OBJ = "java.nio.ByteBuffer"; + // Messages public static final String PARAM_TYPE_DIAG_MSG = "Invalid parameter type. When using %s, parameter must be of type: \n- %s\n- annotated with @PathParams and of type String or any Java primitive type or boxed version thereof"; + + public static final String TEXT_PARAMS_DIAG_MSG = "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + + " - String to receive the whole message\r\n" + + " - Java primitive or class equivalent to receive the whole message converted to that type\r\n" + + " - String and boolean pair to receive the message in parts\r\n" + + " - Reader to receive the whole message as a blocking stream\r\n" + + " - any object parameter for which the endpoint has a text decoder (Decoder.Text or Decoder.TextStream)"; + + public static final String BINARY_PARAMS_DIAG_MSG = "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + + " - byte[] or ByteBuffer to receive the whole message\r\n" + + " - byte[] and boolean pair, or ByteBuffer and boolean pair to receive the message in parts\r\n" + + " - InputStream to receive the whole message as a blocking stream\r\n" + + " - any object parameter for which the endpoint has a binary decoder (Decoder.Binary or Decoder.BinaryStream)"; + + public static final String PONG_PARAMS_DIAG_MSG = "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + + " - PongMessage for handling pong messages"; + + public static final String INVALID_PARAMS_DIAG_MSG = "Invalid parameter type. Please see @OnMessage API Specification for valid parameter specifications."; + + // Enums + public enum MESSAGE_FORMAT {TEXT, BINARY, PONG, INVALID}; } diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java index ecdb4bc0..ce3eb32b 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java @@ -17,7 +17,9 @@ import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.ArrayList; @@ -92,8 +94,7 @@ public void collectDiagnostics(ICompilationUnit unit, List diagnosti WebSocketConstants.RAW_ON_OPEN_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_OPEN_INVALID_PARAMS, unit, diagnostics); invalidParamsCheck(type, WebSocketConstants.ON_CLOSE, WebSocketConstants.ON_CLOSE_PARAM_OPT_TYPES, WebSocketConstants.RAW_ON_CLOSE_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_CLOSE_INVALID_PARAMS, unit, diagnostics); - onMessageParamsCheck(type, WebSocketConstants.ON_MESSAGE, WebSocketConstants.ON_MESSAGE_PARAM_OPT_TYPES, - WebSocketConstants.RAW_ON_MESSAGE_PARAM_OPT_TYPES, WebSocketConstants.DIAGNOSTIC_CODE_ON_MESSAGE_INVALID_PARAMS, unit, diagnostics); + onMessageParamsCheck(type, unit, diagnostics); // PathParam URI Mismatch Warning Diagnostic uriMismatchWarningCheck(type, diagnostics, unit); } @@ -161,62 +162,137 @@ private void invalidParamsCheck(IType type, String methodAnnotTarget, Set specialParamTypes, Set rawSpecialParamTypes, String diagnosticCode, ICompilationUnit unit, - List diagnostics) throws JavaModelException { + private void onMessageParamsCheck(IType type, ICompilationUnit unit, List diagnostics) throws JavaModelException { - boolean decoderIncluded = checkEndpointHasDecoder(type); + boolean endpointDecodersSpecified = checkEndpointHasDecoder(type); IMethod[] allMethods = type.getMethods(); - for (IMethod method : allMethods) { + IAnnotation[] allAnnotations = method.getAnnotations(); - for (IAnnotation annotation : allAnnotations) { - if (annotation.getElementName().equals(methodAnnotTarget)) { + if (annotation.getElementName().equals(WebSocketConstants.ON_MESSAGE)) { + + Set seenSpecialParams = new HashSet<>(); + Map seenMessageParams = new HashMap<>(); + ILocalVariable[] allParams = method.getParameters(); - for (ILocalVariable param : allParams) { + String signature = param.getTypeSignature(); String formatSignature = signature.replace("/", "."); String resolvedTypeName = JavaModelUtil.getResolvedTypeName(formatSignature, type); - - boolean isPrimitive = JavaModelUtil.isPrimitive(formatSignature); + String finalTypeName; boolean isSpecialType; - boolean isPrimWrapped; - + + IAnnotation[] param_annotations = param.getAnnotations(); + boolean hasPathParamAnnot = Arrays.asList(param_annotations).stream().anyMatch( + annot -> annot.getElementName().equals(WebSocketConstants.PATH_PARAM_ANNOTATION)); + if (resolvedTypeName != null) { - isSpecialType = specialParamTypes.contains(resolvedTypeName); - isPrimWrapped = isWrapper(resolvedTypeName); + isSpecialType = WebSocketConstants.ON_MESSAGE_PARAM_OPT_TYPES.contains(resolvedTypeName); + finalTypeName = resolvedTypeName; } else { String simpleParamType = Signature.getSignatureSimpleName(signature); - isSpecialType = rawSpecialParamTypes.contains(simpleParamType); - isPrimWrapped = isWrapper(simpleParamType); + isSpecialType = WebSocketConstants.RAW_ON_MESSAGE_PARAM_OPT_TYPES.contains(simpleParamType); + finalTypeName = simpleParamType; } - - // check parameters valid types - if (!(isSpecialType || isPrimWrapped || isPrimitive)) { - Diagnostic diagnostic = createDiagnostic(param, unit, - createParamTypeDiagMsg(specialParamTypes, methodAnnotTarget), - diagnosticCode); - diagnostics.add(diagnostic); - continue; + + if (isSpecialType) { + if (seenSpecialParams.contains(finalTypeName)){ + Diagnostic duplicateSpecialParamDiagnostic = createDiagnostic(param, unit, + "Only one optional parameter of this type is allowed for a method with the @OnMessage annotation.", + "OnMessageDuplicateOptionalParam"); + diagnostics.add(duplicateSpecialParamDiagnostic); + } else { + seenSpecialParams.add(finalTypeName); + } + } else if (hasPathParamAnnot) { + boolean isPrimitive = JavaModelUtil.isPrimitive(formatSignature); + boolean isPrimWrapped = isWrapper(finalTypeName); + if (!isPrimitive && !isPrimWrapped) { + Diagnostic invalidPathParamDiagnostic = createDiagnostic(param, unit, + "Only String and Java primitive types are allowed to be annotated with the @PathParam annotation.", + "OnMessageInvalidPathParam"); + diagnostics.add(invalidPathParamDiagnostic); + } + } else { + if (seenMessageParams.containsKey(finalTypeName)) { + Diagnostic duplicateMessageParamDiagnostic = createDiagnostic(param, unit, + "Multiple parameters of this type are not allowed for a method with the @OnMessage annotation.", + "OnMessageDuplicateMessageParam"); + diagnostics.add(duplicateMessageParamDiagnostic); + } else { + seenMessageParams.put(finalTypeName, param); + } } - - if (!isSpecialType) { - // check that if parameter is not a specialType, it has a @PathParam annotation - IAnnotation[] param_annotations = param.getAnnotations(); - boolean hasPathParamAnnot = Arrays.asList(param_annotations).stream().anyMatch( - annot -> annot.getElementName().equals(WebSocketConstants.PATH_PARAM_ANNOTATION)); - - if (!hasPathParamAnnot) { - Diagnostic diagnostic = createDiagnostic(param, unit, - WebSocketConstants.DIAGNOSTIC_PATH_PARAMS_ANNOT_MISSING, - WebSocketConstants.DIAGNOSTIC_CODE_PATH_PARMS_ANNOT); - diagnostics.add(diagnostic); + } + + WebSocketConstants.MESSAGE_FORMAT methodType = null; + + Set intersection = new HashSet<>(seenMessageParams.keySet()); + Set difference = new HashSet<>(seenMessageParams.keySet()); + + intersection.retainAll(WebSocketConstants.ON_MESSAGE_TEXT_TYPES); + if (intersection.size() > 0) { + // TEXT Message + methodType = WebSocketConstants.MESSAGE_FORMAT.TEXT; + difference.removeAll(WebSocketConstants.ON_MESSAGE_TEXT_TYPES); + } else { + intersection = new HashSet<>(seenMessageParams.keySet()); + intersection.retainAll(WebSocketConstants.ON_MESSAGE_BINARY_TYPES); + if (intersection.size() > 0) { + // BINARY Message + methodType = WebSocketConstants.MESSAGE_FORMAT.BINARY; + difference.removeAll(WebSocketConstants.ON_MESSAGE_BINARY_TYPES); + } else { + intersection = new HashSet<>(seenMessageParams.keySet()); + intersection.retainAll(WebSocketConstants.ON_MESSAGE_PONG_TYPES); + if (intersection.size() > 0) { + // PONG Message + methodType = WebSocketConstants.MESSAGE_FORMAT.PONG; + difference.removeAll(WebSocketConstants.ON_MESSAGE_PONG_TYPES); + } else { + // Invalid Message + methodType = WebSocketConstants.MESSAGE_FORMAT.INVALID; } } } + + switch (methodType) { + case TEXT: + addDiagnosticsForInvalidMessageParams(difference, seenMessageParams, diagnostics, unit, + true, endpointDecodersSpecified, WebSocketConstants.TEXT_PARAMS_DIAG_MSG); + break; + case BINARY: + addDiagnosticsForInvalidMessageParams(difference, seenMessageParams, diagnostics, unit, + true, endpointDecodersSpecified, WebSocketConstants.BINARY_PARAMS_DIAG_MSG); + break; + case PONG: + addDiagnosticsForInvalidMessageParams(difference, seenMessageParams, diagnostics, unit, + false, false, WebSocketConstants.PONG_PARAMS_DIAG_MSG); + break; + case INVALID: + default: + addDiagnosticsForInvalidMessageParams(difference, seenMessageParams, diagnostics, unit, + false, false, WebSocketConstants.INVALID_PARAMS_DIAG_MSG); + } + } + } + } + } + + private void addDiagnosticsForInvalidMessageParams(Set diff, Map params, + List diagnostics, ICompilationUnit unit, boolean boolAllowed, boolean decodersSpecified, String msg) { + if (!decodersSpecified) { + for (String invalidParam : diff) { + if (boolAllowed && (invalidParam.equals(WebSocketConstants.BOOLEAN_OBJ))) { + continue; } + ILocalVariable param = params.get(invalidParam); + Diagnostic invalidTextParam = createDiagnostic(param, unit, + msg, "OnMessageInvalidMessageParams"); + diagnostics.add(invalidTextParam); } } } diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java new file mode 100644 index 00000000..2201c5a7 --- /dev/null +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java @@ -0,0 +1,32 @@ +package io.openliberty.sample.jakarta.websocket; + +import java.io.IOException; + +import jakarta.websocket.CloseReason; +import jakarta.websocket.OnOpen; +import jakarta.websocket.OnClose; +import jakarta.websocket.server.ServerEndpoint; + +/** + * Expected Diagnostics are related to validating that the parameters for the + * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) + * and onClose (diagnostic code: OnCloseChangeInvalidParam). + * See issues #247 (onOpen) and #248 (onClose) + */ +@ServerEndpoint(value = "/infos") +public class InvalidParamTypeBinary { + @OnOpen + public void OnOpen(Session session, Object invalidParam) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnMessage + public void OnMessage(ByteBuffer bb, PongMessage invalid) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnClose + public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + System.out.println("WebSocket closed for " + session.getId()); + } +} diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java new file mode 100644 index 00000000..5a59c4ec --- /dev/null +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java @@ -0,0 +1,32 @@ +package io.openliberty.sample.jakarta.websocket; + +import java.io.IOException; + +import jakarta.websocket.CloseReason; +import jakarta.websocket.OnOpen; +import jakarta.websocket.OnClose; +import jakarta.websocket.server.ServerEndpoint; + +/** + * Expected Diagnostics are related to validating that the parameters for the + * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) + * and onClose (diagnostic code: OnCloseChangeInvalidParam). + * See issues #247 (onOpen) and #248 (onClose) + */ +@ServerEndpoint(value = "/infos") +public class InvalidParamTypePong { + @OnOpen + public void OnOpen(Session session, Object invalidParam) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnMessage + public void OnMessage(PongMessage pong, int invalid) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnClose + public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + System.out.println("WebSocket closed for " + session.getId()); + } +} diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java new file mode 100644 index 00000000..b521eafc --- /dev/null +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java @@ -0,0 +1,32 @@ +package src.main.java.io.openliberty.sample.jakarta.websocket; + +import java.io.IOException; + +import jakarta.websocket.CloseReason; +import jakarta.websocket.OnOpen; +import jakarta.websocket.OnClose; +import jakarta.websocket.server.ServerEndpoint; + +/** + * Expected Diagnostics are related to validating that the parameters for the + * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) + * and onClose (diagnostic code: OnCloseChangeInvalidParam). + * See issues #247 (onOpen) and #248 (onClose) + */ +@ServerEndpoint(value = "/infos") +public class InvalidParamTypeText { + @OnOpen + public void OnOpen(Session session, Object invalidParam) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnMessage + public void OnMessage(Session session, String message, boolean parts, ByteBuffer invalid) throws IOException { + System.out.println("Websocket opened: " + session.getId().toString()); + } + + @OnClose + public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + System.out.println("WebSocket closed for " + session.getId()); + } +} diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java similarity index 96% rename from jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java rename to jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java index d699e56c..52b5fc97 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java @@ -14,7 +14,7 @@ * See issues #247 (onOpen) and #248 (onClose) */ @ServerEndpoint(value = "/infos") -public class InvalidParamType { +public class OnMessageInvalidParam { @OnOpen public void OnOpen(Session session, Object invalidParam) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java index 291f89d0..8fa3be18 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java @@ -98,4 +98,62 @@ public void testPathParamInvalidURI() throws Exception { assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); } + + @Test + public void testInvalidOnMessageTextParams() throws Exception { + IJavaProject javaProject = loadJavaProject("jakarta-sample", ""); + IFile javaFile = javaProject.getProject().getFile( + new Path("src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java")); + String uri = javaFile.getLocation().toFile().toURI().toString(); + + JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); + diagnosticsParams.setUris(Arrays.asList(uri)); + + Diagnostic d = d(24, 85, 92, "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + + " - String to receive the whole message\r\n" + + " - Java primitive or class equivalent to receive the whole message converted to that type\r\n" + + " - String and boolean pair to receive the message in parts\r\n" + + " - Reader to receive the whole message as a blocking stream\r\n" + + " - any object parameter for which the endpoint has a text decoder (Decoder.Text or Decoder.TextStream)", + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + + assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); + } + + @Test + public void testInvalidOnMessageBinaryParams() throws Exception { + IJavaProject javaProject = loadJavaProject("jakarta-sample", ""); + IFile javaFile = javaProject.getProject().getFile( + new Path("src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java")); + String uri = javaFile.getLocation().toFile().toURI().toString(); + + JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); + diagnosticsParams.setUris(Arrays.asList(uri)); + + Diagnostic d = d(24, 53, 60, "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + + " - byte[] or ByteBuffer to receive the whole message\r\n" + + " - byte[] and boolean pair, or ByteBuffer and boolean pair to receive the message in parts\r\n" + + " - InputStream to receive the whole message as a blocking stream\r\n" + + " - any object parameter for which the endpoint has a binary decoder (Decoder.Binary or Decoder.BinaryStream)", + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + + assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); + } + + @Test + public void testInvalidOnMessagePongParams() throws Exception { + IJavaProject javaProject = loadJavaProject("jakarta-sample", ""); + IFile javaFile = javaProject.getProject().getFile( + new Path("src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java")); + String uri = javaFile.getLocation().toFile().toURI().toString(); + + JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); + diagnosticsParams.setUris(Arrays.asList(uri)); + + Diagnostic d = d(24, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + + " - PongMessage for handling pong messages", + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + + assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); + } } \ No newline at end of file From d92e703a30414584a3535f0fd21703e96565ea67 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:03:40 -0700 Subject: [PATCH 04/11] Fix Syntax --- .../lsp4jakarta/jdt/core/websocket/WebSocketConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 7f432f33..7c5b8cf9 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -138,4 +138,4 @@ public enum MESSAGE_FORMAT {TEXT, BINARY, PONG, INVALID}; public static final String REGEX_RELATIVE_PATHS = ".*\\/\\.{0,2}\\/.*"; // Check that a URI string is a valid level 1 variable (wrapped in curly brackets): alpha-numeric characters, dash, or a percent encoded character public static final String REGEX_URI_VARIABLE = "\\{(\\w|-|%20|%21|%23|%24|%25|%26|%27|%28|%29|%2A|%2B|%2C|%2F|%3A|%3B|%3D|%3F|%40|%5B|%5D)+\\}"; - +} \ No newline at end of file From 4e07ca05febee74abfe57101cd51aefebfab3fd5 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:10:46 -0700 Subject: [PATCH 05/11] Fix File Name --- .../{OnMessageInvalidParam.java => InvalidParamType.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/{OnMessageInvalidParam.java => InvalidParamType.java} (90%) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java similarity index 90% rename from jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java rename to jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java index 52b5fc97..265a25d7 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/OnMessageInvalidParam.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java @@ -1,4 +1,4 @@ -package io.openliberty.sample.jakarta.websocket; +package src.main.java.io.openliberty.sample.jakarta.websocket; import java.io.IOException; @@ -14,7 +14,7 @@ * See issues #247 (onOpen) and #248 (onClose) */ @ServerEndpoint(value = "/infos") -public class OnMessageInvalidParam { +public class InvalidParamType { @OnOpen public void OnOpen(Session session, Object invalidParam) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); From 312c2fda97d74487d713850d8e323a76ff5a9ada Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:11:22 -0700 Subject: [PATCH 06/11] Fix File Package --- .../sample/jakarta/websocket/InvalidParamType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java index 265a25d7..bcd2da0e 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamType.java @@ -1,5 +1,5 @@ -package src.main.java.io.openliberty.sample.jakarta.websocket; - +package io.openliberty.sample.jakarta.websocket; + import java.io.IOException; import jakarta.websocket.CloseReason; From e59721fa543213ed30432d75200e147dc9a504a0 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:16:07 -0700 Subject: [PATCH 07/11] Remove Test Failures --- .../sample/jakarta/websocket/InvalidParamTypeBinary.java | 4 ++-- .../sample/jakarta/websocket/InvalidParamTypePong.java | 4 ++-- .../sample/jakarta/websocket/InvalidParamTypeText.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java index 2201c5a7..932876b4 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java @@ -16,7 +16,7 @@ @ServerEndpoint(value = "/infos") public class InvalidParamTypeBinary { @OnOpen - public void OnOpen(Session session, Object invalidParam) throws IOException { + public void OnOpen(Session session) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); } @@ -26,7 +26,7 @@ public void OnMessage(ByteBuffer bb, PongMessage invalid) throws IOException { } @OnClose - public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + public void OnClose(Session session) throws IOException { System.out.println("WebSocket closed for " + session.getId()); } } diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java index 5a59c4ec..8469c578 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java @@ -16,7 +16,7 @@ @ServerEndpoint(value = "/infos") public class InvalidParamTypePong { @OnOpen - public void OnOpen(Session session, Object invalidParam) throws IOException { + public void OnOpen(Session session) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); } @@ -26,7 +26,7 @@ public void OnMessage(PongMessage pong, int invalid) throws IOException { } @OnClose - public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + public void OnClose(Session session) throws IOException { System.out.println("WebSocket closed for " + session.getId()); } } diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java index b521eafc..625133e1 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java @@ -16,7 +16,7 @@ @ServerEndpoint(value = "/infos") public class InvalidParamTypeText { @OnOpen - public void OnOpen(Session session, Object invalidParam) throws IOException { + public void OnOpen(Session session) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); } @@ -26,7 +26,7 @@ public void OnMessage(Session session, String message, boolean parts, ByteBuffer } @OnClose - public void OnClose(Session session, CloseReason closeReason, Object invalidParam) throws IOException { + public void OnClose(Session session) throws IOException { System.out.println("WebSocket closed for " + session.getId()); } } From 33854cde2126e64540aa584240e4b78504536123 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:17:34 -0700 Subject: [PATCH 08/11] Fix Test Line Numbers --- .../lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java index 311662b4..03f7f5fc 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java @@ -176,7 +176,7 @@ public void testInvalidOnMessageTextParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(24, 85, 92, "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + Diagnostic d = d(23, 85, 92, "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + " - String to receive the whole message\r\n" + " - Java primitive or class equivalent to receive the whole message converted to that type\r\n" + " - String and boolean pair to receive the message in parts\r\n" @@ -197,7 +197,7 @@ public void testInvalidOnMessageBinaryParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(24, 53, 60, "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + Diagnostic d = d(23, 53, 60, "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + " - byte[] or ByteBuffer to receive the whole message\r\n" + " - byte[] and boolean pair, or ByteBuffer and boolean pair to receive the message in parts\r\n" + " - InputStream to receive the whole message as a blocking stream\r\n" @@ -217,7 +217,7 @@ public void testInvalidOnMessagePongParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(24, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + Diagnostic d = d(23, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + " - PongMessage for handling pong messages", DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); From 2232a10b047eb6af2c580ef73e0a8fcee34b56b0 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:32:04 -0700 Subject: [PATCH 09/11] Add Imports to Test --- .../websocket/InvalidParamTypeBinary.java | 23 ++++++++++-------- .../websocket/InvalidParamTypePong.java | 22 ++++++++++------- .../websocket/InvalidParamTypeText.java | 24 ++++++++++--------- .../jdt/websocket/JakartaWebSocketTest.java | 6 ++--- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java index 932876b4..da107adf 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeBinary.java @@ -1,20 +1,23 @@ package io.openliberty.sample.jakarta.websocket; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.ByteBuffer; -import jakarta.websocket.CloseReason; +import jakarta.websocket.server.PathParam; +import jakarta.websocket.server.ServerEndpoint; import jakarta.websocket.OnOpen; +import jakarta.websocket.PongMessage; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.EndpointConfig; import jakarta.websocket.OnClose; -import jakarta.websocket.server.ServerEndpoint; +import jakarta.websocket.Session; -/** - * Expected Diagnostics are related to validating that the parameters for the - * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) - * and onClose (diagnostic code: OnCloseChangeInvalidParam). - * See issues #247 (onOpen) and #248 (onClose) - */ -@ServerEndpoint(value = "/infos") +@ServerEndpoint(value = "/demo/{test}/var/{abcd}") public class InvalidParamTypeBinary { + private static Session session; @OnOpen public void OnOpen(Session session) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); @@ -29,4 +32,4 @@ public void OnMessage(ByteBuffer bb, PongMessage invalid) throws IOException { public void OnClose(Session session) throws IOException { System.out.println("WebSocket closed for " + session.getId()); } -} +} \ No newline at end of file diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java index 8469c578..f460585a 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypePong.java @@ -1,20 +1,24 @@ package io.openliberty.sample.jakarta.websocket; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.ByteBuffer; -import jakarta.websocket.CloseReason; +import jakarta.websocket.server.PathParam; +import jakarta.websocket.server.ServerEndpoint; import jakarta.websocket.OnOpen; +import jakarta.websocket.PongMessage; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.EndpointConfig; import jakarta.websocket.OnClose; -import jakarta.websocket.server.ServerEndpoint; +import jakarta.websocket.Session; -/** - * Expected Diagnostics are related to validating that the parameters for the - * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) - * and onClose (diagnostic code: OnCloseChangeInvalidParam). - * See issues #247 (onOpen) and #248 (onClose) - */ -@ServerEndpoint(value = "/infos") +@ServerEndpoint(value = "/demo/{test}/var/{abcd}") public class InvalidParamTypePong { + private static Session session; + @OnOpen public void OnOpen(Session session) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java index 625133e1..a1cf8c75 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/projects/jakarta-sample/src/main/java/io/openliberty/sample/jakarta/websocket/InvalidParamTypeText.java @@ -1,19 +1,21 @@ package src.main.java.io.openliberty.sample.jakarta.websocket; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.ByteBuffer; -import jakarta.websocket.CloseReason; +import jakarta.websocket.server.PathParam; +import jakarta.websocket.server.ServerEndpoint; import jakarta.websocket.OnOpen; +import jakarta.websocket.PongMessage; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.EndpointConfig; import jakarta.websocket.OnClose; -import jakarta.websocket.server.ServerEndpoint; +import jakarta.websocket.Session; -/** - * Expected Diagnostics are related to validating that the parameters for the - * WebSocket methods are of valid types for onOpen (diagnostic code: OnOpenChangeInvalidParam) - * and onClose (diagnostic code: OnCloseChangeInvalidParam). - * See issues #247 (onOpen) and #248 (onClose) - */ -@ServerEndpoint(value = "/infos") +@ServerEndpoint(value = "/demo/{test}/var/{abcd}") public class InvalidParamTypeText { @OnOpen public void OnOpen(Session session) throws IOException { @@ -21,7 +23,7 @@ public void OnOpen(Session session) throws IOException { } @OnMessage - public void OnMessage(Session session, String message, boolean parts, ByteBuffer invalid) throws IOException { + public void OnMessage(Session session, String message, ByteBuffer invalid) throws IOException { System.out.println("Websocket opened: " + session.getId().toString()); } @@ -29,4 +31,4 @@ public void OnMessage(Session session, String message, boolean parts, ByteBuffer public void OnClose(Session session) throws IOException { System.out.println("WebSocket closed for " + session.getId()); } -} +} \ No newline at end of file diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java index 03f7f5fc..eebfa066 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java @@ -176,7 +176,7 @@ public void testInvalidOnMessageTextParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(23, 85, 92, "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + Diagnostic d = d(25, 70, 77, "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + " - String to receive the whole message\r\n" + " - Java primitive or class equivalent to receive the whole message converted to that type\r\n" + " - String and boolean pair to receive the message in parts\r\n" @@ -197,7 +197,7 @@ public void testInvalidOnMessageBinaryParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(23, 53, 60, "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + Diagnostic d = d(26, 53, 60, "Invalid parameter type. OnMessage methods for handling binary messages may have the following parameters: \r\n" + " - byte[] or ByteBuffer to receive the whole message\r\n" + " - byte[] and boolean pair, or ByteBuffer and boolean pair to receive the message in parts\r\n" + " - InputStream to receive the whole message as a blocking stream\r\n" @@ -217,7 +217,7 @@ public void testInvalidOnMessagePongParams() throws Exception { JakartaDiagnosticsParams diagnosticsParams = new JakartaDiagnosticsParams(); diagnosticsParams.setUris(Arrays.asList(uri)); - Diagnostic d = d(23, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + Diagnostic d = d(27, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + " - PongMessage for handling pong messages", DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); From 594ec3a3320bd84f394a8de00aac87c0e7173381 Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:44:45 -0700 Subject: [PATCH 10/11] Fix Expected Diagnostic Code --- .../jdt/core/websocket/WebSocketConstants.java | 9 +++------ .../lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 7c5b8cf9..89ea1bf5 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -87,14 +87,11 @@ public class WebSocketConstants { public static final Set ON_MESSAGE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.Session")); public static final Set RAW_ON_MESSAGE_PARAM_OPT_TYPES = new HashSet<>(Arrays.asList("Session")); /* For OnMessage (Text) annotation */ - public static final Set ON_MESSAGE_TEXT_TYPES = new HashSet<>(Arrays.asList("java.lang.String", "java.io.Reader")); - public static final Set RAW_ON_MESSAGE_TEXT_TYPES = new HashSet<>(Arrays.asList("String", "Reader")); + public static final Set ON_MESSAGE_TEXT_TYPES = new HashSet<>(Arrays.asList("java.lang.String", "java.io.Reader", "String", "Reader")); /* For OnMessage (Text) annotation */ - public static final Set ON_MESSAGE_BINARY_TYPES = new HashSet<>(Arrays.asList("java.nio.ByteBuffer", "java.io.InputStream")); - public static final Set RAW_ON_MESSAGE_BINARY_TYPES = new HashSet<>(Arrays.asList("ByteBuffer", "InputStream")); + public static final Set ON_MESSAGE_BINARY_TYPES = new HashSet<>(Arrays.asList("java.nio.ByteBuffer", "java.io.InputStream", "ByteBuffer", "InputStream")); /* For OnMessage (Text) annotation */ - public static final Set ON_MESSAGE_PONG_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.PongMessage")); - public static final Set RAW_ON_MESSAGE_PONG_TYPES = new HashSet<>(Arrays.asList("PongMessage")); + public static final Set ON_MESSAGE_PONG_TYPES = new HashSet<>(Arrays.asList("jakarta.websocket.PongMessage", "PongMessage")); /* Wrapper Objects */ public static final Set RAW_WRAPPER_OBJS = new HashSet<>(Arrays.asList("String", "Boolean", "Integer", "Long", "Double", "Float")); diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java index eebfa066..ce20edf9 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.test/src/main/java/org/eclipse/lsp4jakarta/jdt/websocket/JakartaWebSocketTest.java @@ -182,7 +182,7 @@ public void testInvalidOnMessageTextParams() throws Exception { + " - String and boolean pair to receive the message in parts\r\n" + " - Reader to receive the whole message as a blocking stream\r\n" + " - any object parameter for which the endpoint has a text decoder (Decoder.Text or Decoder.TextStream)", - DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidMessageParams"); assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); } @@ -202,7 +202,7 @@ public void testInvalidOnMessageBinaryParams() throws Exception { + " - byte[] and boolean pair, or ByteBuffer and boolean pair to receive the message in parts\r\n" + " - InputStream to receive the whole message as a blocking stream\r\n" + " - any object parameter for which the endpoint has a binary decoder (Decoder.Binary or Decoder.BinaryStream)", - DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidMessageParams"); assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); } @@ -219,7 +219,7 @@ public void testInvalidOnMessagePongParams() throws Exception { Diagnostic d = d(27, 48, 55, "Invalid parameter type. OnMessage methods for handling pong messages may have the following parameters: \r\n" + " - PongMessage for handling pong messages", - DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidPathParam"); + DiagnosticSeverity.Error, "jakarta-websocket", "OnMessageInvalidMessageParams"); assertJavaDiagnostics(diagnosticsParams, JDT_UTILS, d); } From 4464a60058815fa9d29968268dc0c04c7cbea5cc Mon Sep 17 00:00:00 2001 From: Aviral Saxena Date: Mon, 11 Apr 2022 23:59:04 -0700 Subject: [PATCH 11/11] Move Constants --- .../jdt/core/websocket/WebSocketConstants.java | 7 ++++++- .../websocket/WebSocketDiagnosticsCollector.java | 14 +++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java index 89ea1bf5..843968eb 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketConstants.java @@ -48,7 +48,7 @@ public class WebSocketConstants { /* Diagnostic codes */ public static final String DIAGNOSTIC_CODE_ON_OPEN_INVALID_PARAMS = "OnOpenChangeInvalidParam"; public static final String DIAGNOSTIC_CODE_ON_CLOSE_INVALID_PARAMS = "OnCloseChangeInvalidParam"; - public static final String DIAGNOSTIC_CODE_ON_MESSAGE_INVALID_PARAMS = "OnMessageChangeInvalidParam"; + public static final String DIAGNOSTIC_CODE_ON_MESSAGE_INVALID_PARAMS = "OnMessageInvalidMessageParams"; public static final String DIAGNOSTIC_SERVER_ENDPOINT_NO_SLASH = "Server endpoint paths must start with a leading '/'."; public static final String DIAGNOSTIC_SERVER_ENDPOINT_NOT_LEVEL1 = "Server endpoint paths must be a URI-template (level-1) or a partial URI."; @@ -109,6 +109,11 @@ public class WebSocketConstants { // Messages public static final String PARAM_TYPE_DIAG_MSG = "Invalid parameter type. When using %s, parameter must be of type: \n- %s\n- annotated with @PathParams and of type String or any Java primitive type or boxed version thereof"; + public static final String ONMESSAGE_DUPLICATE_SPECIAL_MSG = "Only one optional parameter of this type is allowed for a method with the @OnMessage annotation."; + public static final String ONMESSAGE_INVALID_PATH_PARAM_MSG = "Only String and Java primitive types are allowed to be annotated with the @PathParam annotation."; + public static final String ONMESSAGE_DUPLICATE_MESSAGE_PARAM_MSG = "Multiple parameters of this type are not allowed for a method with the @OnMessage annotation."; + + public static final String TEXT_PARAMS_DIAG_MSG = "Invalid parameter type. OnMessage methods for handling text messages may have the following parameters: \r\n" + " - String to receive the whole message\r\n" + " - Java primitive or class equivalent to receive the whole message converted to that type\r\n" diff --git a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java index 807ae7c6..8dd8931c 100644 --- a/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java +++ b/jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/src/main/java/org/eclipse/lsp4jakarta/jdt/core/websocket/WebSocketDiagnosticsCollector.java @@ -195,8 +195,8 @@ private void onMessageParamsCheck(IType type, ICompilationUnit unit, List diff, Map