Skip to content

Commit

Permalink
Merge pull request #1056 from anusreelakshmi934/Issue#1031
Browse files Browse the repository at this point in the history
Removed unused unqualified names from WebSocketConstants
  • Loading branch information
anusreelakshmi934 authored Nov 7, 2024
2 parents 9272646 + b0ee2e1 commit 001047d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Copyright (c) 2022, 2023 IBM Corporation and others.
/** Copyright (c) 2022, 2024 IBM Corporation and others.
*
* 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 All @@ -19,15 +19,13 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class WebSocketConstants {
public static final String DIAGNOSTIC_SOURCE = "jakarta-websocket";

public static final DiagnosticSeverity ERROR = DiagnosticSeverity.Error;
public static final DiagnosticSeverity WARNING = DiagnosticSeverity.Warning;

public static final String DIAGNOSTIC_ERR_MSG = "Cannot calculate WebSocket diagnostics";


public static final String PATHPARAM_ANNOTATION = "jakarta.websocket.server.PathParam";

Expand Down Expand Up @@ -58,26 +56,23 @@ public class WebSocketConstants {
public static final String CLIENT_ENDPOINT_ANNOTATION = "jakarta.websocket.ClientEndpoint";

// Superclass
public static final String ENDPOINT_SUPERCLASS = "Endpoint";
public static final String FQ_ENDPOINT_SUPERCLASS = "jakarta.websocket.Endpoint";
public static final String IS_SUPERCLASS = "isSuperclass";

public static final String[] WS_ANNOTATION_CLASS = { SERVER_ENDPOINT_ANNOTATION, CLIENT_ENDPOINT_ANNOTATION };

public static final String BOOLEAN = "java.lang.Boolean";
public static final String INTEGER = "java.lang.Integer";
public static final String LONG = "java.lang.Long";
public static final String DOUBLE = "java.lang.Double";
public static final String FLOAT = "java.lang.Float";
public static final String STRING_CLASS_LONG = "java.lang.String";
public static final String STRING_CLASS_SHORT = "String";
public static final String READER_CLASS_LONG = "java.io.Reader";
public static final String READER_CLASS_SHORT = "Reader";
public static final String BYTEBUFFER_CLASS_LONG = "java.nio.ByteBuffer";
public static final String BYTEBUFFER_CLASS_SHORT = "ByteBuffer";
public static final String INPUTSTREAM_CLASS_LONG = "java.io.InputStream";
public static final String INPUTSTREAM_CLASS_SHORT = "InputStream";
public static final String PONGMESSAGE_CLASS_LONG = "jakarta.websocket.PongMessage";
public static final String PONGMESSAGE_CLASS_SHORT = "PongMessage";
public static final Set<String> LONG_MESSAGE_CLASSES = new HashSet<>(
Arrays.asList(STRING_CLASS_LONG, READER_CLASS_LONG, BYTEBUFFER_CLASS_LONG, INPUTSTREAM_CLASS_LONG, PONGMESSAGE_CLASS_LONG));
public static final Set<String> SHORT_MESSAGE_CLASSES = new HashSet<>(
Arrays.asList(STRING_CLASS_SHORT, READER_CLASS_SHORT, BYTEBUFFER_CLASS_SHORT, INPUTSTREAM_CLASS_SHORT, PONGMESSAGE_CLASS_SHORT));
public static final String SESSION_CLASS = "jakarta.websocket.Session";

/* Annotations */
Expand All @@ -93,18 +88,12 @@ public class WebSocketConstants {
// For OnOpen annotation
public static final Set<String> ON_OPEN_PARAM_OPT_TYPES = new HashSet<>(
Arrays.asList("jakarta.websocket.EndpointConfig", SESSION_CLASS));
public static final Set<String> RAW_ON_OPEN_PARAM_OPT_TYPES = new HashSet<>(
Arrays.asList("EndpointConfig", "Session"));

public static final Set<String> ON_CLOSE_PARAM_OPT_TYPES = new HashSet<>(
Arrays.asList("jakarta.websocket.CloseReason", SESSION_CLASS));
public static final Set<String> RAW_ON_CLOSE_PARAM_OPT_TYPES = new HashSet<>(
Arrays.asList("CloseReason", "Session"));

public static final Set<String> RAW_WRAPPER_OBJS = new HashSet<>(
Arrays.asList("String", "Boolean", "Integer", "Long", "Double", "Float"));
public static final Set<String> WRAPPER_OBJS = RAW_WRAPPER_OBJS.stream().map(raw -> "java.lang.".concat(raw))
.collect(Collectors.toSet());
Arrays.asList(STRING_CLASS_LONG, BOOLEAN, INTEGER, LONG, DOUBLE, FLOAT ));

// Enums
public enum MESSAGE_FORMAT {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022, 2023 IBM Corporation and others.
* Copyright (c) 2022, 2024 IBM Corporation and others.
*
* 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 @@ -76,19 +76,17 @@ private void invalidParamsCheck(PsiClass type, PsiJavaFile unit, List<Diagnostic
PsiMethod[] allMethods = type.getMethods();
for (PsiMethod method : allMethods) {
PsiAnnotation[] allAnnotations = method.getAnnotations();
Set<String> specialParamTypes = null, rawSpecialParamTypes = null;
Set<String> specialParamTypes = null;

for (PsiAnnotation annotation : allAnnotations) {
String annotationName = annotation.getQualifiedName();
String diagnosticCode = null;

if (isMatchedJavaElement(type, annotationName, WebSocketConstants.ON_OPEN)) {
specialParamTypes = WebSocketConstants.ON_OPEN_PARAM_OPT_TYPES;
rawSpecialParamTypes = WebSocketConstants.RAW_ON_OPEN_PARAM_OPT_TYPES;
diagnosticCode = WebSocketConstants.DIAGNOSTIC_CODE_ON_OPEN_INVALID_PARAMS;
} else if (isMatchedJavaElement(type, annotationName, WebSocketConstants.ON_CLOSE)) {
specialParamTypes = WebSocketConstants.ON_CLOSE_PARAM_OPT_TYPES;
rawSpecialParamTypes = WebSocketConstants.RAW_ON_CLOSE_PARAM_OPT_TYPES;
diagnosticCode = WebSocketConstants.DIAGNOSTIC_CODE_ON_CLOSE_INVALID_PARAMS;
}
if (diagnosticCode != null) {
Expand Down Expand Up @@ -188,9 +186,7 @@ private void onMessageWSMessageFormats(PsiClass type, List<Diagnostic> diagnosti

if (typeName != null
&& WebSocketConstants.LONG_MESSAGE_CLASSES.contains(typeName)) {
WebSocketConstants.MESSAGE_FORMAT messageFormat = typeName != null
? getMessageFormat(typeName, true)
: getMessageFormat(typeName, false);
WebSocketConstants.MESSAGE_FORMAT messageFormat = getMessageFormat(typeName);
switch (messageFormat) {
case TEXT:
if (onMessageTextUsed != null) {
Expand Down Expand Up @@ -325,8 +321,7 @@ private List<String> findAndProcessEndpointURI(PsiClass type) {
* @return if valueClass is a wrapper object
*/
private boolean isWrapper(String valueClass) {
return WebSocketConstants.WRAPPER_OBJS.contains(valueClass)
|| WebSocketConstants.RAW_WRAPPER_OBJS.contains(valueClass);
return WebSocketConstants.RAW_WRAPPER_OBJS.contains(valueClass);
}

/**
Expand Down Expand Up @@ -371,37 +366,15 @@ private boolean isParamPath(PsiClass type, PsiParameter param) {
}
return false;
}
private WebSocketConstants.MESSAGE_FORMAT getMessageFormat(String typeName, boolean longName) {
if (longName) {
switch (typeName) {
case WebSocketConstants.STRING_CLASS_LONG:
return WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.READER_CLASS_LONG:
return WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.BYTEBUFFER_CLASS_LONG:
return WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.INPUTSTREAM_CLASS_LONG:
return WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.PONGMESSAGE_CLASS_LONG:
return WebSocketConstants.MESSAGE_FORMAT.PONG;
default:
throw new IllegalArgumentException("Invalid message format type");
}
}
switch (typeName) {
case WebSocketConstants.STRING_CLASS_SHORT:
return WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.READER_CLASS_SHORT:
return WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.BYTEBUFFER_CLASS_SHORT:
return WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.INPUTSTREAM_CLASS_SHORT:
return WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.PONGMESSAGE_CLASS_SHORT:
return WebSocketConstants.MESSAGE_FORMAT.PONG;
default:
throw new IllegalArgumentException("Invalid message format type");
}
private WebSocketConstants.MESSAGE_FORMAT getMessageFormat(String typeName) {
return switch (typeName) {
case WebSocketConstants.STRING_CLASS_LONG -> WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.READER_CLASS_LONG -> WebSocketConstants.MESSAGE_FORMAT.TEXT;
case WebSocketConstants.BYTEBUFFER_CLASS_LONG -> WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.INPUTSTREAM_CLASS_LONG -> WebSocketConstants.MESSAGE_FORMAT.BINARY;
case WebSocketConstants.PONGMESSAGE_CLASS_LONG -> WebSocketConstants.MESSAGE_FORMAT.PONG;
default -> throw new IllegalArgumentException("Invalid message format type");
};
}

private String createParamTypeDiagMsg(Set<String> methodParamOptTypes, String methodAnnotTarget) {
Expand Down

0 comments on commit 001047d

Please sign in to comment.